Under construction

Still being built. Things may change

Heavy Stones

Time limit2sMemory limit512 MB

Summary
For each starting pile k, find the minimum total cost of merging all piles into one when every merge must extend the currently selected pile to a neighbor.
Level

Hard9 of 10

Topics
Greedy, Dynamic programming
Solved
No attempts yet

Problem

After learning the Garsia-Wachs algorithm, you came up with the following problem.

There are nn piles of stones in a line. The ii-th pile contains aia_i stones. You want to merge all the stones into one pile.

At first, you select the kk-th pile. Then you perform 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 performing this operation n−1n-1 times, only one pile is 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 kk-th pile initially. For k=1,2,…,nk=1, 2, \ldots, n, output the answer.

Input

The first line contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1061 \le a_i \le 10^6).

Output

Output nn integers. The kk-th number is the smallest total cost if you select the kk-th pile initially.

Hint

If you select the 4th 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.

Examples2

  1. Example 1

    Input
    5
    2 1 3 5 4
    
    Expected output
    35 35 36 43 49
    
  2. Example 2

    Input
    10
    18 37 81 6 58 99 87 34 75 9
    
    Expected output
    2637 2637 2657 2657 2695 2949 2995 2905 2880 2880