Mex and Cards
Time limit3sMemory limit1024 MB
Given counts of cards with values 0 to n-1, find the maximum sum of mexes over all piles after the initial deck and after each of q single-card additions or removals.
- Level
Medium7 of 10
- Topics
- Segment tree, Greedy, Math
- Solved
- No attempts yet
Statement
Mike enjoys playing with cards. Each card in his deck has a single integer value from to written on it. Initially the deck contains cards with value .
Today Mike is learning the concept of mex. The mex of a collection of integers is the smallest non-negative integer that does not belong to the collection. For instance, .
Mike will distribute all cards in his deck into non-empty piles. Each card must belong to exactly one pile. He will then find the mex of the card values in each pile and add them all together. Mike wants to find a distribution that maximizes this sum.
A sequence of modifications happens to the deck. Sometimes a new card is added, and other times a card is removed. Mike wants to find the distribution with the maximum sum of mexes before the first modification and after the first modifications for every .
Input
The first line contains a single integer , the range of card values ().
The second line contains integers , the number of cards with value in the deck initially ().
The third line contains a single integer , the number of deck modifications ().
Each of the next lines contains two integers and , describing the -th modification (, ). If , a new card with value is added to the deck. If , a card with value is removed from the deck.
It is guaranteed that if , then the deck contains at least one card with value right before the -th modification.
Output
Print integers. The -th integer is the maximum possible sum of mexes for some valid distribution of all cards into piles after the first modifications of the deck ().
Hint
In the example test, one of the best distributions for the initial deck is to put the cards with values and into one pile, the cards with values into another pile, and the card with value into a third pile. The sum of mexes for this distribution is .