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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	int m;
	int operacja;
	int bufor;
	int bufor2;
	int wynik = 0;
	cin >> n;
	vector<int>ryba2;
	vector<int> ryba;
	for (int i = 0; i < n; i++)
	{
		cin >> bufor;
		ryba.push_back(bufor);
	}
	sort(ryba.begin(), ryba.end());
	cin >> m;

	for (int i = 0; i < m; i++)
	{
		cin >> operacja;
		switch (operacja)
		{
		case 1:
			ryba2.assign(ryba.begin(), ryba.end());
			cin >> bufor;
			cin >> bufor2;
			wynik = 0;
			while (bufor < bufor2)
			{
				if (ryba2.size() == 0)
				{
					cout << "-1" << endl;
					wynik = -1;
					break;
				}
				if (ryba2[0] >= bufor && bufor < bufor2)
				{
					cout << "-1" << endl;
					wynik = -1;
					break;
				}

				for (int i = ryba2.size() - 1; i >= 0; i--)
				{
					if (bufor > ryba2[i])
					{
						bufor += ryba2[i];
						ryba2.erase(ryba2.begin() + i);
						wynik++;
						break;
					}
				}
			}
			if (wynik >= 0)
				cout << wynik << endl;
			break;
		case 2:
			cin >> bufor;
			if (bufor <= ryba[0])
				ryba.insert(ryba.begin(), bufor);
			else if (bufor >= ryba[ryba.size() - 1])
				ryba.push_back(bufor);
			else
			{
				for (int i = 0; i < ryba.size() - 1; i++)
				{
					if (bufor < ryba[i])
					{
						ryba.insert(ryba.begin() + i, bufor);
						break;
					}

				}
			}
			break;
		case 3:
			cin >> bufor;
			for (int i = 0; i < ryba.size() - 1; i++)
			{
				if (bufor == ryba[i])
					ryba.erase(ryba.begin() + i);
			}
			break;
		}
	}

	return 0;
}