제출 #418085

#제출 시각아이디문제언어결과실행 시간메모리
418085lycWorst Reporter 4 (JOI21_worst_reporter4)C++14
14 / 100
127 ms197080 KiB
#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], V;
vector<int> al[mxN], vals;

ll dp[mxN][mxN];

void dfs(int u) {
    FOR(x,0,V-1) dp[u][x] = 0;

    for (int v : al[u]) {
        dfs(v);

        FOR(x,0,V-1){
            ll cur = dp[v][x] + C[v];
            if (x <= H[v]) cur = min(cur, dp[v][H[v]]);
            dp[u][x] += cur;
        }
    }

    //cout << u << ": ";
    //FOR(x,0,V-1){
    //    cout << dp[u][x] << ' ';
    //}
    //cout << endl;
}

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);
        vals.push_back(H[i]);
    }

    vals.push_back(1);
    sort(ALL(vals));
    vals.resize(unique(ALL(vals))-vals.begin());
    FOR(i,1,N){
        H[i] = lower_bound(ALL(vals),H[i])-vals.begin();
    }
    V = SZ(vals);

    dfs(1);
    cout << min(dp[1][0] + C[1], dp[1][H[1]]) << '\n';
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...