Under construction

Still being built. Things may change

Rainfall Storage

Time limit2sMemory limit512 MB

Summary
Given N pillars, arrange them in any order and list every total rain volume obtainable, where volume sums the water trapped above each pillar.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Combinatorics, Greedy
Solved
No attempts yet

Problem

It was a dark and stormy night. It rained, and rained, and rained.

Lucy wants to capture some of that rain, but her materials are limited. She owns a collection of pillars of various heights. Every pillar has an integer height and a base of 1 by 1. She stands all of the pillars in a row, in any order she likes, and she has enough siding to close off the front and the back, so rain fills every space between the pillars. There is more than enough rain, and whatever overflows soaks into the ground.

Number the pillars of an arrangement a1,a2,…,aNa_1, a_2, \dots, a_N from left to right. The water resting on top of pillar ii has height min⁡(max⁡(a1,…,ai), max⁡(ai,…,aN))−ai\min(\max(a_1, \dots, a_i),\ \max(a_i, \dots, a_N)) - a_i, and the captured volume is the sum of that value over every ii.

For example, with pillars of heights 1, 5, 2, 1, 4, Lucy could build the arrangement below, drawn from the side.

 *
 *  *
 *  *
 ** *
*****

It captures 5 units of rain, marked with R.

 *
 *RR*
 *RR*
 **R*
*****

The same five pillars can also capture 6 units.

 *
 *RR*
 *RR*
**RR*
*****

With pillars of heights 5, 1, 5, 1, 5, this arrangement captures 8 units.

*R*R*
*R*R*
*R*R*
*R*R*
*****

This arrangement of 5, 1, 4, 1, 5 captures 9 units.

*RRR*
*R*R*
*R*R*
*R*R*
*****

Lucy has NN pillars of heights h1,h2,…,hNh_1, h_2, \dots, h_N, and she uses all of them in every arrangement. She wants to know every volume of rain she can capture.

Input

The first line contains the integer NN (2≤N≤5002 \le N \le 500), the number of pillars.

The second line contains NN integers h1,h2,…,hNh_1, h_2, \dots, h_N (1≤hi≤501 \le h_i \le 50), where hih_i is the height of the ii-th pillar.

Output

On one line, print every obtainable volume of captured rain in increasing order, separated by single spaces.

Examples3

  1. Example 1

    Input
    5
    1 5 2 1 4
    
    Expected output
    0 1 2 3 4 5 6 8
    
  2. Example 2

    Input
    5
    5 1 5 1 5
    
    Expected output
    0 4 8
    
  3. Example 3

    Input
    5
    5 1 4 1 5
    
    Expected output
    0 1 3 4 5 6 7 8 9