1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdio>
#include <utility>
#include <algorithm>

#define REP(a, n) for (int a = 0; a < (n); ++a)
#define FOR(a, b, c) for (int a = (b); a <= (c); ++a)
#define FORD(a, b, c) for (int a = (b); a >= (c); --a)

#define INFI 1000000000
#define INFL (INFI*(LL)INFI+1)

typedef long long LL;

using namespace std;

//////////////////////

#define SIZE (1<<19)

int cur_ile_ryb[2 * SIZE], orig_ile_ryb[2 * SIZE];
LL cur_waga_ryb[2 * SIZE], orig_waga_ryb[2 * SIZE];

int jedz(LL &ile_kg, int kon, int p = 1, int s = SIZE) {
    if (s == 1) {
        if (!cur_ile_ryb[p])
            return 0;
        LL jedna = cur_waga_ryb[p] / cur_ile_ryb[p];
        int jem = min((ile_kg + jedna - 1) / jedna, (LL)cur_ile_ryb[p]);
        ile_kg -= jem * jedna;
        cur_ile_ryb[p] -= jem;
        cur_waga_ryb[p] -= jem * jedna;
        return jem;
    }
    if (kon == s && ile_kg >= cur_waga_ryb[p]) {
        int jem = cur_ile_ryb[p];
        ile_kg -= cur_waga_ryb[p];
        cur_ile_ryb[p] = 0;
        cur_waga_ryb[p] = 0;
        return jem;
    }
    s /= 2;
    int jem = 0;
    if (kon > s)
        jem = jedz(ile_kg, kon - s, 2 * p + 1, s);
    if (ile_kg > 0)
        jem += jedz(ile_kg, min(kon, s), 2 * p, s);
    cur_ile_ryb[p] = cur_ile_ryb[2 * p] + cur_ile_ryb[2 * p + 1];
    cur_waga_ryb[p] = cur_waga_ryb[2 * p] + cur_waga_ryb[2 * p + 1];
    return jem;
}

void czysc_cur(int p = 1) {
    if (cur_ile_ryb[p] != orig_ile_ryb[p]) {
        cur_ile_ryb[p] = orig_ile_ryb[p];
        cur_waga_ryb[p] = orig_waga_ryb[p];
        if (p < SIZE) {
            czysc_cur(2 * p);
            czysc_cur(2 * p + 1);
        }
    }
}

int nastepna_zywa(int x) {
    int p = 1, s = SIZE;
    while (p < SIZE) {
        p *= 2;
        s /= 2;
        if (x >= s || !cur_ile_ryb[p]) {
            ++p;
            x -= s;
        }
    }
    return p - SIZE;
}

int N, Q, ile_wag;
LL poczatkowe[300001], wagi[400002];
pair<int, pair<LL, LL>> zap[100000];

inline int waga2nr(LL x) {
    return lower_bound(wagi, wagi + ile_wag, x) - wagi;
}

int licz(LL waga, LL cel) {
    int res = 0;
    while (waga < cel) {
        int x = waga2nr(waga);
        LL ile_kg = min(cel, wagi[nastepna_zywa(x)] + 1) - waga;
        LL orig_ile_kg = ile_kg;
        int zj_ile = jedz(ile_kg, x);
        if (ile_kg > 0)
            return -1;
        res += zj_ile;
        waga += orig_ile_kg - ile_kg;
    }
    return res;
}

int main() {
    scanf("%d", &N);
    REP(a, N) {
        scanf("%lld", &poczatkowe[a]);
        wagi[ile_wag++] = poczatkowe[a];
    }
    wagi[ile_wag++] = poczatkowe[N++] = INFL;
    wagi[ile_wag++] = 0;
    scanf("%d", &Q);
    REP(q, Q) {
        scanf("%d%lld", &zap[q].first, &zap[q].second.first);
        if (zap[q].first == 1)
            scanf("%lld", &zap[q].second.second);
        else
            wagi[ile_wag++] = zap[q].second.first;
    }
    sort(wagi, wagi + ile_wag);
    ile_wag = unique(wagi, wagi + ile_wag) - wagi;
    
    // wstaw początkowe
    REP(a, N) {
        int x = waga2nr(poczatkowe[a]);
        cur_ile_ryb[SIZE + x] = ++orig_ile_ryb[SIZE + x];
        cur_waga_ryb[SIZE + x] = orig_waga_ryb[SIZE + x] += poczatkowe[a];
    }
    FORD(p, SIZE - 1, 1) {
        cur_ile_ryb[p] = orig_ile_ryb[p] = orig_ile_ryb[2 * p] + orig_ile_ryb[2 * p + 1];
        cur_waga_ryb[p] = orig_waga_ryb[p] = orig_waga_ryb[2 * p] + orig_waga_ryb[2 * p + 1];
    }
    
    // zapytania
    REP(q, Q) {
        LL waga = zap[q].second.first;
        if (zap[q].first > 1) {
            int p = SIZE + waga2nr(waga);
            int ch = (zap[q].first == 2) ? 1 : -1;
            waga *= ch;
            while (p) {
                cur_ile_ryb[p] = orig_ile_ryb[p] += ch;
                cur_waga_ryb[p] = orig_waga_ryb[p] += waga;
                p /= 2;
            }
        }
        else {
            printf("%d\n", licz(waga, zap[q].second.second));
            czysc_cur();
        }
    }    
}