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

#DSU0000083. Các bình thông nhau – Bản khó (Vessels, Heights and Two Versions, Hard 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 – Bản khó (Vessels, Heights and Two Versions, Hard Version)

Vessels, Heights and Two Versions (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn cylindrical vessels arranged in a circle. Each vessel has a base area of 11 cm2^2 and unbounded height. Vessels ii and i+1i+1 are connected at height hih_i; vessel nn is connected to vessel 11 at height hnh_n. The connections have negligible volume.

Let wiw_i be the water level in vessel ii, where all levels are nonnegative integers. Adjacent vessels can have different levels while the connecting pipe has not been reached. An arrangement is valid exactly when, 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 position kk, independently maximize w1+⋯+wnw_1+\cdots+w_n under the additional restriction wk=0w_k=0. Print one maximum for every choice of the empty vessel.

Input

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

Output

Print nn integers: the kk-th is the maximum total volume with vessel kk empty.

Subtasks

  • Subtask 1 (20 points): 3≤n≤153\le n\le 15, 1≤hi≤401\le h_i\le 40.
  • Subtask 2 (30 points): 3≤n≤30003\le n\le 3000, 1≤hi≤1061\le h_i\le 10^6.
  • Subtask 3 (50 points): 3≤n≤2⋅1053\le n\le 2\cdot10^5, 1≤hi≤1091\le h_i\le 10^9.

Example

Input

4
1 2 3 4

Output

6 6 7 9

Explanation

With vessel 11 empty, [0,1,2,3][0,1,2,3] has total volume 66. With vessel 44 empty, [3,3,3,0][3,3,3,0] has total volume 99. These are independent scenarios.