#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ll=long long;
using ii=pair<int,int>;
const int mxN = 1e5+5;
int N, M, K;
vector<int> al[mxN];
ii fruit[mxN];
map<int,ll> dfs(int u) {
map<int,ll> res;
for (int v : al[u]) {
auto x = dfs(v);
if (SZ(res) < SZ(x)) swap(res,x);
for (auto& y : x) {
if (res.count(y.first)) res[y.first] += y.second;
else res.insert(y);
}
}
if (fruit[u] != ii(-1,-1)) {
auto it = res.upper_bound(fruit[u].first);
if (it != res.begin() && prev(it)->first == fruit[u].first) {
auto x = *prev(it);
res.erase(prev(it));
x.second += fruit[u].second;
res.insert(x);
} else res.insert(fruit[u]);
for (ll y = fruit[u].second; y && it != res.end();) {
auto x = *it;
auto jt = next(it);
res.erase(it);
ll d = min(y,x.second);
y -= d;
x.second -= d;
if (x.second) res.insert(x);
it = jt;
}
}
//cout << u << " :: ";
//for (auto& x : res) cout << x.first << " " << x.second << " | ";
//cout << endl;
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> K;
FOR(i,2,N){
int P; cin >> P;
al[P].push_back(i);
}
FOR(i,1,N) fruit[i] = ii(-1,-1);
FOR(i,1,M){
int V, D, W; cin >> V >> D >> W;
fruit[V] = ii(D,W);
}
auto res = dfs(1);
ll ans = 0;
for (auto& x : res) ans += x.second;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
6868 KB |
Output is correct |
2 |
Correct |
51 ms |
13944 KB |
Output is correct |
3 |
Correct |
152 ms |
8824 KB |
Output is correct |
4 |
Correct |
84 ms |
7540 KB |
Output is correct |
5 |
Correct |
103 ms |
9592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
6 ms |
2944 KB |
Output is correct |
4 |
Correct |
74 ms |
22264 KB |
Output is correct |
5 |
Correct |
93 ms |
26232 KB |
Output is correct |
6 |
Correct |
83 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
5120 KB |
Output is correct |
2 |
Correct |
64 ms |
5144 KB |
Output is correct |
3 |
Correct |
71 ms |
12280 KB |
Output is correct |
4 |
Correct |
43 ms |
3964 KB |
Output is correct |
5 |
Correct |
68 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
92 ms |
5120 KB |
Output is correct |
11 |
Correct |
84 ms |
5112 KB |
Output is correct |
12 |
Correct |
78 ms |
12280 KB |
Output is correct |
13 |
Correct |
47 ms |
3964 KB |
Output is correct |
14 |
Correct |
77 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3200 KB |
Output is correct |
2 |
Correct |
29 ms |
5112 KB |
Output is correct |
3 |
Correct |
49 ms |
5120 KB |
Output is correct |
4 |
Correct |
30 ms |
5120 KB |
Output is correct |
5 |
Correct |
13 ms |
3964 KB |
Output is correct |
6 |
Correct |
34 ms |
8064 KB |
Output is correct |
7 |
Correct |
37 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
6 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2944 KB |
Output is correct |
13 |
Correct |
74 ms |
22264 KB |
Output is correct |
14 |
Correct |
93 ms |
26232 KB |
Output is correct |
15 |
Correct |
83 ms |
22392 KB |
Output is correct |
16 |
Correct |
92 ms |
5120 KB |
Output is correct |
17 |
Correct |
84 ms |
5112 KB |
Output is correct |
18 |
Correct |
78 ms |
12280 KB |
Output is correct |
19 |
Correct |
47 ms |
3964 KB |
Output is correct |
20 |
Correct |
77 ms |
22264 KB |
Output is correct |
21 |
Correct |
26 ms |
3456 KB |
Output is correct |
22 |
Correct |
93 ms |
5624 KB |
Output is correct |
23 |
Correct |
91 ms |
7416 KB |
Output is correct |
24 |
Correct |
151 ms |
8440 KB |
Output is correct |
25 |
Correct |
79 ms |
7540 KB |
Output is correct |
26 |
Correct |
112 ms |
12024 KB |
Output is correct |
27 |
Correct |
100 ms |
13816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2816 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
66 ms |
6868 KB |
Output is correct |
11 |
Correct |
51 ms |
13944 KB |
Output is correct |
12 |
Correct |
152 ms |
8824 KB |
Output is correct |
13 |
Correct |
84 ms |
7540 KB |
Output is correct |
14 |
Correct |
103 ms |
9592 KB |
Output is correct |
15 |
Correct |
6 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2816 KB |
Output is correct |
17 |
Correct |
6 ms |
2944 KB |
Output is correct |
18 |
Correct |
74 ms |
22264 KB |
Output is correct |
19 |
Correct |
93 ms |
26232 KB |
Output is correct |
20 |
Correct |
83 ms |
22392 KB |
Output is correct |
21 |
Correct |
67 ms |
5120 KB |
Output is correct |
22 |
Correct |
64 ms |
5144 KB |
Output is correct |
23 |
Correct |
71 ms |
12280 KB |
Output is correct |
24 |
Correct |
43 ms |
3964 KB |
Output is correct |
25 |
Correct |
68 ms |
22264 KB |
Output is correct |
26 |
Correct |
92 ms |
5120 KB |
Output is correct |
27 |
Correct |
84 ms |
5112 KB |
Output is correct |
28 |
Correct |
78 ms |
12280 KB |
Output is correct |
29 |
Correct |
47 ms |
3964 KB |
Output is correct |
30 |
Correct |
77 ms |
22264 KB |
Output is correct |
31 |
Correct |
11 ms |
3200 KB |
Output is correct |
32 |
Correct |
29 ms |
5112 KB |
Output is correct |
33 |
Correct |
49 ms |
5120 KB |
Output is correct |
34 |
Correct |
30 ms |
5120 KB |
Output is correct |
35 |
Correct |
13 ms |
3964 KB |
Output is correct |
36 |
Correct |
34 ms |
8064 KB |
Output is correct |
37 |
Correct |
37 ms |
12280 KB |
Output is correct |
38 |
Correct |
26 ms |
3456 KB |
Output is correct |
39 |
Correct |
93 ms |
5624 KB |
Output is correct |
40 |
Correct |
91 ms |
7416 KB |
Output is correct |
41 |
Correct |
151 ms |
8440 KB |
Output is correct |
42 |
Correct |
79 ms |
7540 KB |
Output is correct |
43 |
Correct |
112 ms |
12024 KB |
Output is correct |
44 |
Correct |
100 ms |
13816 KB |
Output is correct |
45 |
Correct |
26 ms |
3456 KB |
Output is correct |
46 |
Correct |
79 ms |
5496 KB |
Output is correct |
47 |
Correct |
95 ms |
7288 KB |
Output is correct |
48 |
Correct |
143 ms |
8824 KB |
Output is correct |
49 |
Correct |
81 ms |
7540 KB |
Output is correct |
50 |
Correct |
119 ms |
12256 KB |
Output is correct |
51 |
Correct |
111 ms |
13960 KB |
Output is correct |