This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 5015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
struct lisan{
vector<int>vt;
void in(int x){ vt.push_back(x); }
void build(){
sort(vt.begin(),vt.end());
vt.resize(unique(vt.begin(),vt.end())-vt.begin());
}
int find(int x){ return upper_bound(vt.begin(),vt.end(),x)-vt.begin(); }
}uni;
int dp[N][N],a[N],h[N],c[N],n;
bool vis[N][N];
vector<int>g[N];
int DP(int i,int j){
if (vis[i][j]) return dp[i][j];
vis[i][j]=1;
dp[i][j]=1e18;
int case1=1e18,case2=c[i];
if (h[i]>=j){
case1=0;
for (auto nxt:g[i]){
if (nxt!=i){
case1+=DP(nxt,h[i]);
}
}
}
for (auto nxt:g[i]){
if (nxt!=i){
case2+=DP(nxt,j);
}
}
dp[i][j]=min(case1,case2);
return dp[i][j];
}
signed main(){
fast
cin>>n;
for (int i=1;i<=n;i++){
cin>>a[i]>>h[i]>>c[i];
g[a[i]].push_back(i);
uni.in(h[i]);
}
uni.build();
for (int i=1;i<=n;i++)
h[i]=uni.find(h[i]);
int ans=0;
cout<<DP(1,0)<<'\n';
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:61:9: warning: unused variable 'ans' [-Wunused-variable]
61 | int ans=0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |