-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1271D.cpp
More file actions
executable file
·71 lines (57 loc) · 1.34 KB
/
1271D.cpp
File metadata and controls
executable file
·71 lines (57 loc) · 1.34 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
// Problem Code: 1271D
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int portals(int n, int k, vector<int>& a, vector<int>& b, vector<int>& c, vector<int>& far) {
int imp = 0;
vector< vector<int> > adj(n, vector<int>());
priority_queue<int, vector<int>, greater<int>> pq;
// Initialize adjacency list for portals
for (int i = 0; i < n; i++)
if (far[i] != -1)
adj[far[i]].push_back(i);
for (int i = 0; i < n; i++) {
// Undo defended castles to get warriors from min heap
while (k < a[i] && !pq.empty()) {
pq.pop();
k++;
}
// We cannot win the game
if (k < a[i])
return -1;
// Capture current castle
k += b[i];
// Defend castle because it has no incoming portal from future castles
if (far[i] == -1) {
pq.push(c[i]);
k--;
}
// Defend previous castles using portals of current castle
for (int v: adj[i]) {
pq.push(c[v]);
k--;
}
}
// Sum importance values of all defended castles
while (!pq.empty()) {
if (k >= 0)
imp += pq.top();
pq.pop();
k++;
}
return imp;
}
int main() {
int n, m, k, u, v;
cin >> n >> m >> k;
vector<int> a(n), b(n), c(n), far(n, -1);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i] >> c[i];
for (int i = 0; i < m; i++) {
cin >> u >> v;
far[v - 1] = max(u - 1, far[v - 1]);
}
cout << portals(n, k, a, b, c, far);
return 0;
}