Joker's Card Trick
Time limit3sMemory limit512 MB
After each point update to a row of nonzero integers, find the smallest prefix index maximizing the running sum of values scaled by the total positive and total negative sums.
- Level
Hard8 of 10
- Topics
- Segment tree, Prefix sum, Greedy, Math
- Solved
- No attempts yet
Problem
Joker is preparing a new card trick that needs some arithmetic. Help him with the calculation.
A row holds cards, and card carries a nonzero integer . Let be the sum of all positive numbers in the row, and let be the sum of all negative numbers. Card has weight when , and when .
Let . Joker wants the position with the largest . If several positions give that largest value, he takes the smallest one.
A trick with a fixed row is boring. Joker changes the numbers written on the cards, and after every change he wants the position with the largest again.
Input
The first line contains two integers and , the number of cards and the number of changes ().
The second line contains integers , the numbers written on the cards at the start (, ).
Each of the next lines contains two integers and , meaning that the number on the card at position becomes (, , ).
At every moment the row contains at least one card with a positive number and at least one card with a negative number. The sum of the positive numbers never exceeds , and the sum of the absolute values of the negative numbers never exceeds .
Output
Print lines.
On the first line print the position with the largest for the initial numbers. On each of the following lines print the position with the largest right after the corresponding change, one position per line.