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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdio.h>
#include <stdlib.h>

#define xint64 long long

#define MAXN 512000
#define MAXC 102400

int n;
xint64 w[MAXN+MAXC];
int wn[MAXN+MAXC];
int ix[MAXN+MAXC];

typedef struct {
  char c;
  xint64 s;
  xint64 k;
} t_cmd;

t_cmd cmds[MAXC];

typedef struct {
  int l, r;
  xint64 v;
  int n;
  int c;
} t_node; 

int in = 0;
t_node nodes[MAXN+MAXC];

int xmax(int a, int b) {
  return a<b ? b : a;
}

int cmp(const void* i1, const void* i2) {
  int ix1 = *(const int*) i1;
  int ix2 = *(const int*) i2;
  if (w[ix1]<w[ix2])
    return -1;
  else if (w[ix2]<w[ix1])
    return 1;
  else
    return 0;
}

int build(int l, int r) {
  if (l==r) {
    nodes[in].l = -1;
    nodes[in].r = -1;
    nodes[in].v = w[ix[l]];
    nodes[in].n = wn[ix[l]];
    nodes[in].c = 0;
    in++;
    return in-1;
  } if (l+1==r) {
    nodes[in].l = -1;
    nodes[in].r = -1;
    nodes[in].v = w[ix[l]];
    nodes[in].n = wn[ix[l]];
    nodes[in].c = 0;
    in++;
    nodes[in].l = in-1;
    nodes[in].r = -1;
    nodes[in].v = w[ix[r]];
    nodes[in].n = wn[ix[r]];
    nodes[in].c = 0;
    in++;
    return in-1;
  } else {
    int ii = (l+r)/2;
    int iin = in;
    in++;
    nodes[iin].l = build(l, ii-1);
    nodes[iin].r = build(ii+1, r);
    nodes[iin].v = w[ix[ii]];
    nodes[iin].n = wn[ix[ii]];
    nodes[iin].c = 0;
    return iin;
  }
}

int n_count(int r) {
  nodes[r].c = nodes[r].n + (nodes[r].l==-1 ? 0 : n_count(nodes[r].l)) + (nodes[r].r==-1 ? 0 : n_count(nodes[r].r));
  return nodes[r].c;
}

void walk(int r) {
  if (r==-1)
    return ;
  walk(nodes[r].l);
  printf("N %d = %d [%d] c: %d\n", r, nodes[r].v, nodes[r].n, nodes[r].c);
  walk(nodes[r].r);
}

void add(int r, xint64 v) {
  nodes[r].c++;
  if (nodes[r].v==v)
    nodes[r].n++;
  else if (nodes[r].v>v) {
    if (nodes[r].l!=-1)
      add(nodes[r].l, v);
    else
      printf("WTF!\n");
  } else {
    if (nodes[r].r!=-1)
      add(nodes[r].r, v);
    else
      printf("WTF!\n");
  }
}

void remove(int r, xint64 v) {
  nodes[r].c--;
  if (nodes[r].v==v)
    nodes[r].n--;
  else if (nodes[r].v>v) {
    if (nodes[r].l!=-1)
      remove(nodes[r].l, v);
    else
      printf("WTF!\n");
  } else {
    if (nodes[r].r!=-1)
      remove(nodes[r].r, v);
    else
      printf("WTF!\n");
  }
}

int first_smaller(int r, xint64 s) {
  if (nodes[r].c==0)
    return -1;
  else if (nodes[r].v>=s) {
    if (nodes[r].l==-1)
      return -1;
    return first_smaller(nodes[r].l, s);
  } else {
    int ii = (nodes[r].r!=-1 ? first_smaller(nodes[r].r, s) : -1);
    if (ii!=-1)
      return ii;
    if (nodes[r].n>0)
      return r;
    if (nodes[r].l==-1)
      return -1;
    return first_smaller(nodes[r].l, s);
  }
}

void do_check(int r, xint64 s, xint64 k) {
  int tn = 0;
  while (s<k) {
    int ii = first_smaller(r, s);
  //  printf("FS [%lld, %lld] => %lld\n", s, k, (ii==-1) ? -1 : nodes[ii].v);
    if (ii==-1)
      break ;
    s += nodes[ii].v;
    nodes[ii].n--;
    w[tn] = nodes[ii].v;
    tn++;
  }
  for (int i=0;i<tn;i++)
    add(r, w[i]);
  if (s>=k)
    printf("%d\n", tn);
  else
    printf("-1\n");
}

int main() {
  scanf("%d", &n);
  for (int i=0;i<n;i++)
    scanf("%lld", &w[i]);
  int q;
  scanf("%d", &q);
  for (int i=0;i<q;i++) {
    scanf("%d", &cmds[i].c);
    if (cmds[i].c==1)
      scanf("%lld%lld", &cmds[i].s, &cmds[i].k);
    else if (cmds[i].c==2)
      scanf("%lld", &cmds[i].s);
    else
      scanf("%lld", &cmds[i].s);
  }
  int iw = n;
  for (int i=0;i<q;i++)
    if (cmds[i].c==2)  
      w[iw++] = cmds[i].s;
  for (int i=0;i<iw;i++)
    ix[i] = i;
  qsort(ix, iw, sizeof(int), cmp);
  int j = 0;
  wn[ix[0]] = (ix[0]<n ? 1 : 0);
  for (int i=1;i<iw;i++)
    if (w[ix[i]]==w[ix[i-1]])
      wn[ix[j]] += (ix[i]<n ? 1 : 0);
    else {
      j++;
      w[ix[j]] = w[ix[i]];
      wn[ix[j]] = (ix[i]<n ? 1 : 0);
    }
  int r = build(0, j);
  n_count(r);
  //walk(r);printf("\n");
  n = iw;
  for (int i=0;i<q;i++) {
    if (cmds[i].c==1) {
      do_check(r, cmds[i].s, cmds[i].k);
    } else if (cmds[i].c==2) {
      add(r, cmds[i].s);
      //walk(r);printf("\n");
    } else {
      remove(r, cmds[i].s);
      //walk(r);printf("\n");
    }
  }
  
  return 0;
}