// author : HuuHung
// 25.3.2024
#include <bits/stdc++.h>
#define ii pair <int, int>
#define F first
#define S second
#define ll long long
#define lb long double
#define pb push_back
#define vi vector <int>
#define vll vector <ll>
#define Bit(x, i) ((x) >> (i) & 1)
#define Mask(i) (1ll << (i))
#define All(v) (v).begin(), (v).end()
using namespace std;
void maxzi(int &a, int b){
a = max(a, b);
}
void minzi(int &a, int b){
a = min(a, b);
}
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;
void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
int Pow(int a, int b){
if (b == 0) return 1;
if (b % 2) return 1ll * a * Pow(a, b - 1) % mod;
int c = Pow(a, b / 2);
return 1ll * c * c % mod;
}
// * end
int n, m, k;
int d[N], w[N];
vector <int> adj[N];
map <int, ll> dp[N];
void dfs(int u){
for (int v : adj[u]){
dfs(v);
if (dp[u].size() < dp[v].size()) swap(dp[u], dp[v]);
for (auto x : dp[v]) dp[u][x.F] += x.S;
}
if (d[u]){
dp[u][d[u]] += w[u];
for (auto x = dp[u].upper_bound(d[u]); x != dp[u].end();){
if (x -> S > w[u]){
x -> S -= w[u];
break;
}
w[u] -= x -> S;
auto tmp = x ++;
dp[u].erase(tmp);
}
}
}
void solve(){
cin >> n >> m >> k;
for (int i = 2, p; i <= n; ++ i){
cin >> p;
adj[p].pb(i);
}
for (int i = 1, v; i <= m; ++ i){
cin >> v;
cin >> d[v] >> w[v];
}
dfs(1);
ll ans = 0;
for (auto v : dp[1]) ans += v.S;
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t --) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
24400 KB |
Output is correct |
2 |
Correct |
33 ms |
26196 KB |
Output is correct |
3 |
Correct |
105 ms |
41676 KB |
Output is correct |
4 |
Correct |
56 ms |
25048 KB |
Output is correct |
5 |
Correct |
79 ms |
26704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
42 ms |
32520 KB |
Output is correct |
5 |
Correct |
57 ms |
36436 KB |
Output is correct |
6 |
Correct |
55 ms |
32592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
20684 KB |
Output is correct |
2 |
Correct |
48 ms |
21852 KB |
Output is correct |
3 |
Correct |
51 ms |
26704 KB |
Output is correct |
4 |
Correct |
30 ms |
23072 KB |
Output is correct |
5 |
Correct |
38 ms |
34692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
58 ms |
23376 KB |
Output is correct |
11 |
Correct |
49 ms |
22360 KB |
Output is correct |
12 |
Correct |
44 ms |
24400 KB |
Output is correct |
13 |
Correct |
29 ms |
21212 KB |
Output is correct |
14 |
Correct |
37 ms |
32596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15452 KB |
Output is correct |
2 |
Correct |
21 ms |
17576 KB |
Output is correct |
3 |
Correct |
19 ms |
17756 KB |
Output is correct |
4 |
Correct |
20 ms |
17756 KB |
Output is correct |
5 |
Correct |
11 ms |
16076 KB |
Output is correct |
6 |
Correct |
21 ms |
20056 KB |
Output is correct |
7 |
Correct |
20 ms |
24156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14936 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
42 ms |
32520 KB |
Output is correct |
14 |
Correct |
57 ms |
36436 KB |
Output is correct |
15 |
Correct |
55 ms |
32592 KB |
Output is correct |
16 |
Correct |
58 ms |
23376 KB |
Output is correct |
17 |
Correct |
49 ms |
22360 KB |
Output is correct |
18 |
Correct |
44 ms |
24400 KB |
Output is correct |
19 |
Correct |
29 ms |
21212 KB |
Output is correct |
20 |
Correct |
37 ms |
32596 KB |
Output is correct |
21 |
Correct |
16 ms |
17756 KB |
Output is correct |
22 |
Correct |
52 ms |
24816 KB |
Output is correct |
23 |
Correct |
58 ms |
27880 KB |
Output is correct |
24 |
Correct |
92 ms |
35700 KB |
Output is correct |
25 |
Correct |
65 ms |
25040 KB |
Output is correct |
26 |
Correct |
69 ms |
26708 KB |
Output is correct |
27 |
Correct |
57 ms |
26204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
46 ms |
24400 KB |
Output is correct |
11 |
Correct |
33 ms |
26196 KB |
Output is correct |
12 |
Correct |
105 ms |
41676 KB |
Output is correct |
13 |
Correct |
56 ms |
25048 KB |
Output is correct |
14 |
Correct |
79 ms |
26704 KB |
Output is correct |
15 |
Correct |
4 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
42 ms |
32520 KB |
Output is correct |
19 |
Correct |
57 ms |
36436 KB |
Output is correct |
20 |
Correct |
55 ms |
32592 KB |
Output is correct |
21 |
Correct |
46 ms |
20684 KB |
Output is correct |
22 |
Correct |
48 ms |
21852 KB |
Output is correct |
23 |
Correct |
51 ms |
26704 KB |
Output is correct |
24 |
Correct |
30 ms |
23072 KB |
Output is correct |
25 |
Correct |
38 ms |
34692 KB |
Output is correct |
26 |
Correct |
58 ms |
23376 KB |
Output is correct |
27 |
Correct |
49 ms |
22360 KB |
Output is correct |
28 |
Correct |
44 ms |
24400 KB |
Output is correct |
29 |
Correct |
29 ms |
21212 KB |
Output is correct |
30 |
Correct |
37 ms |
32596 KB |
Output is correct |
31 |
Correct |
6 ms |
15452 KB |
Output is correct |
32 |
Correct |
21 ms |
17576 KB |
Output is correct |
33 |
Correct |
19 ms |
17756 KB |
Output is correct |
34 |
Correct |
20 ms |
17756 KB |
Output is correct |
35 |
Correct |
11 ms |
16076 KB |
Output is correct |
36 |
Correct |
21 ms |
20056 KB |
Output is correct |
37 |
Correct |
20 ms |
24156 KB |
Output is correct |
38 |
Correct |
16 ms |
17756 KB |
Output is correct |
39 |
Correct |
52 ms |
24816 KB |
Output is correct |
40 |
Correct |
58 ms |
27880 KB |
Output is correct |
41 |
Correct |
92 ms |
35700 KB |
Output is correct |
42 |
Correct |
65 ms |
25040 KB |
Output is correct |
43 |
Correct |
69 ms |
26708 KB |
Output is correct |
44 |
Correct |
57 ms |
26204 KB |
Output is correct |
45 |
Correct |
17 ms |
18600 KB |
Output is correct |
46 |
Correct |
54 ms |
27076 KB |
Output is correct |
47 |
Correct |
68 ms |
30288 KB |
Output is correct |
48 |
Correct |
105 ms |
40536 KB |
Output is correct |
49 |
Correct |
56 ms |
27036 KB |
Output is correct |
50 |
Correct |
76 ms |
29660 KB |
Output is correct |
51 |
Correct |
67 ms |
28884 KB |
Output is correct |