#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N =5005;
int H[N];
int C[N];
int sz[N];
vector<int> side[N];
ll dp[N][N];
bool vis[N];
int getsize(int cur){
sz[cur] =1;
for(auto i : side[cur]){
sz[cur]+=getsize(i);
}
return sz[cur];
}
int n;
void get(int cur){
vis[cur]=1;
dp[cur][n+1]=1e18;
for(auto i : side[cur]){
get(i);
for(int k=1;k<=n;k++){
dp[cur][k]+=dp[i][k];
}
}
for(int k=n;k>=1;k--){
if(k!=H[cur]) dp[cur][k]+=C[cur];
dp[cur][k] = min(dp[cur][k+1],dp[cur][k]);
}
return ;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
vector<int> d;
for(int i=1;i<=n;i++){
int a;
cin>>a>>H[i]>>C[i];
d.push_back(H[i]);
if(a!=i) side[a].push_back(i);
}
sort(d.begin(),d.end());
d.resize(unique(d.begin(),d.end())-d.begin());
for(int i=1;i<=n;i++){
H[i] = lower_bound(d.begin(),d.end(),H[i])-d.begin()+1;
}
ll ans=0;
for(int i=1;i<=n;i++){
if(vis[i]) continue;
getsize(i);
get(i);
ans+=dp[i][1];
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
137 ms |
196496 KB |
Output is correct |
6 |
Correct |
83 ms |
196816 KB |
Output is correct |
7 |
Correct |
81 ms |
196452 KB |
Output is correct |
8 |
Correct |
84 ms |
196692 KB |
Output is correct |
9 |
Correct |
82 ms |
196692 KB |
Output is correct |
10 |
Correct |
85 ms |
196692 KB |
Output is correct |
11 |
Correct |
85 ms |
196680 KB |
Output is correct |
12 |
Correct |
85 ms |
197204 KB |
Output is correct |
13 |
Correct |
89 ms |
197208 KB |
Output is correct |
14 |
Correct |
84 ms |
197000 KB |
Output is correct |
15 |
Correct |
83 ms |
196944 KB |
Output is correct |
16 |
Correct |
81 ms |
196752 KB |
Output is correct |
17 |
Correct |
77 ms |
196688 KB |
Output is correct |
18 |
Correct |
77 ms |
196688 KB |
Output is correct |
19 |
Correct |
109 ms |
196696 KB |
Output is correct |
20 |
Correct |
113 ms |
196880 KB |
Output is correct |
21 |
Correct |
108 ms |
196944 KB |
Output is correct |
22 |
Correct |
77 ms |
196564 KB |
Output is correct |
23 |
Correct |
77 ms |
196552 KB |
Output is correct |
24 |
Correct |
83 ms |
196832 KB |
Output is correct |
25 |
Correct |
80 ms |
196948 KB |
Output is correct |
26 |
Correct |
85 ms |
197084 KB |
Output is correct |
27 |
Correct |
81 ms |
196672 KB |
Output is correct |
28 |
Correct |
82 ms |
196944 KB |
Output is correct |
29 |
Correct |
85 ms |
196908 KB |
Output is correct |
30 |
Correct |
88 ms |
196840 KB |
Output is correct |
31 |
Correct |
84 ms |
196944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
137 ms |
196496 KB |
Output is correct |
6 |
Correct |
83 ms |
196816 KB |
Output is correct |
7 |
Correct |
81 ms |
196452 KB |
Output is correct |
8 |
Correct |
84 ms |
196692 KB |
Output is correct |
9 |
Correct |
82 ms |
196692 KB |
Output is correct |
10 |
Correct |
85 ms |
196692 KB |
Output is correct |
11 |
Correct |
85 ms |
196680 KB |
Output is correct |
12 |
Correct |
85 ms |
197204 KB |
Output is correct |
13 |
Correct |
89 ms |
197208 KB |
Output is correct |
14 |
Correct |
84 ms |
197000 KB |
Output is correct |
15 |
Correct |
83 ms |
196944 KB |
Output is correct |
16 |
Correct |
81 ms |
196752 KB |
Output is correct |
17 |
Correct |
77 ms |
196688 KB |
Output is correct |
18 |
Correct |
77 ms |
196688 KB |
Output is correct |
19 |
Correct |
109 ms |
196696 KB |
Output is correct |
20 |
Correct |
113 ms |
196880 KB |
Output is correct |
21 |
Correct |
108 ms |
196944 KB |
Output is correct |
22 |
Correct |
77 ms |
196564 KB |
Output is correct |
23 |
Correct |
77 ms |
196552 KB |
Output is correct |
24 |
Correct |
83 ms |
196832 KB |
Output is correct |
25 |
Correct |
80 ms |
196948 KB |
Output is correct |
26 |
Correct |
85 ms |
197084 KB |
Output is correct |
27 |
Correct |
81 ms |
196672 KB |
Output is correct |
28 |
Correct |
82 ms |
196944 KB |
Output is correct |
29 |
Correct |
85 ms |
196908 KB |
Output is correct |
30 |
Correct |
88 ms |
196840 KB |
Output is correct |
31 |
Correct |
84 ms |
196944 KB |
Output is correct |
32 |
Correct |
83 ms |
196944 KB |
Output is correct |
33 |
Runtime error |
6 ms |
1368 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
137 ms |
196496 KB |
Output is correct |
6 |
Correct |
83 ms |
196816 KB |
Output is correct |
7 |
Correct |
81 ms |
196452 KB |
Output is correct |
8 |
Correct |
84 ms |
196692 KB |
Output is correct |
9 |
Correct |
82 ms |
196692 KB |
Output is correct |
10 |
Correct |
85 ms |
196692 KB |
Output is correct |
11 |
Correct |
85 ms |
196680 KB |
Output is correct |
12 |
Correct |
85 ms |
197204 KB |
Output is correct |
13 |
Correct |
89 ms |
197208 KB |
Output is correct |
14 |
Correct |
84 ms |
197000 KB |
Output is correct |
15 |
Correct |
83 ms |
196944 KB |
Output is correct |
16 |
Correct |
81 ms |
196752 KB |
Output is correct |
17 |
Correct |
77 ms |
196688 KB |
Output is correct |
18 |
Correct |
77 ms |
196688 KB |
Output is correct |
19 |
Correct |
109 ms |
196696 KB |
Output is correct |
20 |
Correct |
113 ms |
196880 KB |
Output is correct |
21 |
Correct |
108 ms |
196944 KB |
Output is correct |
22 |
Correct |
77 ms |
196564 KB |
Output is correct |
23 |
Correct |
77 ms |
196552 KB |
Output is correct |
24 |
Correct |
83 ms |
196832 KB |
Output is correct |
25 |
Correct |
80 ms |
196948 KB |
Output is correct |
26 |
Correct |
85 ms |
197084 KB |
Output is correct |
27 |
Correct |
81 ms |
196672 KB |
Output is correct |
28 |
Correct |
82 ms |
196944 KB |
Output is correct |
29 |
Correct |
85 ms |
196908 KB |
Output is correct |
30 |
Correct |
88 ms |
196840 KB |
Output is correct |
31 |
Correct |
84 ms |
196944 KB |
Output is correct |
32 |
Correct |
83 ms |
196944 KB |
Output is correct |
33 |
Runtime error |
6 ms |
1368 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |