#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
int N, M, K; cin >> N >> M >> K;
vector<vector<int>> adj(N);
for (int i = 1; i < N; i++) {
int p; cin >> p; p--;
adj[p].pb(i);
}
vector<vector<vector<int>>> fruits(N);
for (int i = 0; i < M; i++) {
int u, t, k; cin >> u >> t >> k; u--;
fruits[u].pb({t, k});
}
vector<map<int, ll>> mp(N);
for (int u = N - 1; u >= 0; u--) {
for (int v : adj[u]) {
if (sz(mp[v]) > sz(mp[u])) {
swap(mp[v], mp[u]);
}
}
for (int v : adj[u]) {
for (auto &p : mp[v]) {
mp[u][p.first] += p.second;
}
}
for (auto &fruit : fruits[u]) {
int t = fruit[0], k = fruit[1];
mp[u][t] += k;
ll m = k;
while (m && prev(mp[u].end())->first >= t + 1) {
int f = mp[u].lower_bound(t + 1)->first;
ll r = min(mp[u][f], m);
mp[u][f] -= r;
m -= r;
if (!mp[u][f]) mp[u].erase(f);
}
}
}
ll res = 0;
for (auto &p : mp[0]) {
res += p.second;
}
cout << res << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
21604 KB |
Output is correct |
2 |
Correct |
43 ms |
17340 KB |
Output is correct |
3 |
Correct |
103 ms |
42832 KB |
Output is correct |
4 |
Correct |
74 ms |
27092 KB |
Output is correct |
5 |
Correct |
83 ms |
27728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
44 ms |
21164 KB |
Output is correct |
5 |
Correct |
49 ms |
25128 KB |
Output is correct |
6 |
Correct |
67 ms |
21000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
22732 KB |
Output is correct |
2 |
Correct |
64 ms |
22076 KB |
Output is correct |
3 |
Correct |
54 ms |
21072 KB |
Output is correct |
4 |
Correct |
58 ms |
23072 KB |
Output is correct |
5 |
Correct |
37 ms |
20576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
69 ms |
24912 KB |
Output is correct |
11 |
Correct |
87 ms |
23892 KB |
Output is correct |
12 |
Correct |
54 ms |
20560 KB |
Output is correct |
13 |
Correct |
54 ms |
23076 KB |
Output is correct |
14 |
Correct |
40 ms |
20048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2904 KB |
Output is correct |
2 |
Correct |
19 ms |
12116 KB |
Output is correct |
3 |
Correct |
18 ms |
12120 KB |
Output is correct |
4 |
Correct |
17 ms |
12124 KB |
Output is correct |
5 |
Correct |
9 ms |
10584 KB |
Output is correct |
6 |
Correct |
16 ms |
11868 KB |
Output is correct |
7 |
Correct |
19 ms |
13000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
44 ms |
21164 KB |
Output is correct |
14 |
Correct |
49 ms |
25128 KB |
Output is correct |
15 |
Correct |
67 ms |
21000 KB |
Output is correct |
16 |
Correct |
69 ms |
24912 KB |
Output is correct |
17 |
Correct |
87 ms |
23892 KB |
Output is correct |
18 |
Correct |
54 ms |
20560 KB |
Output is correct |
19 |
Correct |
54 ms |
23076 KB |
Output is correct |
20 |
Correct |
40 ms |
20048 KB |
Output is correct |
21 |
Correct |
15 ms |
6492 KB |
Output is correct |
22 |
Correct |
57 ms |
23120 KB |
Output is correct |
23 |
Correct |
77 ms |
26376 KB |
Output is correct |
24 |
Correct |
106 ms |
37100 KB |
Output is correct |
25 |
Correct |
70 ms |
26460 KB |
Output is correct |
26 |
Correct |
111 ms |
25936 KB |
Output is correct |
27 |
Correct |
95 ms |
22356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
43 ms |
21604 KB |
Output is correct |
11 |
Correct |
43 ms |
17340 KB |
Output is correct |
12 |
Correct |
103 ms |
42832 KB |
Output is correct |
13 |
Correct |
74 ms |
27092 KB |
Output is correct |
14 |
Correct |
83 ms |
27728 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
44 ms |
21164 KB |
Output is correct |
19 |
Correct |
49 ms |
25128 KB |
Output is correct |
20 |
Correct |
67 ms |
21000 KB |
Output is correct |
21 |
Correct |
71 ms |
22732 KB |
Output is correct |
22 |
Correct |
64 ms |
22076 KB |
Output is correct |
23 |
Correct |
54 ms |
21072 KB |
Output is correct |
24 |
Correct |
58 ms |
23072 KB |
Output is correct |
25 |
Correct |
37 ms |
20576 KB |
Output is correct |
26 |
Correct |
69 ms |
24912 KB |
Output is correct |
27 |
Correct |
87 ms |
23892 KB |
Output is correct |
28 |
Correct |
54 ms |
20560 KB |
Output is correct |
29 |
Correct |
54 ms |
23076 KB |
Output is correct |
30 |
Correct |
40 ms |
20048 KB |
Output is correct |
31 |
Correct |
4 ms |
2904 KB |
Output is correct |
32 |
Correct |
19 ms |
12116 KB |
Output is correct |
33 |
Correct |
18 ms |
12120 KB |
Output is correct |
34 |
Correct |
17 ms |
12124 KB |
Output is correct |
35 |
Correct |
9 ms |
10584 KB |
Output is correct |
36 |
Correct |
16 ms |
11868 KB |
Output is correct |
37 |
Correct |
19 ms |
13000 KB |
Output is correct |
38 |
Correct |
15 ms |
6492 KB |
Output is correct |
39 |
Correct |
57 ms |
23120 KB |
Output is correct |
40 |
Correct |
77 ms |
26376 KB |
Output is correct |
41 |
Correct |
106 ms |
37100 KB |
Output is correct |
42 |
Correct |
70 ms |
26460 KB |
Output is correct |
43 |
Correct |
111 ms |
25936 KB |
Output is correct |
44 |
Correct |
95 ms |
22356 KB |
Output is correct |
45 |
Correct |
16 ms |
7000 KB |
Output is correct |
46 |
Correct |
65 ms |
24452 KB |
Output is correct |
47 |
Correct |
70 ms |
27596 KB |
Output is correct |
48 |
Correct |
118 ms |
40204 KB |
Output is correct |
49 |
Correct |
75 ms |
27092 KB |
Output is correct |
50 |
Correct |
139 ms |
27036 KB |
Output is correct |
51 |
Correct |
111 ms |
23328 KB |
Output is correct |