#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)
typedef long long ll;
//const int mxN = 2e5+5;
const int mxN = 5e3+5;
const ll inf = 1e18;
int N, A[mxN], H[mxN], C[mxN], V;
vector<int> al[mxN], vals;
ll dp[mxN][mxN];
void dfs(int u) {
FOR(x,0,V-1) dp[u][x] = 0;
for (int v : al[u]) {
dfs(v);
FOR(x,0,V-1){
ll cur = dp[v][x] + C[v];
if (x <= H[v]) cur = min(cur, dp[v][H[v]]);
dp[u][x] += cur;
}
}
//cout << u << ": ";
//FOR(x,0,V-1){
// cout << dp[u][x] << ' ';
//}
//cout << endl;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
FOR(i,1,N){
cin >> A[i] >> H[i] >> C[i];
if (i != 1) al[A[i]].push_back(i);
vals.push_back(H[i]);
}
vals.push_back(1);
sort(ALL(vals));
vals.resize(unique(ALL(vals))-vals.begin());
FOR(i,1,N){
H[i] = lower_bound(ALL(vals),H[i])-vals.begin();
}
V = SZ(vals);
dfs(1);
cout << min(dp[1][0] + C[1], dp[1][H[1]]) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
121 ms |
196512 KB |
Output is correct |
6 |
Correct |
14 ms |
24908 KB |
Output is correct |
7 |
Correct |
12 ms |
21492 KB |
Output is correct |
8 |
Correct |
120 ms |
196484 KB |
Output is correct |
9 |
Correct |
15 ms |
24908 KB |
Output is correct |
10 |
Correct |
12 ms |
21452 KB |
Output is correct |
11 |
Correct |
12 ms |
21092 KB |
Output is correct |
12 |
Correct |
127 ms |
197080 KB |
Output is correct |
13 |
Correct |
12 ms |
21952 KB |
Output is correct |
14 |
Correct |
126 ms |
196956 KB |
Output is correct |
15 |
Correct |
15 ms |
25164 KB |
Output is correct |
16 |
Correct |
116 ms |
196528 KB |
Output is correct |
17 |
Correct |
14 ms |
24912 KB |
Output is correct |
18 |
Correct |
11 ms |
21056 KB |
Output is correct |
19 |
Correct |
121 ms |
196688 KB |
Output is correct |
20 |
Correct |
14 ms |
25164 KB |
Output is correct |
21 |
Correct |
11 ms |
21408 KB |
Output is correct |
22 |
Correct |
112 ms |
196524 KB |
Output is correct |
23 |
Correct |
14 ms |
24884 KB |
Output is correct |
24 |
Correct |
119 ms |
196736 KB |
Output is correct |
25 |
Correct |
14 ms |
25156 KB |
Output is correct |
26 |
Correct |
120 ms |
197048 KB |
Output is correct |
27 |
Correct |
124 ms |
196704 KB |
Output is correct |
28 |
Correct |
121 ms |
196748 KB |
Output is correct |
29 |
Correct |
125 ms |
196836 KB |
Output is correct |
30 |
Correct |
123 ms |
196712 KB |
Output is correct |
31 |
Correct |
123 ms |
196892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
121 ms |
196512 KB |
Output is correct |
6 |
Correct |
14 ms |
24908 KB |
Output is correct |
7 |
Correct |
12 ms |
21492 KB |
Output is correct |
8 |
Correct |
120 ms |
196484 KB |
Output is correct |
9 |
Correct |
15 ms |
24908 KB |
Output is correct |
10 |
Correct |
12 ms |
21452 KB |
Output is correct |
11 |
Correct |
12 ms |
21092 KB |
Output is correct |
12 |
Correct |
127 ms |
197080 KB |
Output is correct |
13 |
Correct |
12 ms |
21952 KB |
Output is correct |
14 |
Correct |
126 ms |
196956 KB |
Output is correct |
15 |
Correct |
15 ms |
25164 KB |
Output is correct |
16 |
Correct |
116 ms |
196528 KB |
Output is correct |
17 |
Correct |
14 ms |
24912 KB |
Output is correct |
18 |
Correct |
11 ms |
21056 KB |
Output is correct |
19 |
Correct |
121 ms |
196688 KB |
Output is correct |
20 |
Correct |
14 ms |
25164 KB |
Output is correct |
21 |
Correct |
11 ms |
21408 KB |
Output is correct |
22 |
Correct |
112 ms |
196524 KB |
Output is correct |
23 |
Correct |
14 ms |
24884 KB |
Output is correct |
24 |
Correct |
119 ms |
196736 KB |
Output is correct |
25 |
Correct |
14 ms |
25156 KB |
Output is correct |
26 |
Correct |
120 ms |
197048 KB |
Output is correct |
27 |
Correct |
124 ms |
196704 KB |
Output is correct |
28 |
Correct |
121 ms |
196748 KB |
Output is correct |
29 |
Correct |
125 ms |
196836 KB |
Output is correct |
30 |
Correct |
123 ms |
196712 KB |
Output is correct |
31 |
Correct |
123 ms |
196892 KB |
Output is correct |
32 |
Correct |
118 ms |
196528 KB |
Output is correct |
33 |
Runtime error |
14 ms |
2716 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
121 ms |
196512 KB |
Output is correct |
6 |
Correct |
14 ms |
24908 KB |
Output is correct |
7 |
Correct |
12 ms |
21492 KB |
Output is correct |
8 |
Correct |
120 ms |
196484 KB |
Output is correct |
9 |
Correct |
15 ms |
24908 KB |
Output is correct |
10 |
Correct |
12 ms |
21452 KB |
Output is correct |
11 |
Correct |
12 ms |
21092 KB |
Output is correct |
12 |
Correct |
127 ms |
197080 KB |
Output is correct |
13 |
Correct |
12 ms |
21952 KB |
Output is correct |
14 |
Correct |
126 ms |
196956 KB |
Output is correct |
15 |
Correct |
15 ms |
25164 KB |
Output is correct |
16 |
Correct |
116 ms |
196528 KB |
Output is correct |
17 |
Correct |
14 ms |
24912 KB |
Output is correct |
18 |
Correct |
11 ms |
21056 KB |
Output is correct |
19 |
Correct |
121 ms |
196688 KB |
Output is correct |
20 |
Correct |
14 ms |
25164 KB |
Output is correct |
21 |
Correct |
11 ms |
21408 KB |
Output is correct |
22 |
Correct |
112 ms |
196524 KB |
Output is correct |
23 |
Correct |
14 ms |
24884 KB |
Output is correct |
24 |
Correct |
119 ms |
196736 KB |
Output is correct |
25 |
Correct |
14 ms |
25156 KB |
Output is correct |
26 |
Correct |
120 ms |
197048 KB |
Output is correct |
27 |
Correct |
124 ms |
196704 KB |
Output is correct |
28 |
Correct |
121 ms |
196748 KB |
Output is correct |
29 |
Correct |
125 ms |
196836 KB |
Output is correct |
30 |
Correct |
123 ms |
196712 KB |
Output is correct |
31 |
Correct |
123 ms |
196892 KB |
Output is correct |
32 |
Correct |
118 ms |
196528 KB |
Output is correct |
33 |
Runtime error |
14 ms |
2716 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |