#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
vector<vector<int>> v;
vector<vector<ll>> dp;
vector<int> h;
vector<ll> cost;
void dfs(int nd){
for(int x: v[nd]) dfs(x);
for(int x: v[nd]) for(int i = 0; i < n; i++) dp[nd][i]+=dp[x][i];
for(int i = h[nd]+1; i < n; i++){
dp[nd][i]+=cost[nd];
}
for(int i = h[nd]-1; i >= 0; i--){
dp[nd][i] = min(dp[nd][i+1], dp[nd][i]+cost[nd]);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v.resize(n);
dp.resize(n, vector<ll>(n, 0));
h.resize(n);
cost.resize(n);
for(int i = 0; i < n; i++){
int p; cin >> p >> h[i] >> cost[i];
p--;
if(p == i) continue;
v[p].pb(i);
}
/*compress h*/{
map<int, int> mp;
vector<int> srt = h;
sort(srt.begin(), srt.end());
int in = 0;
for(int i = 0; i < n; i++) mp[srt[i]] = in, in++;
for(int &x: h) x = mp[x];
}
dfs(0);
cout << *min_element(dp[0].begin(), dp[0].end()) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
160 ms |
196708 KB |
Output is correct |
6 |
Correct |
169 ms |
196428 KB |
Output is correct |
7 |
Correct |
174 ms |
196472 KB |
Output is correct |
8 |
Correct |
166 ms |
196852 KB |
Output is correct |
9 |
Correct |
172 ms |
196560 KB |
Output is correct |
10 |
Correct |
188 ms |
196548 KB |
Output is correct |
11 |
Correct |
191 ms |
196684 KB |
Output is correct |
12 |
Correct |
159 ms |
197128 KB |
Output is correct |
13 |
Correct |
177 ms |
196892 KB |
Output is correct |
14 |
Correct |
164 ms |
196944 KB |
Output is correct |
15 |
Correct |
159 ms |
196780 KB |
Output is correct |
16 |
Correct |
187 ms |
196816 KB |
Output is correct |
17 |
Correct |
169 ms |
196592 KB |
Output is correct |
18 |
Correct |
201 ms |
196704 KB |
Output is correct |
19 |
Correct |
172 ms |
196928 KB |
Output is correct |
20 |
Correct |
165 ms |
196616 KB |
Output is correct |
21 |
Correct |
169 ms |
196700 KB |
Output is correct |
22 |
Correct |
160 ms |
196756 KB |
Output is correct |
23 |
Correct |
160 ms |
196548 KB |
Output is correct |
24 |
Correct |
182 ms |
196944 KB |
Output is correct |
25 |
Correct |
182 ms |
196728 KB |
Output is correct |
26 |
Correct |
161 ms |
197068 KB |
Output is correct |
27 |
Correct |
174 ms |
196896 KB |
Output is correct |
28 |
Correct |
169 ms |
196836 KB |
Output is correct |
29 |
Correct |
207 ms |
197020 KB |
Output is correct |
30 |
Correct |
178 ms |
196712 KB |
Output is correct |
31 |
Correct |
193 ms |
197056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
160 ms |
196708 KB |
Output is correct |
6 |
Correct |
169 ms |
196428 KB |
Output is correct |
7 |
Correct |
174 ms |
196472 KB |
Output is correct |
8 |
Correct |
166 ms |
196852 KB |
Output is correct |
9 |
Correct |
172 ms |
196560 KB |
Output is correct |
10 |
Correct |
188 ms |
196548 KB |
Output is correct |
11 |
Correct |
191 ms |
196684 KB |
Output is correct |
12 |
Correct |
159 ms |
197128 KB |
Output is correct |
13 |
Correct |
177 ms |
196892 KB |
Output is correct |
14 |
Correct |
164 ms |
196944 KB |
Output is correct |
15 |
Correct |
159 ms |
196780 KB |
Output is correct |
16 |
Correct |
187 ms |
196816 KB |
Output is correct |
17 |
Correct |
169 ms |
196592 KB |
Output is correct |
18 |
Correct |
201 ms |
196704 KB |
Output is correct |
19 |
Correct |
172 ms |
196928 KB |
Output is correct |
20 |
Correct |
165 ms |
196616 KB |
Output is correct |
21 |
Correct |
169 ms |
196700 KB |
Output is correct |
22 |
Correct |
160 ms |
196756 KB |
Output is correct |
23 |
Correct |
160 ms |
196548 KB |
Output is correct |
24 |
Correct |
182 ms |
196944 KB |
Output is correct |
25 |
Correct |
182 ms |
196728 KB |
Output is correct |
26 |
Correct |
161 ms |
197068 KB |
Output is correct |
27 |
Correct |
174 ms |
196896 KB |
Output is correct |
28 |
Correct |
169 ms |
196836 KB |
Output is correct |
29 |
Correct |
207 ms |
197020 KB |
Output is correct |
30 |
Correct |
178 ms |
196712 KB |
Output is correct |
31 |
Correct |
193 ms |
197056 KB |
Output is correct |
32 |
Correct |
192 ms |
196952 KB |
Output is correct |
33 |
Runtime error |
330 ms |
524288 KB |
Execution killed with signal 9 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
160 ms |
196708 KB |
Output is correct |
6 |
Correct |
169 ms |
196428 KB |
Output is correct |
7 |
Correct |
174 ms |
196472 KB |
Output is correct |
8 |
Correct |
166 ms |
196852 KB |
Output is correct |
9 |
Correct |
172 ms |
196560 KB |
Output is correct |
10 |
Correct |
188 ms |
196548 KB |
Output is correct |
11 |
Correct |
191 ms |
196684 KB |
Output is correct |
12 |
Correct |
159 ms |
197128 KB |
Output is correct |
13 |
Correct |
177 ms |
196892 KB |
Output is correct |
14 |
Correct |
164 ms |
196944 KB |
Output is correct |
15 |
Correct |
159 ms |
196780 KB |
Output is correct |
16 |
Correct |
187 ms |
196816 KB |
Output is correct |
17 |
Correct |
169 ms |
196592 KB |
Output is correct |
18 |
Correct |
201 ms |
196704 KB |
Output is correct |
19 |
Correct |
172 ms |
196928 KB |
Output is correct |
20 |
Correct |
165 ms |
196616 KB |
Output is correct |
21 |
Correct |
169 ms |
196700 KB |
Output is correct |
22 |
Correct |
160 ms |
196756 KB |
Output is correct |
23 |
Correct |
160 ms |
196548 KB |
Output is correct |
24 |
Correct |
182 ms |
196944 KB |
Output is correct |
25 |
Correct |
182 ms |
196728 KB |
Output is correct |
26 |
Correct |
161 ms |
197068 KB |
Output is correct |
27 |
Correct |
174 ms |
196896 KB |
Output is correct |
28 |
Correct |
169 ms |
196836 KB |
Output is correct |
29 |
Correct |
207 ms |
197020 KB |
Output is correct |
30 |
Correct |
178 ms |
196712 KB |
Output is correct |
31 |
Correct |
193 ms |
197056 KB |
Output is correct |
32 |
Correct |
192 ms |
196952 KB |
Output is correct |
33 |
Runtime error |
330 ms |
524288 KB |
Execution killed with signal 9 |
34 |
Halted |
0 ms |
0 KB |
- |