Jerry's Trap
InterviewTime limit2sMemory limit1024 MB
Given platform heights, after each uniform shift x, print the sum of absolute values of all heights, with shifts accumulating.
- Level
Medium4 of 10
- Topics
- Sorting, Prefix sum, Binary search
- Solved
- No attempts yet
Statement
Tom has finally managed to trap Jerry. The trap is a water tank with platforms fixed inside. The height of platform relative to the water level is .
Each platform is a danger to Jerry. If , the platform is underwater and its danger is . If , Jerry can fall from the platform, and its danger is . So the danger of platform is exactly .
Tom has access to a control panel that changes the heights of all platforms by the same number , so the new height of platform becomes . Tom can use the panel times before Jerry gets out of the trap. After each action, print the total danger of the trap, that is, the sum of the dangers of all platforms.
Input
The first line contains an integer (), the number of platforms. The second line contains integers separated by spaces ().
The third line contains an integer (), the number of times Tom changes the heights. The last line contains integers , where is the amount by which Tom changes the heights in the -th action ().
Output
After each action of Tom, print on a new line the total danger of the trap.
Hint
In the first example, after the first action the heights become . Then Tom lowers them by more and gets . After the last action the heights are .
In the second example, Tom lowers all heights by five times in a row. Each action adds negative numbers, so the answer first decreases and then increases. At the end the heights are , which gives the same answer as after the first action, when the heights were .