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
#include <bits/stdc++.h>
using namespace std;

const int base=(1<<19);
const long long inf=1e18+2137;

multiset<long long> st;
vector<long long> vals;
int bad[base*2];
long long sum[base*2];
int cnt[base*2];
long long sum_lowest[base*2];
long long def[base];
int cnt_lowest[base*2];
pair<int,pair<long long,long long>> query[base];

long long get_sum(int l,int r,int cl,int cr,int u)
{
    if(bad[u]||cl>r||cr<l)
        return 0;
    if(cl>=l&&cr<=r)
        return sum[u];
    return get_sum(l,r,cl,(cl+cr)/2,u*2)+get_sum(l,r,(cl+cr)/2+1,cr,u*2+1);
}

long long get_cnt(int l,int r,int cl,int cr,int u)
{
    if(bad[u]||cl>r||cr<l)
        return 0;
    if(cl>=l&&cr<=r)
        return cnt[u];
    return get_cnt(l,r,cl,(cl+cr)/2,u*2)+get_cnt(l,r,(cl+cr)/2+1,cr,u*2+1);
}

void add_bad(int l,int r,int cl,int cr,int u,int v)
{
    if(cl>r||cr<l)
        return;
    if(cl>=l&&cr<=r)
        bad[u]+=v;
    else
        add_bad(l,r,cl,(cl+cr)/2,u*2,v),add_bad(l,r,(cl+cr)/2+1,cr,u*2+1,v);
    if(bad[u])
        sum[u]=0,cnt[u]=0;
    else
    {
        if(u>=base)
            sum[u]=sum_lowest[u],cnt[u]=cnt_lowest[u];
        else
            sum[u]=sum[u*2]+sum[u*2+1],cnt[u]=cnt[u*2]+cnt[u*2+1];
    }
}

void add_val(int pos,long long v,int occ)
{
    pos+=base;
    sum_lowest[pos]+=v;
    cnt_lowest[pos]+=occ;
    while(pos&&!bad[pos])
    {
        sum[pos]+=v;
        cnt[pos]+=occ;
        pos/=2;
    }
}

pair<int,pair<int,long long>> get_pos_greater(long long x)
{
    int pos=1;
    int tot_cnt=0;
    bool under_bad=false;
    long long tot_sum=0,rsum,lsum;
    while(pos<base)
    {
        if(bad[pos])
            under_bad=true;
        if(under_bad)
            pos=pos*2+1;
        else
        {
            if(bad[pos*2])
                lsum=0;
            else
                lsum=sum[pos*2];
            if(bad[pos*2+1])
                rsum=0;
            else
                rsum=sum[pos*2+1];
            if(rsum>=x)
                pos=pos*2+1;
            else
            {
                x-=rsum;
                if(!under_bad&&!bad[pos*2+1])
                    tot_cnt+=cnt[pos*2+1],tot_sum+=sum[pos*2+1];
                pos*=2;
            }
        }
    }
    if(!under_bad&&!bad[pos])
        tot_cnt+=cnt[pos],tot_sum+=sum[pos];
    pos-=base;
    return make_pair(pos,make_pair(tot_cnt,tot_sum));
}

int get_tree_pos(long long x)
{
    return lower_bound(vals.begin(),vals.end(),x)-vals.begin();
}

long long solve_query(long long s,long long k)
{
    if(s>=k)
        return 0;
    int s_pos=get_tree_pos(s);
    long long curr_res;
    long long right_sum=get_sum(s_pos,base-1,0,base-1,1);
    long long next_value=min(k,(*st.lower_bound(s)+1));
    if(sum[1]-right_sum>=next_value-s)
    {
        auto xd=get_pos_greater(right_sum+next_value-s);
        xd.second.first-=get_cnt(s_pos,base-1,0,base-1,1);
        xd.second.second-=right_sum;
        add_bad(xd.first,s_pos-1,0,base-1,1,1);
        curr_res=solve_query(s+xd.second.second,k)+xd.second.first;
        add_bad(xd.first,s_pos-1,0,base-1,1,-1);
        return curr_res;
    }
    else
        return -inf;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,q,t;
    long long val,s,k,w,res;
    cin>>n;
    vals.emplace_back(inf);
    vals.emplace_back(-inf);
    vals.emplace_back(inf);
    vals.emplace_back(-inf);
    st.insert(inf);
    st.insert(-inf);
    st.insert(inf);
    st.insert(-inf);
    for(int i=0;i<n;i++)
    {
        cin>>def[i];
        vals.emplace_back(def[i]);
    }
    cin>>q;
    for(int i=0;i<q;i++)
    {
        cin>>t;
        query[i].first=t;
        if(t==1)
        {
            cin>>s>>k;
            query[i].second=make_pair(s,k);
        }
        else
        {
            cin>>w;
            vals.emplace_back(w);
            query[i].second.first=w;
        }
    }
    sort(vals.begin(),vals.end());
    for(int i=0;i<n;i++)
    {
        add_val(get_tree_pos(def[i])+st.count(def[i]),def[i],1);
        st.insert(def[i]);
    }
    for(int i=0;i<q;i++)
    {
        if(query[i].first==1)
        {
            res=solve_query(query[i].second.first,query[i].second.second);
            if(res<0)
                res=-1;
            cout<<res<<'\n';
        }
        else
        if(query[i].first==2)
        {
            add_val(get_tree_pos(query[i].second.first)+st.count(query[i].second.first),query[i].second.first,1);
            st.insert(query[i].second.first);
        }
        else
        {
            st.erase(st.find(query[i].second.first));
            add_val(get_tree_pos(query[i].second.first)+st.count(query[i].second.first),-query[i].second.first,-1);
        }
    }
}