PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000080. Các bình thông nhau (Vessels, Heights and Two Versions – Easy Version)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các bình thông nhau (Vessels, Heights and Two Versions – Easy Version)

Vessels, Heights and Two Versions (Easy Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn vessels arranged in a circle and numbered from 11 to nn. Every vessel has unlimited height and base area 11 cm2^2.

Vessel ii is connected to vessel i+1i+1 by a pipe at height hih_i cm, and vessel nn is connected to vessel 11 at height hnh_n. Pipe volumes are negligible. If the water level in either neighboring vessel exceeds the pipe's height, the water levels in both vessels must be equal.

Let wiw_i denote the non-negative integer water level of vessel ii, also equal to its water volume in cm3^3. A configuration is valid if the following holds for every ii, with wn+1=w1w_{n+1}=w_1:

max⁡(wi,wi+1)>hi  ⟹  wi=wi+1.\max(w_i,w_{i+1})>h_i \implies w_i=w_{i+1}.

For each ii from 11 to nn, independently consider the condition that vessel ii is empty, i.e. wi=0w_i=0. Determine the maximum total water volume in all nn vessels for that condition.

Each empty-vessel choice is a separate scenario; water configurations are not carried over between scenarios.

Input

  • The first line contains the integer nn.
  • The second line contains nn integers h1,h2,…,hnh_1,h_2,\ldots,h_n.

Output

Print nn integers on one line. The ii-th integer is the maximum total water volume if vessel ii is empty.

Subtasks

  • Subtask 1 (20 points): 3≤n≤123\le n\le12, 1≤hi≤301\le h_i\le30.
  • Subtask 2 (30 points): 3≤n≤3003\le n\le300, 1≤hi≤1061\le h_i\le10^6.
  • Subtask 3 (50 points): 3≤n≤30003\le n\le3000, 1≤hi≤1091\le h_i\le10^9.

Examples

Input

4
1 2 3 4

Output

6 6 7 9

Explanation

When vessel 11 must be empty, water levels [0,1,2,3][0,1,2,3] give a total volume of 66.

When vessel 33 must be empty, an optimal configuration is [2,2,0,3][2,2,0,3], totaling 77.

When vessel 44 must be empty, levels [3,3,3,0][3,3,3,0] give a total of 99. Adding more water would violate at least one pipe constraint.