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

int n, q, x;
long long w, s, k, suma, lic=1000000000000000007;
multiset <long long> ms;
multiset <long long> ::iterator it, it1, it2;
vector <long long> v;

void atak (long long s, long long k)
{
    v.clear();
    int czy = 0, czy1 = 0;
    int ile = 0;
    it = ms.lower_bound(s);
    it1 = it;
    it1--;
    while (s<k && czy1==0)
    {
        if (czy == 1 && s<=*it)
        {
            printf ("-1\n");
            czy1 = 1;
            break;
        }
        if (s>*it)
        {
            it = ms.lower_bound(s);
            it1 = it;
            if (it1 == ms.begin())
                czy=1;
            else
            {
                czy=0;
                it1--;
            }
        }
        else
        {
            ile++;
            s+=*it1;
            it2 = it1;
            if (it1 == ms.begin())
                czy=1;
            else
            {
                czy=0;
                it1--;
            }
            v.push_back(*it2);
            ms.erase(it2);
        }
    }
    if (czy1 == 0)
        printf ("%d\n", ile);

    for (int i=0; i<v.size(); i++)
        ms.insert(v[i]);
}

int main ()
{
    scanf ("%d", &n);
    for (int i=0; i<n; i++)
    {
        scanf ("%lld", &w);
        ms.insert(w);
        suma += w;
    }
    ms.insert(lic);
    scanf ("%d", &q);
    for (int i=0; i<q; i++)
    {
        scanf ("%d", &x);
        switch (x)
        {
            case 1:
            {
                scanf ("%lld%lld", &s, &k);
                if (s+suma < k)
                    printf ("-1\n");
                else
                    atak(s, k);
                break;
            }
            case 2:
            {
                scanf ("%lld", &w);
                ms.insert(w);
                suma += w;
                break;
            }
            case 3:
            {
                scanf ("%lld", &w);
                suma -= w;
                it = ms.find(w);
                ms.erase(it);
                break;
            }
        }
    }
    return 0;
}