#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const int N = 100000;
map<int, ll> dp[N];
int p[N], d[N], w[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; ++i) {
cin >> p[i];
--p[i];
}
for (int i = 0; i < m; ++i) {
int v;
cin >> v;
--v;
cin >> d[v] >> w[v];
}
for (int i = n - 1; i > -1; --i) {
if (d[i] != 0) {
ll sum = w[i];
dp[i][d[i]] += w[i];
while (true) {
auto it = dp[i].upper_bound(d[i]);
if (it == dp[i].end()) {
break;
}
if (it->second <= sum) {
sum -= it->second;
dp[i].erase(it);
} else {
it->second -= sum;
break;
}
}
}
if (i > 0) {
if (dp[p[i]].size() < dp[i].size())
swap(dp[i], dp[p[i]]);
for (auto [a, b] : dp[i]) {
dp[p[i]][a] += b;
}
}
}
ll ans = 0;
for (auto [a, b] : dp[0]) {
ans += b;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
15816 KB |
Output is correct |
2 |
Correct |
26 ms |
8608 KB |
Output is correct |
3 |
Correct |
139 ms |
37476 KB |
Output is correct |
4 |
Correct |
89 ms |
15436 KB |
Output is correct |
5 |
Correct |
100 ms |
20428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
34 ms |
8032 KB |
Output is correct |
5 |
Correct |
56 ms |
11984 KB |
Output is correct |
6 |
Correct |
54 ms |
8180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
9880 KB |
Output is correct |
2 |
Correct |
53 ms |
11148 KB |
Output is correct |
3 |
Correct |
35 ms |
9420 KB |
Output is correct |
4 |
Correct |
44 ms |
13472 KB |
Output is correct |
5 |
Correct |
34 ms |
8268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
55 ms |
15052 KB |
Output is correct |
11 |
Correct |
52 ms |
13820 KB |
Output is correct |
12 |
Correct |
33 ms |
8944 KB |
Output is correct |
13 |
Correct |
33 ms |
12820 KB |
Output is correct |
14 |
Correct |
32 ms |
7628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5460 KB |
Output is correct |
2 |
Correct |
11 ms |
6360 KB |
Output is correct |
3 |
Correct |
12 ms |
6356 KB |
Output is correct |
4 |
Correct |
15 ms |
6484 KB |
Output is correct |
5 |
Correct |
10 ms |
6256 KB |
Output is correct |
6 |
Correct |
15 ms |
6228 KB |
Output is correct |
7 |
Correct |
12 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5036 KB |
Output is correct |
13 |
Correct |
34 ms |
8032 KB |
Output is correct |
14 |
Correct |
56 ms |
11984 KB |
Output is correct |
15 |
Correct |
54 ms |
8180 KB |
Output is correct |
16 |
Correct |
55 ms |
15052 KB |
Output is correct |
17 |
Correct |
52 ms |
13820 KB |
Output is correct |
18 |
Correct |
33 ms |
8944 KB |
Output is correct |
19 |
Correct |
33 ms |
12820 KB |
Output is correct |
20 |
Correct |
32 ms |
7628 KB |
Output is correct |
21 |
Correct |
16 ms |
8780 KB |
Output is correct |
22 |
Correct |
55 ms |
16796 KB |
Output is correct |
23 |
Correct |
61 ms |
20540 KB |
Output is correct |
24 |
Correct |
119 ms |
31040 KB |
Output is correct |
25 |
Correct |
70 ms |
17096 KB |
Output is correct |
26 |
Correct |
90 ms |
17172 KB |
Output is correct |
27 |
Correct |
59 ms |
10780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
45 ms |
15816 KB |
Output is correct |
11 |
Correct |
26 ms |
8608 KB |
Output is correct |
12 |
Correct |
139 ms |
37476 KB |
Output is correct |
13 |
Correct |
89 ms |
15436 KB |
Output is correct |
14 |
Correct |
100 ms |
20428 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5036 KB |
Output is correct |
18 |
Correct |
34 ms |
8032 KB |
Output is correct |
19 |
Correct |
56 ms |
11984 KB |
Output is correct |
20 |
Correct |
54 ms |
8180 KB |
Output is correct |
21 |
Correct |
40 ms |
9880 KB |
Output is correct |
22 |
Correct |
53 ms |
11148 KB |
Output is correct |
23 |
Correct |
35 ms |
9420 KB |
Output is correct |
24 |
Correct |
44 ms |
13472 KB |
Output is correct |
25 |
Correct |
34 ms |
8268 KB |
Output is correct |
26 |
Correct |
55 ms |
15052 KB |
Output is correct |
27 |
Correct |
52 ms |
13820 KB |
Output is correct |
28 |
Correct |
33 ms |
8944 KB |
Output is correct |
29 |
Correct |
33 ms |
12820 KB |
Output is correct |
30 |
Correct |
32 ms |
7628 KB |
Output is correct |
31 |
Correct |
5 ms |
5460 KB |
Output is correct |
32 |
Correct |
11 ms |
6360 KB |
Output is correct |
33 |
Correct |
12 ms |
6356 KB |
Output is correct |
34 |
Correct |
15 ms |
6484 KB |
Output is correct |
35 |
Correct |
10 ms |
6256 KB |
Output is correct |
36 |
Correct |
15 ms |
6228 KB |
Output is correct |
37 |
Correct |
12 ms |
6228 KB |
Output is correct |
38 |
Correct |
16 ms |
8780 KB |
Output is correct |
39 |
Correct |
55 ms |
16796 KB |
Output is correct |
40 |
Correct |
61 ms |
20540 KB |
Output is correct |
41 |
Correct |
119 ms |
31040 KB |
Output is correct |
42 |
Correct |
70 ms |
17096 KB |
Output is correct |
43 |
Correct |
90 ms |
17172 KB |
Output is correct |
44 |
Correct |
59 ms |
10780 KB |
Output is correct |
45 |
Correct |
19 ms |
9236 KB |
Output is correct |
46 |
Correct |
61 ms |
17996 KB |
Output is correct |
47 |
Correct |
70 ms |
22180 KB |
Output is correct |
48 |
Correct |
133 ms |
34364 KB |
Output is correct |
49 |
Correct |
79 ms |
17608 KB |
Output is correct |
50 |
Correct |
97 ms |
18148 KB |
Output is correct |
51 |
Correct |
70 ms |
11732 KB |
Output is correct |