34532023-02-28 09:42:57AncsaSzemetessorcpp11Accepted 100/100284ms31748 KiB
// greedy_linear solution O(N+K)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>

/*
5 11
66 73 68 79 78
2 50
0 1
0 12
2 20
0 23
1 44
1 30
4 12
4 3
4 63
0 10

kimenet: 158
*/


using namespace std;

constexpr int INF = 1e9;

int N, K;
vector<int> C, T, Q;

int main() {
    cin >> N >> K;
    C.resize(N);
    for (auto &x : C)
		cin >> x;

    T.resize(K);
    Q.resize(K);
    for (int i = 0; i < K; i++)
        cin >> T[i] >> Q[i];

	/* for each bin, calculate the first moment you can empty */
	vector <vector<int>>empty_times(N);
	vector <int> bins(N, 0);
	long long ans = 0;

	for (int i = 0; i < K; ++i) {
		auto &idx = T[i];
		auto &qty = Q[i];

		if (bins[idx] == 0 || bins[idx] + qty > C[idx]) {
			/* add index to empty times */
			if (bins[idx] != 0) ans += C[idx] - bins[idx];
			empty_times[idx].push_back(i);
			bins[idx] = qty;
		} else {
			/* substitute the previous index */
			empty_times[idx].back() = i;
			bins[idx] += qty;
		}
	}

	/* add last index */
	for (int i = 0; i < N; ++i) {
		if (empty_times[i].size() > 0) {
			ans += C[i] - bins[i];
		}
	}

	cout << ans << endl;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1808 KiB
2Accepted3ms2060 KiB
subtask217/17
3Accepted3ms2312 KiB
4Accepted3ms2512 KiB
5Accepted3ms2596 KiB
6Accepted3ms2812 KiB
7Accepted3ms2980 KiB
8Accepted3ms3192 KiB
9Accepted3ms3404 KiB
10Accepted3ms3512 KiB
11Accepted3ms3516 KiB
subtask325/25
12Accepted123ms8084 KiB
13Accepted123ms8140 KiB
14Accepted136ms8320 KiB
15Accepted143ms8632 KiB
16Accepted157ms9780 KiB
17Accepted216ms19788 KiB
18Accepted284ms31620 KiB
19Accepted284ms31620 KiB
20Accepted284ms31748 KiB
subtask420/20
21Accepted128ms7844 KiB
22Accepted153ms12828 KiB
23Accepted128ms8364 KiB
24Accepted152ms10420 KiB
25Accepted136ms8336 KiB
26Accepted208ms18416 KiB
27Accepted270ms26740 KiB
28Accepted270ms27360 KiB
29Accepted270ms26988 KiB
30Accepted237ms26944 KiB
subtask538/38
31Accepted3ms4348 KiB
32Accepted3ms4428 KiB
33Accepted3ms4552 KiB
34Accepted3ms4572 KiB
35Accepted14ms5380 KiB
36Accepted143ms9440 KiB
37Accepted266ms23624 KiB
38Accepted263ms23680 KiB
39Accepted273ms24772 KiB
40Accepted275ms26996 KiB
41Accepted279ms28256 KiB
42Accepted277ms27160 KiB