#P13610. [NWRRC 2022] Mex and Cards
[NWRRC 2022] Mex and Cards
题目描述
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 . The mex of a collection of integers is the smallest non-negative integer that does not belong to the collection. For instance, $\operatorname{mex}(\{4, 1, 4, 12, 0, 7, 0, 0, 5\}) = 2$.
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.
Moreover, a sequence of modifications happens to the deck: sometimes a new card is added to the deck, while other times a card is removed from the deck. Mike wants to find the distribution with the maximum sum of mexes at every point in the sequence: before the first modification, and after the first modifications for every .
输入格式
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 ().
The -th 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.
输出格式
Print integers --- the maximum possible sum of mexes for some valid distribution of all cards into piles after the first modifications to the deck.
5
2 1 3 0 2
6
1 0
1 1
2 4
1 3
2 1
2 1
4
5
7
7
9
7
3
提示
For the initial deck of the example test, one of the best distributions is to assign the cards with values and into one pile, the cards with values into another pile, and the card with value into the third pile. The sum of mexes in this distribution is $\operatorname{mex}(\{0, 2\}) + \operatorname{mex}(\{0, 1, 2, 2, 4\}) + \operatorname{mex}(\{4\}) = 1 + 3 + 0 = 4$.