#C14258. Calculate Total Waiting Time

    ID: 43887 Type: Default 1000ms 256MiB

Calculate Total Waiting Time

Calculate Total Waiting Time

Given a queue of bank customers with individual transaction times, your task is to compute the total waiting time for all customers. The waiting time for a customer is the sum of transaction times of all the customers before them. If there is only one customer or no customer at all, the waiting time is 0.

Formally, if there are n customers with transaction times \(t_1, t_2, \dots, t_n\), then the total waiting time is defined as:

\(\text{Total Waiting Time} = \sum_{i=1}^{n-1} \sum_{j=1}^{i} t_j\)

For example, for transaction times [5, 3, 8, 2], the waiting times are computed as follows: the first customer waits 0, the second waits 5, the third waits 5+3, and the fourth waits 5+3+8. So, the total is 0 + 5 + 8 + 16 = 29.

inputFormat

The input is read from standard input (stdin) in the following format:

  • The first line contains an integer n indicating the number of customers.
  • The second line contains n space-separated integers representing the transaction times of the customers.

If n is 0, there are no customers and the output should be 0.

outputFormat

The output is a single integer printed to standard output (stdout) representing the total waiting time for all customers.

## sample
4
5 3 8 2
29