Flowing Fountain
InterviewTime limit5sMemory limit2048 MB
Champagne poured into a level spills into the first lower level with a larger capacity, and the task is to answer how much each level holds.
- Level
Medium6 of 10
- Topics
- Stack, Union-find, Simulation
- Solved
- No attempts yet
Problem
Last week, Bill filled a champagne fountain for the first time. Delighted by the champagne pouring from one glass into another, he decided to organize an even bigger champagne fountain for the next World Finals. He has already ordered glass bowls with different capacities to stack on top of each other into a huge glass fountain. However, he is still unsure how to pour the champagne into it. One bottle will not be enough, and pouring from the top might not fill every bowl. Bill wants to try out different ways to fill the fountain, but wasting any champagne would be a shame.

The th image visualizes the th query of type '+'.
You are tasked with writing a program that simulates pouring champagne into a given fountain. If a bowl in some level is already full, the champagne spills over to the first level below it with a larger capacity. If that level is also full, the champagne spills further down, until it seeps into the ground and is wasted. Bill also wants to know, at certain points in the simulation, how much champagne is in a given level.
Input
The first line has two integers and (), the number of levels and the number of queries.
The second line has distinct integers (), the capacity of each level in litres. The levels are given from top to bottom.
Each of the next lines describes a query. The first symbol is either '+' or '?'.
- If is '
+', two integers and follow (, ). Bill pours litres of champagne into level . - If is '
?', one integer follows (). Bill requests the current amount of champagne in level in litres.
It is guaranteed that there is at least one query of type '?'.
Output
For each query of type '?', output the amount of champagne in the requested level in litres.