#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int n, m, k, ans;
vector<int> adj[100005];
vector<ii> vec[100005];
map<int, int> M[100005];
void dp(int u) {
for (auto v : adj[u]) {
dp(v);
if (M[u].size() < M[v].size()) swap(M[u], M[v]);
for (auto i : M[v]) M[u][i.first] += i.second;
}
reverse(vec[u].begin(), vec[u].end());
for (int i = 0; i < (int)vec[u].size(); i++) {
vector<map<int, int>::iterator> to_del;
ii to_add = mp(-1, -1);
auto [t, val] = vec[u][i];
M[u][t] += val;
auto it = M[u].find(t);
++it;
int sf = 0;
while (it != M[u].end()) {
if (sf + it->second <= val) {
to_del.pb(it);
sf += it->second;
} else {
to_del.pb(it);
assert(sf + it->second - val > 0);
to_add = mp(it->first, sf + it->second - val);
break;
}
++it;
}
for (auto j : to_del) M[u].erase(j);
if (to_add.first != -1) M[u][to_add.first] += to_add.second;
}
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 2, p; i <= n; i++) {
cin >> p;
adj[p].pb(i);
}
for (int i = 1, v, d, w; i <= m; i++) {
cin >> v >> d >> w;
vec[v].eb(d, w);
}
dp(1);
for (auto i : M[1]) ans += i.second;
cout << ans << '\n';
}
Compilation message
magictree.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
20036 KB |
Output is correct |
2 |
Correct |
50 ms |
23936 KB |
Output is correct |
3 |
Correct |
141 ms |
39340 KB |
Output is correct |
4 |
Correct |
104 ms |
22600 KB |
Output is correct |
5 |
Correct |
129 ms |
25068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9852 KB |
Output is correct |
2 |
Correct |
6 ms |
9940 KB |
Output is correct |
3 |
Correct |
7 ms |
9940 KB |
Output is correct |
4 |
Correct |
63 ms |
36276 KB |
Output is correct |
5 |
Correct |
86 ms |
40152 KB |
Output is correct |
6 |
Correct |
85 ms |
38348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
17996 KB |
Output is correct |
2 |
Correct |
80 ms |
17296 KB |
Output is correct |
3 |
Correct |
81 ms |
24284 KB |
Output is correct |
4 |
Correct |
51 ms |
18832 KB |
Output is correct |
5 |
Correct |
55 ms |
35972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
96 ms |
20988 KB |
Output is correct |
11 |
Correct |
85 ms |
19856 KB |
Output is correct |
12 |
Correct |
72 ms |
24276 KB |
Output is correct |
13 |
Correct |
52 ms |
18864 KB |
Output is correct |
14 |
Correct |
60 ms |
35996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10296 KB |
Output is correct |
2 |
Correct |
37 ms |
11788 KB |
Output is correct |
3 |
Correct |
31 ms |
11732 KB |
Output is correct |
4 |
Correct |
29 ms |
11816 KB |
Output is correct |
5 |
Correct |
11 ms |
10836 KB |
Output is correct |
6 |
Correct |
29 ms |
15516 KB |
Output is correct |
7 |
Correct |
29 ms |
20436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
8 ms |
9852 KB |
Output is correct |
11 |
Correct |
6 ms |
9940 KB |
Output is correct |
12 |
Correct |
7 ms |
9940 KB |
Output is correct |
13 |
Correct |
63 ms |
36276 KB |
Output is correct |
14 |
Correct |
86 ms |
40152 KB |
Output is correct |
15 |
Correct |
85 ms |
38348 KB |
Output is correct |
16 |
Correct |
96 ms |
20988 KB |
Output is correct |
17 |
Correct |
85 ms |
19856 KB |
Output is correct |
18 |
Correct |
72 ms |
24276 KB |
Output is correct |
19 |
Correct |
52 ms |
18864 KB |
Output is correct |
20 |
Correct |
60 ms |
35996 KB |
Output is correct |
21 |
Correct |
25 ms |
13424 KB |
Output is correct |
22 |
Correct |
79 ms |
22012 KB |
Output is correct |
23 |
Correct |
92 ms |
25292 KB |
Output is correct |
24 |
Correct |
135 ms |
34980 KB |
Output is correct |
25 |
Correct |
89 ms |
23992 KB |
Output is correct |
26 |
Correct |
120 ms |
27496 KB |
Output is correct |
27 |
Correct |
107 ms |
27584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9692 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9680 KB |
Output is correct |
10 |
Correct |
67 ms |
20036 KB |
Output is correct |
11 |
Correct |
50 ms |
23936 KB |
Output is correct |
12 |
Correct |
141 ms |
39340 KB |
Output is correct |
13 |
Correct |
104 ms |
22600 KB |
Output is correct |
14 |
Correct |
129 ms |
25068 KB |
Output is correct |
15 |
Correct |
8 ms |
9852 KB |
Output is correct |
16 |
Correct |
6 ms |
9940 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
63 ms |
36276 KB |
Output is correct |
19 |
Correct |
86 ms |
40152 KB |
Output is correct |
20 |
Correct |
85 ms |
38348 KB |
Output is correct |
21 |
Correct |
91 ms |
17996 KB |
Output is correct |
22 |
Correct |
80 ms |
17296 KB |
Output is correct |
23 |
Correct |
81 ms |
24284 KB |
Output is correct |
24 |
Correct |
51 ms |
18832 KB |
Output is correct |
25 |
Correct |
55 ms |
35972 KB |
Output is correct |
26 |
Correct |
96 ms |
20988 KB |
Output is correct |
27 |
Correct |
85 ms |
19856 KB |
Output is correct |
28 |
Correct |
72 ms |
24276 KB |
Output is correct |
29 |
Correct |
52 ms |
18864 KB |
Output is correct |
30 |
Correct |
60 ms |
35996 KB |
Output is correct |
31 |
Correct |
10 ms |
10296 KB |
Output is correct |
32 |
Correct |
37 ms |
11788 KB |
Output is correct |
33 |
Correct |
31 ms |
11732 KB |
Output is correct |
34 |
Correct |
29 ms |
11816 KB |
Output is correct |
35 |
Correct |
11 ms |
10836 KB |
Output is correct |
36 |
Correct |
29 ms |
15516 KB |
Output is correct |
37 |
Correct |
29 ms |
20436 KB |
Output is correct |
38 |
Correct |
25 ms |
13424 KB |
Output is correct |
39 |
Correct |
79 ms |
22012 KB |
Output is correct |
40 |
Correct |
92 ms |
25292 KB |
Output is correct |
41 |
Correct |
135 ms |
34980 KB |
Output is correct |
42 |
Correct |
89 ms |
23992 KB |
Output is correct |
43 |
Correct |
120 ms |
27496 KB |
Output is correct |
44 |
Correct |
107 ms |
27584 KB |
Output is correct |
45 |
Correct |
26 ms |
13884 KB |
Output is correct |
46 |
Correct |
91 ms |
23128 KB |
Output is correct |
47 |
Correct |
99 ms |
26744 KB |
Output is correct |
48 |
Correct |
159 ms |
37924 KB |
Output is correct |
49 |
Correct |
110 ms |
24692 KB |
Output is correct |
50 |
Correct |
140 ms |
28292 KB |
Output is correct |
51 |
Correct |
122 ms |
28680 KB |
Output is correct |