시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 53 | 28 | 24 | 54.545% |
After learning Garsia–Wachs algorithm, you came up with the following problem.
There are n piles of stones in a line. The i-th pile contains ai stones. You want to merge all the stones into one pile.
At first, you will select the k-th pile. Then you can do the following operation on the selected pile: Choose the left or right adjacent pile of the selected one, and merge them into one pile. The new pile becomes the selected pile after the operation. After doing this operation n − 1 times, there will be only one pile left. The cost of each merge operation is the number of stones in the new pile.
You want to know the smallest total cost if you select the k-th pile initially. For k = 1, 2, . . . , n, output the answer.
The first line contains an integer n (1 ≤ n ≤ 2 · 105).
The second line contains n integers a1, a2, . . . , an (1 ≤ ai ≤ 106).
Output n integers. The k-th number indicates the smallest total cost if you select the k-th pile initially.
5 2 1 3 5 4
35 35 36 43 49
10 18 37 81 6 58 99 87 34 75 9
2637 2637 2657 2657 2695 2949 2995 2905 2880 2880
If you select the 4-th pile initially, the process can go as follows:
{2, 1, 3, 5, 4} → {2, 1, 8, 4} → {2, 9, 4} → {11, 4} → {15}.
The total cost is 8 + 9 + 11 + 15 = 43.