Mex and Cards

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Mike enjoys playing with cards. Each card in his deck has a single integer value from 00 to n1n-1 written on it. Initially the deck contains a_ia\_i cards with value ii.

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, mex(4,1,4,12,0,7,0,0,5)=2\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 qq 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 ii modifications for every i=1,2,,qi = 1, 2, \ldots, q.

입력

The first line contains a single integer nn --- the range of card values (1n21051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a_0,a_1,,a_n1a\_0, a\_1, \ldots, a\_{n-1} --- the number of cards with value 0,1,,n10, 1, \ldots, n-1 in the deck initially (0a_i1060 \le a\_i \le 10^6).

The third line contains a single integer qq --- the number of deck modifications (0q21050 \le q \le 2 \cdot 10^5).

The ii-th of the next qq lines contains two integers p_ip\_i and v_iv\_i, describing the ii-th modification (1p_i21 \le p\_i \le 2; 0v_i<n0 \le v\_i < n). If p_i=1p\_i = 1, a new card with value v_iv\_i is added to the deck. If p_i=2p\_i = 2, a card with value v_iv\_i is removed from the deck.

It is guaranteed that if p_i=2p\_i = 2, then the deck contains at least one card with value v_iv\_i right before the ii-th modification.

출력

Print q+1q+1 integers --- the maximum possible sum of mexes for some valid distribution of all cards into piles after the first 0,1,,q0, 1, \ldots, q modifications to the deck.

힌트

For the initial deck of the example test, one of the best distributions is to assign the cards with values 00 and 22 into one pile, the cards with values 0,1,2,2,40, 1, 2, 2, 4 into another pile, and the card with value 44 into the third pile. The sum of mexes in this distribution is mex(0,2)+mex(0,1,2,2,4)+mex(4)=1+3+0=4\operatorname{mex}(\\{0, 2\\}) + \operatorname{mex}(\\{0, 1, 2, 2, 4\\}) + \operatorname{mex}(\\{4\\}) = 1 + 3 + 0 = 4.