Submission #1197856

#TimeUsernameProblemLanguageResultExecution timeMemory
1197856mychecksedadWorst Reporter 4 (JOI21_worst_reporter4)C++20
14 / 100
166 ms200820 KiB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 5000+100, M = 1e5+10, K = 52, MX = 30;


int n;
vi g[N];
array<ll, 2> a[N];
ll dp[N][N];
void dfs(int v){
  for(int i = 1; i <= n; ++i) dp[v][i] = (i == a[v][0] ? 0 : a[v][1]);
  for(int u: g[v]){
    dfs(u);
    for(int i = 1; i <= n; ++i) dp[v][i] += dp[u][i];
  } 
  for(int i = n-1; i >= 1; --i) dp[v][i] = min(dp[v][i], dp[v][i + 1]);
}

void solve(){
  cin >> n;
  vi val;
  for(int i = 1; i <= n; ++i){
    int x; cin >> x;
    if(x < i)
      g[x].pb(i);
    cin >> a[i][0] >> a[i][1];
    val.pb(a[i][0]);
  }
  sort(all(val));
  val.erase(unique(all(val)), val.end());
  for(int i = 1; i <= n; ++i){
    a[i][0] = lower_bound(all(val), a[i][0]) - val.begin() + 1;
  }
  dfs(1);
  cout << dp[1][1];
}



int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  while(tt--){
    solve();
    en;
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...