#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int mxN = (int)2e5+10;
const int LINF = (int)2e18;
int n;
unordered_map<int,int> dp[mxN];
int a[mxN], h[mxN], c[mxN];
vector<int> adj[mxN];
set<int> S[mxN];
int recur(int s, int v){
if(dp[s].count(v)) return dp[s][v];
int ans = (v!=h[s])*c[s];
S[s].insert(h[s]);
for(auto u : adj[s]){
ans+=recur(u,v);
if(sz(S[s])<sz(S[u])) S[s].swap(S[u]);
for(auto u : S[u]) if(u>v) S[s].insert(u);
}
for(auto u : S[s]) ans = min(ans, recur(s,u));
return dp[s][v]=ans;
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> h[i] >> c[i];
if(i>1) adj[a[i]].pb(i);
}
cout << recur(1,*min_element(h+1,h+n+1));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |