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
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;

struct Node{
    long long data;
    Node *left;
    Node *right;
};
Node *rt;
vector<long long> temp;
long long sum, biggest = 0;

Node *MaxNode(Node *root){
    while(root->right != NULL) root = root->right;
    return root;
}
long long maks = 0;
Node *GetNewNode(long long data){
    Node *newNode = new Node();
    newNode->data = data;
    newNode->left = NULL;
    newNode->left = NULL;
    return newNode;
}
Node* Insert(Node *root,long long data) {
	if(root == NULL) {
		root = new Node();
		root->data = data;
		root->left = NULL;
		root->right = NULL;
	}
	else if(data <= root->data)
		root->left = Insert(root->left,data);
	else
		root->right = Insert(root->right,data);
	return root;
}

void Inorder(Node *root) {
	if(root == NULL) return;

	Inorder(root->left);
	cout<<root->data<<" ";
	Inorder(root->right);
}

Node* FindMin(Node* root)
{
 while(root->left != NULL) root = root->left;
 return root;
}

Node *Search(Node *root, long long data){
    if(data >= root->data) maks = maks>root->data?maks:root->data;
    if(root == NULL) return root;
    if(data == root->data) return root;
    else if(data < root->data) {
        if(root->left == NULL) return root;
        return Search(root->left, data);
    }
    else {
        if(root->right == NULL) return root;
        return Search(root->right, data);
    }
}
struct Node* Delete(struct Node *root, long long data) {
	if(root == NULL) return root;
	else if(data < root->data) root->left = Delete(root->left,data);
	else if (data > root->data) root->right = Delete(root->right,data);
	else {
		if(root->left == NULL && root->right == NULL) {
			delete root;
			root = NULL;
		}
		else if(root->left == NULL) {
			struct Node *temp = root;
			root = root->right;
			delete temp;
		}
		else if(root->right == NULL) {
			struct Node *temp = root;
			root = root->left;
			delete temp;
		}
		else {
			struct Node *temp = FindMin(root->right);
			root->data = temp->data;
			root->right = Delete(root->right,temp->data);
		}
	}
	return root;
}
bool done = false;
long long cc = 0, chc = 0, obc = 0;
void bigFish(Node *root){
	if(root == NULL) return;

    bigFish(root->right);
    if(done) return;
    cc++;
    obc += root->data;
    if(obc >= chc) {done = true; return;}
    bigFish(root->left);
}

vector<long long> ryb;

long long Atak(long long w, long long cel){
    if(w+sum < cel) return -1;
    long long cont = 0;
    bool brk = false;
    while(w < cel){
        maks = 0;
        if(rt == NULL) {brk = true; break;}
        if(w > biggest){
            temp.resize(0);
            cc = 0; chc = cel; obc = w;
            done = false;
            bigFish(rt);
            cont += cc;
            break;
        }
        Node *pt = Search(rt, w-1);
        if(pt == NULL || maks==0) {brk = true; break;}
        if(w > maks){
            w += maks;
            cont++;
            ryb.push_back(maks);
            rt = Delete(rt, maks);
        }else {brk = true; break;}
    }
    if(rt == NULL){if(ryb.size()>0){rt = GetNewNode(ryb.back()); ryb.pop_back();}}
    while(ryb.size()>0) {rt = Insert(rt, ryb.back()); ryb.pop_back();}
    if(brk) return -1;
    return cont;
}
vector<long long> tree;
void buildTree(int dol, int gora){
    //cout<<dol<<" "<<gora<<endl;
    if(gora - dol == 1){
        rt = Insert(rt, tree[dol]);
        rt = Insert(rt, tree[gora]);
        return;
    }
    rt = Insert(rt, tree[(dol+gora+1)/2]);
    if(gora == dol) return;
    buildTree(dol, ((dol+gora+1)/2) -1);
    buildTree(((dol+gora+1)/2) +1, gora);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(nullptr); cin.tie(nullptr);

    long long n;
    cin>>n;
    long long w;
    cin>>w;
    temp.resize(0);
    sum = 0;
    tree.resize(0);
    tree.push_back(w);
    biggest = w;
    sum += w;

    for(long long i = 1; i < n; i++){
        cin>>w;
        biggest = biggest>w?biggest:w;
        sum+=w;
        tree.push_back(w);
    }
    sort(tree.begin(), tree.end());
    rt = GetNewNode(tree[tree.size()/2]);
    long long rttr = tree[tree.size()/2];
    buildTree(0, tree.size()-1);
    rt = Delete(rt, rttr);
    long long q;
    cin>>q;
    for(long long i = 0; i < q; i++){
        long long a; long long k, s;
        cin>>a>>k;
        if(a == 1) {
            cin>>s;
            cout<<Atak(k, s)<<endl;
        }
        else if(a == 2){
            sum += k;
            biggest = biggest>k?biggest:k;
            if(rt == NULL){
                rt = GetNewNode(k);
            }else{
                rt = Insert(rt, k);
            }
        }
        else {
            if(rt->data == biggest){
                rt = Delete(rt, k);
                sum -= k;
                if(rt != NULL) biggest = MaxNode(rt)->data;
                else biggest = 0;
            }else{
                rt = Delete(rt, k);
                sum -= k;
            }
        }
    }
}