#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define ff first
#define ss second
const int N = 1e5 + 9;
int n, m, k;
int p[N];
pii fruit[N];
vector<int> g[N];
map<int, ll> dp[N];
namespace sub8 {
int cnt(0), b[N];
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
if (dp[u].size() < dp[v].size()) dp[u].swap(dp[v]);
for (auto it : dp[v])
dp[u][it.ff] += it.ss;
}
if (fruit[u].ff) {
dp[u][fruit[u].ff] += fruit[u].ss;
map<int, ll>::iterator it = dp[u].upper_bound(fruit[u].ff), tmp;
for (ll diff = 0; it != dp[u].end(); it = tmp) {
diff += it->ss;
if (diff <= fruit[u].ss) {
// set dp[u][it->ff] = dp[u][fruit[u].ff], reset diff = 0
tmp = next(it);
dp[u].erase(it);
}
else {
// nothing change, recalc diff
it->ss = diff - fruit[u].ss;
break;
}
}
}
}
void solve() {
for (int i = 1; i <= n; ++i) if (fruit[i].ff) b[++cnt] = fruit[i].ff;
sort(b + 1, b + cnt + 1);
cnt = unique(b + 1, b + cnt + 1) - b;
for (int i = 1; i <= n; ++i)
if (fruit[i].ff) fruit[i].ff = lower_bound(b + 1, b + cnt, fruit[i].ff) - b;
dfs(1);
ll ans(0);
for (auto it : dp[1]) ans += it.ss;
cout << ans << '\n';
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 2; i <= n; ++i) {
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 0, v; i < m; ++i) {
cin >> v;
cin >> fruit[v].ff >> fruit[v].ss;
}
sub8::solve();
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7272 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7328 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
18924 KB |
Output is correct |
2 |
Correct |
45 ms |
20036 KB |
Output is correct |
3 |
Correct |
137 ms |
37712 KB |
Output is correct |
4 |
Correct |
88 ms |
20704 KB |
Output is correct |
5 |
Correct |
120 ms |
22716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7500 KB |
Output is correct |
2 |
Correct |
5 ms |
7508 KB |
Output is correct |
3 |
Correct |
6 ms |
7504 KB |
Output is correct |
4 |
Correct |
52 ms |
28000 KB |
Output is correct |
5 |
Correct |
72 ms |
31936 KB |
Output is correct |
6 |
Correct |
73 ms |
28012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
16288 KB |
Output is correct |
2 |
Correct |
76 ms |
15460 KB |
Output is correct |
3 |
Correct |
64 ms |
20164 KB |
Output is correct |
4 |
Correct |
49 ms |
16592 KB |
Output is correct |
5 |
Correct |
54 ms |
28216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7272 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7328 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7372 KB |
Output is correct |
10 |
Correct |
77 ms |
18412 KB |
Output is correct |
11 |
Correct |
75 ms |
17416 KB |
Output is correct |
12 |
Correct |
59 ms |
19584 KB |
Output is correct |
13 |
Correct |
60 ms |
15960 KB |
Output is correct |
14 |
Correct |
50 ms |
27592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
8132 KB |
Output is correct |
2 |
Correct |
26 ms |
10828 KB |
Output is correct |
3 |
Correct |
37 ms |
10784 KB |
Output is correct |
4 |
Correct |
29 ms |
10820 KB |
Output is correct |
5 |
Correct |
14 ms |
9152 KB |
Output is correct |
6 |
Correct |
32 ms |
13344 KB |
Output is correct |
7 |
Correct |
31 ms |
17212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7272 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7328 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7500 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
6 ms |
7504 KB |
Output is correct |
13 |
Correct |
52 ms |
28000 KB |
Output is correct |
14 |
Correct |
72 ms |
31936 KB |
Output is correct |
15 |
Correct |
73 ms |
28012 KB |
Output is correct |
16 |
Correct |
77 ms |
18412 KB |
Output is correct |
17 |
Correct |
75 ms |
17416 KB |
Output is correct |
18 |
Correct |
59 ms |
19584 KB |
Output is correct |
19 |
Correct |
60 ms |
15960 KB |
Output is correct |
20 |
Correct |
50 ms |
27592 KB |
Output is correct |
21 |
Correct |
24 ms |
10788 KB |
Output is correct |
22 |
Correct |
86 ms |
19272 KB |
Output is correct |
23 |
Correct |
91 ms |
22504 KB |
Output is correct |
24 |
Correct |
158 ms |
31088 KB |
Output is correct |
25 |
Correct |
97 ms |
19912 KB |
Output is correct |
26 |
Correct |
137 ms |
22104 KB |
Output is correct |
27 |
Correct |
96 ms |
21412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7272 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7328 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
3 ms |
7372 KB |
Output is correct |
10 |
Correct |
62 ms |
18924 KB |
Output is correct |
11 |
Correct |
45 ms |
20036 KB |
Output is correct |
12 |
Correct |
137 ms |
37712 KB |
Output is correct |
13 |
Correct |
88 ms |
20704 KB |
Output is correct |
14 |
Correct |
120 ms |
22716 KB |
Output is correct |
15 |
Correct |
6 ms |
7500 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
6 ms |
7504 KB |
Output is correct |
18 |
Correct |
52 ms |
28000 KB |
Output is correct |
19 |
Correct |
72 ms |
31936 KB |
Output is correct |
20 |
Correct |
73 ms |
28012 KB |
Output is correct |
21 |
Correct |
68 ms |
16288 KB |
Output is correct |
22 |
Correct |
76 ms |
15460 KB |
Output is correct |
23 |
Correct |
64 ms |
20164 KB |
Output is correct |
24 |
Correct |
49 ms |
16592 KB |
Output is correct |
25 |
Correct |
54 ms |
28216 KB |
Output is correct |
26 |
Correct |
77 ms |
18412 KB |
Output is correct |
27 |
Correct |
75 ms |
17416 KB |
Output is correct |
28 |
Correct |
59 ms |
19584 KB |
Output is correct |
29 |
Correct |
60 ms |
15960 KB |
Output is correct |
30 |
Correct |
50 ms |
27592 KB |
Output is correct |
31 |
Correct |
12 ms |
8132 KB |
Output is correct |
32 |
Correct |
26 ms |
10828 KB |
Output is correct |
33 |
Correct |
37 ms |
10784 KB |
Output is correct |
34 |
Correct |
29 ms |
10820 KB |
Output is correct |
35 |
Correct |
14 ms |
9152 KB |
Output is correct |
36 |
Correct |
32 ms |
13344 KB |
Output is correct |
37 |
Correct |
31 ms |
17212 KB |
Output is correct |
38 |
Correct |
24 ms |
10788 KB |
Output is correct |
39 |
Correct |
86 ms |
19272 KB |
Output is correct |
40 |
Correct |
91 ms |
22504 KB |
Output is correct |
41 |
Correct |
158 ms |
31088 KB |
Output is correct |
42 |
Correct |
97 ms |
19912 KB |
Output is correct |
43 |
Correct |
137 ms |
22104 KB |
Output is correct |
44 |
Correct |
96 ms |
21412 KB |
Output is correct |
45 |
Correct |
25 ms |
11236 KB |
Output is correct |
46 |
Correct |
88 ms |
20508 KB |
Output is correct |
47 |
Correct |
90 ms |
23824 KB |
Output is correct |
48 |
Correct |
142 ms |
33972 KB |
Output is correct |
49 |
Correct |
111 ms |
20692 KB |
Output is correct |
50 |
Correct |
131 ms |
23060 KB |
Output is correct |
51 |
Correct |
134 ms |
22388 KB |
Output is correct |