#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n+1);
vector<ll> h(n+1), c(n+1);
int root = 1;
ll sumc = 0;
for(int i = 1; i <= n; i++){
int v;
cin >> v >> h[i] >> c[i];
sumc += c[i];
adj[i].push_back(v);
adj[v].push_back(i);
}
auto add = [&](map<ll, ll> &u, map<ll, ll> &v){
if(u.size() < v.size()) swap(u, v);
for(auto it : v){
ll h = it.first, c = it.second;
u[h] += c;
}
};
auto fix = [&](int node, map<ll, ll> &u, ll h, ll d){
u[h] -= d;
u[h+1] += d;
u[0] += d;
if(u[h] >= 0 || node == root) return;
d = -u[h];
u[h] = 0;
auto it = u.lower_bound(h);
while(it != u.begin()){
it = prev(it);
if(it->second <= d){
d -= it->second;
it = u.erase(it);
}else{
it->second -= d;
break;
}
}
};
vector<map<ll, ll>> dp(n+1);
auto dfs = [&](auto dfs, int u, int p) -> void{
for(int v : adj[u]){
if(v == p) continue;
dfs(dfs, v, u);
add(dp[u], dp[v]);
}
fix(u, dp[u], h[u], c[u]);
};
dfs(dfs, 1, 1);
ll ans = INF, res = 0;
for(auto it : dp[1]){
res += it.second;
ans = min(ans, res);
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
3328 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
3 ms |
1816 KB |
Output is correct |
8 |
Correct |
7 ms |
3164 KB |
Output is correct |
9 |
Correct |
5 ms |
2140 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
4 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
2140 KB |
Output is correct |
14 |
Correct |
4 ms |
1628 KB |
Output is correct |
15 |
Correct |
3 ms |
1628 KB |
Output is correct |
16 |
Correct |
8 ms |
3856 KB |
Output is correct |
17 |
Correct |
5 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
5 ms |
2140 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1880 KB |
Output is correct |
22 |
Correct |
5 ms |
2396 KB |
Output is correct |
23 |
Correct |
3 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
2140 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2136 KB |
Output is correct |
27 |
Correct |
5 ms |
2392 KB |
Output is correct |
28 |
Correct |
4 ms |
2396 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
4 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
3328 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
3 ms |
1816 KB |
Output is correct |
8 |
Correct |
7 ms |
3164 KB |
Output is correct |
9 |
Correct |
5 ms |
2140 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
4 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
2140 KB |
Output is correct |
14 |
Correct |
4 ms |
1628 KB |
Output is correct |
15 |
Correct |
3 ms |
1628 KB |
Output is correct |
16 |
Correct |
8 ms |
3856 KB |
Output is correct |
17 |
Correct |
5 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
5 ms |
2140 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1880 KB |
Output is correct |
22 |
Correct |
5 ms |
2396 KB |
Output is correct |
23 |
Correct |
3 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
2140 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2136 KB |
Output is correct |
27 |
Correct |
5 ms |
2392 KB |
Output is correct |
28 |
Correct |
4 ms |
2396 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
4 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
7 ms |
3164 KB |
Output is correct |
33 |
Correct |
397 ms |
139984 KB |
Output is correct |
34 |
Correct |
282 ms |
90304 KB |
Output is correct |
35 |
Correct |
380 ms |
133972 KB |
Output is correct |
36 |
Correct |
261 ms |
90240 KB |
Output is correct |
37 |
Correct |
149 ms |
52308 KB |
Output is correct |
38 |
Correct |
133 ms |
41476 KB |
Output is correct |
39 |
Correct |
164 ms |
69712 KB |
Output is correct |
40 |
Correct |
146 ms |
69616 KB |
Output is correct |
41 |
Correct |
97 ms |
69740 KB |
Output is correct |
42 |
Correct |
146 ms |
49744 KB |
Output is correct |
43 |
Correct |
137 ms |
49532 KB |
Output is correct |
44 |
Correct |
409 ms |
190544 KB |
Output is correct |
45 |
Correct |
287 ms |
119116 KB |
Output is correct |
46 |
Correct |
78 ms |
41300 KB |
Output is correct |
47 |
Correct |
269 ms |
75736 KB |
Output is correct |
48 |
Correct |
130 ms |
61892 KB |
Output is correct |
49 |
Correct |
85 ms |
61716 KB |
Output is correct |
50 |
Correct |
228 ms |
79048 KB |
Output is correct |
51 |
Correct |
95 ms |
54028 KB |
Output is correct |
52 |
Correct |
268 ms |
76492 KB |
Output is correct |
53 |
Correct |
127 ms |
62408 KB |
Output is correct |
54 |
Correct |
100 ms |
70128 KB |
Output is correct |
55 |
Correct |
203 ms |
79444 KB |
Output is correct |
56 |
Correct |
181 ms |
84368 KB |
Output is correct |
57 |
Correct |
191 ms |
87544 KB |
Output is correct |
58 |
Correct |
228 ms |
86096 KB |
Output is correct |
59 |
Correct |
227 ms |
86100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
7 ms |
3328 KB |
Output is correct |
6 |
Correct |
5 ms |
2140 KB |
Output is correct |
7 |
Correct |
3 ms |
1816 KB |
Output is correct |
8 |
Correct |
7 ms |
3164 KB |
Output is correct |
9 |
Correct |
5 ms |
2140 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
4 ms |
2140 KB |
Output is correct |
13 |
Correct |
3 ms |
2140 KB |
Output is correct |
14 |
Correct |
4 ms |
1628 KB |
Output is correct |
15 |
Correct |
3 ms |
1628 KB |
Output is correct |
16 |
Correct |
8 ms |
3856 KB |
Output is correct |
17 |
Correct |
5 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
5 ms |
2140 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1880 KB |
Output is correct |
22 |
Correct |
5 ms |
2396 KB |
Output is correct |
23 |
Correct |
3 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
2140 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2136 KB |
Output is correct |
27 |
Correct |
5 ms |
2392 KB |
Output is correct |
28 |
Correct |
4 ms |
2396 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
4 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
7 ms |
3164 KB |
Output is correct |
33 |
Correct |
397 ms |
139984 KB |
Output is correct |
34 |
Correct |
282 ms |
90304 KB |
Output is correct |
35 |
Correct |
380 ms |
133972 KB |
Output is correct |
36 |
Correct |
261 ms |
90240 KB |
Output is correct |
37 |
Correct |
149 ms |
52308 KB |
Output is correct |
38 |
Correct |
133 ms |
41476 KB |
Output is correct |
39 |
Correct |
164 ms |
69712 KB |
Output is correct |
40 |
Correct |
146 ms |
69616 KB |
Output is correct |
41 |
Correct |
97 ms |
69740 KB |
Output is correct |
42 |
Correct |
146 ms |
49744 KB |
Output is correct |
43 |
Correct |
137 ms |
49532 KB |
Output is correct |
44 |
Correct |
409 ms |
190544 KB |
Output is correct |
45 |
Correct |
287 ms |
119116 KB |
Output is correct |
46 |
Correct |
78 ms |
41300 KB |
Output is correct |
47 |
Correct |
269 ms |
75736 KB |
Output is correct |
48 |
Correct |
130 ms |
61892 KB |
Output is correct |
49 |
Correct |
85 ms |
61716 KB |
Output is correct |
50 |
Correct |
228 ms |
79048 KB |
Output is correct |
51 |
Correct |
95 ms |
54028 KB |
Output is correct |
52 |
Correct |
268 ms |
76492 KB |
Output is correct |
53 |
Correct |
127 ms |
62408 KB |
Output is correct |
54 |
Correct |
100 ms |
70128 KB |
Output is correct |
55 |
Correct |
203 ms |
79444 KB |
Output is correct |
56 |
Correct |
181 ms |
84368 KB |
Output is correct |
57 |
Correct |
191 ms |
87544 KB |
Output is correct |
58 |
Correct |
228 ms |
86096 KB |
Output is correct |
59 |
Correct |
227 ms |
86100 KB |
Output is correct |
60 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |