#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
int n, m, k;
int p[N];
int d[N];
int w[N];
map<int, ll> mp[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 2; i <= n; i++) {
cin >> p[i];
}
for (int i = 0; i < m; i++) {
int v;
cin >> v;
cin >> d[v] >> w[v];
}
auto merge = [&](int u, int v) {
if ((int)mp[u].size() < (int)mp[v].size()) swap(mp[u], mp[v]);
for (auto c : mp[v])
mp[u][c.first] += c.second;
};
for (int v = n; v >= 1; v--) {
auto it = mp[v].upper_bound(d[v]);
int s = w[v];
while (it != mp[v].end()) {
if (it->second <= s) {
s -= it->second;
mp[v].erase(it);
} else {
mp[v][it->first] = it->second - s;
break;
}
it = mp[v].upper_bound(d[v]);
}
mp[v][d[v]] += w[v];
merge(p[v], v);
}
ll s = 0;
for (auto c : mp[0])
s += c.second;
cout << s << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
18872 KB |
Output is correct |
2 |
Correct |
22 ms |
8788 KB |
Output is correct |
3 |
Correct |
128 ms |
37968 KB |
Output is correct |
4 |
Correct |
55 ms |
16208 KB |
Output is correct |
5 |
Correct |
91 ms |
20364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
5176 KB |
Output is correct |
3 |
Correct |
2 ms |
5180 KB |
Output is correct |
4 |
Correct |
26 ms |
8212 KB |
Output is correct |
5 |
Correct |
43 ms |
12112 KB |
Output is correct |
6 |
Correct |
44 ms |
8292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
10840 KB |
Output is correct |
2 |
Correct |
40 ms |
11228 KB |
Output is correct |
3 |
Correct |
32 ms |
8812 KB |
Output is correct |
4 |
Correct |
29 ms |
13404 KB |
Output is correct |
5 |
Correct |
24 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
49 ms |
15668 KB |
Output is correct |
11 |
Correct |
42 ms |
14204 KB |
Output is correct |
12 |
Correct |
29 ms |
8788 KB |
Output is correct |
13 |
Correct |
28 ms |
13212 KB |
Output is correct |
14 |
Correct |
25 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6232 KB |
Output is correct |
2 |
Correct |
13 ms |
9560 KB |
Output is correct |
3 |
Correct |
16 ms |
9564 KB |
Output is correct |
4 |
Correct |
18 ms |
9560 KB |
Output is correct |
5 |
Correct |
13 ms |
12636 KB |
Output is correct |
6 |
Correct |
15 ms |
10080 KB |
Output is correct |
7 |
Correct |
11 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
5176 KB |
Output is correct |
12 |
Correct |
2 ms |
5180 KB |
Output is correct |
13 |
Correct |
26 ms |
8212 KB |
Output is correct |
14 |
Correct |
43 ms |
12112 KB |
Output is correct |
15 |
Correct |
44 ms |
8292 KB |
Output is correct |
16 |
Correct |
49 ms |
15668 KB |
Output is correct |
17 |
Correct |
42 ms |
14204 KB |
Output is correct |
18 |
Correct |
29 ms |
8788 KB |
Output is correct |
19 |
Correct |
28 ms |
13212 KB |
Output is correct |
20 |
Correct |
25 ms |
7260 KB |
Output is correct |
21 |
Correct |
14 ms |
9048 KB |
Output is correct |
22 |
Correct |
51 ms |
19288 KB |
Output is correct |
23 |
Correct |
56 ms |
23124 KB |
Output is correct |
24 |
Correct |
90 ms |
32080 KB |
Output is correct |
25 |
Correct |
52 ms |
17560 KB |
Output is correct |
26 |
Correct |
66 ms |
17992 KB |
Output is correct |
27 |
Correct |
60 ms |
11052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
46 ms |
18872 KB |
Output is correct |
11 |
Correct |
22 ms |
8788 KB |
Output is correct |
12 |
Correct |
128 ms |
37968 KB |
Output is correct |
13 |
Correct |
55 ms |
16208 KB |
Output is correct |
14 |
Correct |
91 ms |
20364 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
1 ms |
5176 KB |
Output is correct |
17 |
Correct |
2 ms |
5180 KB |
Output is correct |
18 |
Correct |
26 ms |
8212 KB |
Output is correct |
19 |
Correct |
43 ms |
12112 KB |
Output is correct |
20 |
Correct |
44 ms |
8292 KB |
Output is correct |
21 |
Correct |
32 ms |
10840 KB |
Output is correct |
22 |
Correct |
40 ms |
11228 KB |
Output is correct |
23 |
Correct |
32 ms |
8812 KB |
Output is correct |
24 |
Correct |
29 ms |
13404 KB |
Output is correct |
25 |
Correct |
24 ms |
7508 KB |
Output is correct |
26 |
Correct |
49 ms |
15668 KB |
Output is correct |
27 |
Correct |
42 ms |
14204 KB |
Output is correct |
28 |
Correct |
29 ms |
8788 KB |
Output is correct |
29 |
Correct |
28 ms |
13212 KB |
Output is correct |
30 |
Correct |
25 ms |
7260 KB |
Output is correct |
31 |
Correct |
4 ms |
6232 KB |
Output is correct |
32 |
Correct |
13 ms |
9560 KB |
Output is correct |
33 |
Correct |
16 ms |
9564 KB |
Output is correct |
34 |
Correct |
18 ms |
9560 KB |
Output is correct |
35 |
Correct |
13 ms |
12636 KB |
Output is correct |
36 |
Correct |
15 ms |
10080 KB |
Output is correct |
37 |
Correct |
11 ms |
7512 KB |
Output is correct |
38 |
Correct |
14 ms |
9048 KB |
Output is correct |
39 |
Correct |
51 ms |
19288 KB |
Output is correct |
40 |
Correct |
56 ms |
23124 KB |
Output is correct |
41 |
Correct |
90 ms |
32080 KB |
Output is correct |
42 |
Correct |
52 ms |
17560 KB |
Output is correct |
43 |
Correct |
66 ms |
17992 KB |
Output is correct |
44 |
Correct |
60 ms |
11052 KB |
Output is correct |
45 |
Correct |
16 ms |
9468 KB |
Output is correct |
46 |
Correct |
57 ms |
20572 KB |
Output is correct |
47 |
Correct |
66 ms |
24660 KB |
Output is correct |
48 |
Correct |
112 ms |
35312 KB |
Output is correct |
49 |
Correct |
72 ms |
18340 KB |
Output is correct |
50 |
Correct |
70 ms |
18904 KB |
Output is correct |
51 |
Correct |
62 ms |
11856 KB |
Output is correct |