시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB53282454.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.

예제 입력 1

5
2 1 3 5 4

예제 출력 1

35 35 36 43 49

예제 입력 2

10
18 37 81 6 58 99 87 34 75 9

예제 출력 2

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.