-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathkillingZombies.cpp
More file actions
executable file
·76 lines (66 loc) · 1.95 KB
/
killingZombies.cpp
File metadata and controls
executable file
·76 lines (66 loc) · 1.95 KB
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
/*
Nutanix IISc 2018
https://www.codechef.com/problems/CLKLZM
O(n + m log m)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
// your code goes here
ios_base::sync_with_stdio(false); cin.tie(NULL);
int cases; cin >> cases;
while (cases--) {
int n, m; cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<pair<pair<int,int>,int>> b(m);
for (int i = 0; i < m; i++) {
int l, r, k; cin >> l >> r >> k;
b[i] = {{l - 1, r - 1}, k};
}
sort(b.begin(), b.end(), [](pair<pair<int,int>,int> l, pair<pair<int,int>,int> r) {
if (l.first.first == r.first.first) return l.first.second > r.first.second;
return l.first.first < r.first.first;
});
vector<int> c(n);
int j = 0, ext = 0, ans = 0, fl = 0;
priority_queue<pair<int,int>> q;
for (int i = 0; i < n; i++) {
a[i] -= ext;
while (j < m and b[j].first.first <= i) {
q.push({b[j].first.second, b[j].second});
j++;
}
int r, k;
while (a[i] > 0 and not q.empty()) {
tie(r, k) = q.top(); q.pop();
if (r < i) continue;
if (k <= a[i]) {
ans += k;
ext += k;
c[r] += k;
a[i] -= k;
}
else {
ans += a[i];
ext += a[i];
c[r] += a[i];
q.push({r, k - a[i]});
a[i] = 0;
}
}
if (a[i] > 0) {
fl = 1;
break;
}
ext -= c[i];
}
if (fl) cout << "NO";
else cout << "YES " << ans;
cout << '\n';
}
return 0;
}