Under construction

Still being built. Things may change

Mex and Cards

Time limit3sMemory limit1024 MB

Summary
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 00 to n−1n-1 written on it. Initially the deck contains aia_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.

A sequence of qq 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 ii modifications for every i=1,2,…,qi = 1, 2, \ldots, q.

Input

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

The second line contains nn integers a0,a1,…,an−1a_0, a_1, \ldots, a_{n-1}, the number of cards with value 0,1,…,n−10, 1, \ldots, n-1 in the deck initially (0≤ai≤1060 \le a_i \le 10^6).

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

Each of the next qq lines contains two integers pip_i and viv_i, describing the ii-th modification (1≤pi≤21 \le p_i \le 2, 0≤vi<n0 \le v_i < n). If pi=1p_i = 1, a new card with value viv_i is added to the deck. If pi=2p_i = 2, a card with value viv_i is removed from the deck.

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

Output

Print q+1q+1 integers. The jj-th integer is the maximum possible sum of mexes for some valid distribution of all cards into piles after the first j−1j-1 modifications of the deck (j=1,2,…,q+1j = 1, 2, \ldots, q+1).

Hint

In the example test, one of the best distributions for the initial deck is to put 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 a third pile. The sum of mexes for 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.

Examples1

  1. Example 1

    Input
    5
    2 1 3 0 2
    6
    1 0
    1 1
    2 4
    1 3
    2 1
    2 1
    
    Expected output
    4
    5
    7
    7
    9
    7
    3