#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];
vector<int> al[mxN];
ll dp[mxN], sum[mxN];
ll best(int u, int x) {
ll ret = 0;
for (int v : al[u]) {
ret += best(v,x);
}
if (H[x] <= H[u]) {
ret = min(ret, dp[u]-sum[u]);
}
return ret;
}
void dfs(int u) {
sum[u] = 0;
dp[u] = 0;
for (int v : al[u]) {
dfs(v);
sum[u] += sum[v];
dp[u] += best(v,u);
}
dp[u] += sum[u];
sum[u] += C[u];
}
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);
}
H[0] = 1;
dfs(1);
cout << sum[1] + best(1,0) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
5 ms |
772 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
5 ms |
716 KB |
Output is correct |
12 |
Correct |
133 ms |
1228 KB |
Output is correct |
13 |
Correct |
130 ms |
1212 KB |
Output is correct |
14 |
Correct |
65 ms |
1004 KB |
Output is correct |
15 |
Correct |
65 ms |
1000 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
4 ms |
740 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
58 ms |
944 KB |
Output is correct |
20 |
Correct |
58 ms |
944 KB |
Output is correct |
21 |
Correct |
58 ms |
836 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
83 ms |
972 KB |
Output is correct |
25 |
Correct |
81 ms |
972 KB |
Output is correct |
26 |
Correct |
97 ms |
1228 KB |
Output is correct |
27 |
Correct |
40 ms |
896 KB |
Output is correct |
28 |
Correct |
74 ms |
972 KB |
Output is correct |
29 |
Correct |
108 ms |
1100 KB |
Output is correct |
30 |
Correct |
53 ms |
992 KB |
Output is correct |
31 |
Correct |
55 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
5 ms |
772 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
5 ms |
716 KB |
Output is correct |
12 |
Correct |
133 ms |
1228 KB |
Output is correct |
13 |
Correct |
130 ms |
1212 KB |
Output is correct |
14 |
Correct |
65 ms |
1004 KB |
Output is correct |
15 |
Correct |
65 ms |
1000 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
4 ms |
740 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
58 ms |
944 KB |
Output is correct |
20 |
Correct |
58 ms |
944 KB |
Output is correct |
21 |
Correct |
58 ms |
836 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
83 ms |
972 KB |
Output is correct |
25 |
Correct |
81 ms |
972 KB |
Output is correct |
26 |
Correct |
97 ms |
1228 KB |
Output is correct |
27 |
Correct |
40 ms |
896 KB |
Output is correct |
28 |
Correct |
74 ms |
972 KB |
Output is correct |
29 |
Correct |
108 ms |
1100 KB |
Output is correct |
30 |
Correct |
53 ms |
992 KB |
Output is correct |
31 |
Correct |
55 ms |
972 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Runtime error |
4 ms |
1484 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 |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
5 ms |
772 KB |
Output is correct |
6 |
Correct |
4 ms |
716 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
5 ms |
716 KB |
Output is correct |
12 |
Correct |
133 ms |
1228 KB |
Output is correct |
13 |
Correct |
130 ms |
1212 KB |
Output is correct |
14 |
Correct |
65 ms |
1004 KB |
Output is correct |
15 |
Correct |
65 ms |
1000 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
4 ms |
740 KB |
Output is correct |
18 |
Correct |
3 ms |
716 KB |
Output is correct |
19 |
Correct |
58 ms |
944 KB |
Output is correct |
20 |
Correct |
58 ms |
944 KB |
Output is correct |
21 |
Correct |
58 ms |
836 KB |
Output is correct |
22 |
Correct |
2 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
83 ms |
972 KB |
Output is correct |
25 |
Correct |
81 ms |
972 KB |
Output is correct |
26 |
Correct |
97 ms |
1228 KB |
Output is correct |
27 |
Correct |
40 ms |
896 KB |
Output is correct |
28 |
Correct |
74 ms |
972 KB |
Output is correct |
29 |
Correct |
108 ms |
1100 KB |
Output is correct |
30 |
Correct |
53 ms |
992 KB |
Output is correct |
31 |
Correct |
55 ms |
972 KB |
Output is correct |
32 |
Correct |
4 ms |
716 KB |
Output is correct |
33 |
Runtime error |
4 ms |
1484 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |