Lining up at the laundromat
Statement
A neighborhood laundromat has one washing machine. N people holding laundry baskets are gathered there right now. The machine can run one person's laundry at a time, and the next person's laundry goes in only after one person's laundry is done. The time a wash takes differs from person to person.
A person's waiting time is the time it takes until their own laundry is done. That is, the wash times of everyone ahead of them added together, plus their own wash time.
The total of everyone's waiting times changes with the order the line is set in. The aim is to make this total as small as possible.
For example, suppose the wash times of N=5 people are the ones in the following table.
| Person | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Wash time | 3 | 1 | 4 | 3 | 2 |
Suppose these five people line up in the order person 2, person 5, person 1, person 4, person 3. Each person's waiting time is the following.
| Place in line | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Person | 2 | 5 | 1 | 4 | 3 |
| Wash time | 1 | 2 | 3 | 3 | 4 |
| Waiting time | 1 | 3 | 6 | 9 | 13 |
Adding up the waiting times gives 1 + 3 + 6 + 9 + 13 = 32. This order makes the total as small as it gets, so the answer in this case is 32.
Let us see how the total changes when the line is set the other way round. Lining the same five people up in the order person 3, person 1, person 4, person 5, person 2 gives the following.
| Place in line | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Person | 3 | 1 | 4 | 5 | 2 |
| Wash time | 4 | 3 | 3 | 2 | 1 |
| Waiting time | 4 | 7 | 10 | 12 | 13 |
Here the total of the waiting times is 4 + 7 + 10 + 12 + 13 = 46. The last person's waiting time is 13 in both cases, because the machine never stops running. What changes is the waiting times of the people ahead, and that pushes the total from 32 up to 46.
Given the wash times of the N people, write a program that finds the smallest total of everyone's waiting times.
If N is 1,000 and every person's wash time is 1,000 minutes, the answer is 500,500,000. That is the largest the answer gets, and it fits inside the range of a 32-bit integer, so there is no need to worry about a number overflowing.
Input and output examples
Example 1
Input
5 3 1 4 3 2
Output
32
Example 2
Input
1 606
Output
606
Example 3
Input
2 986 324
Output
1634
Input
The first line gives the number of people N gathered at the laundromat. N is a natural number less than or equal to 1,000.
The second line gives the wash times of the N people, from person 1 to person N in order, separated by one space. Each wash time is a natural number from 1 to 1,000.
Several people can have the same wash time. The order the input gives them in has nothing to do with the order the line is set in.
Output
Print the smallest total of everyone's waiting times on the first line.
Print one number and then a newline. Do not add any other character or space before or after it.
Requirements
- Read the input from standard input and write the answer to standard output
- There is only one washing machine and it runs one person's laundry at a time. It cannot stop partway through to fit another person's laundry in
- A person's waiting time includes their own wash time
- The value to find is not one person's waiting time but the waiting times of all N people added together
- Several people can have the same wash time
- Grading compares the output with the expected value down to the last character
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 606
606
2 986 324
1634
5 3 1 4 3 2
32
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.