Rainfall Storage
Time limit2sMemory limit512 MB
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 from left to right. The water resting on top of pillar has height , and the captured volume is the sum of that value over every .
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 pillars of heights , 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 (), the number of pillars.
The second line contains integers (), where is the height of the -th pillar.
Output
On one line, print every obtainable volume of captured rain in increasing order, separated by single spaces.