Submission #915327

#TimeUsernameProblemLanguageResultExecution timeMemory
915327a_l_i_r_e_z_aMagic Tree (CEOI19_magictree)C++17
100 / 100
113 ms29008 KiB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
int n, m, k; 
map<int, ll> dp[maxn];
pii a[maxn];
vector<int> adj[maxn];

void combine(map<int, ll> &x, map<int, ll> &y){
    if(x.size() < y.size()) swap(x, y);
    for(auto it: y) x[it.F] += it.S;
    y.clear();
}

void dfs(int v = 1){
    dp[v].insert(mp(0, 0));
    for(auto u: adj[v]){
        dfs(u);
        combine(dp[v], dp[u]);
    }
    if(a[v].F){
        ll c = 0;
        dp[v][a[v].F] += a[v].S;
        while(true){
            auto it = dp[v].upper_bound(a[v].F);
            if(it == dp[v].end()) break;
            c += (*it).S;
            if(c >= a[v].S){
                (*it).S = c - a[v].S;
                break;
            }
            dp[v].erase(it);
        }
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n >> m >> k;
    for(int i = 0; i < n - 1; i++){ 
        int p; cin >> p;
        adj[p].pb(i + 2);
    }
    for(int i = 0; i < m; i++){
        int v, d, w; cin >> v >> d >> w;
        a[v] = mp(d, w);
    }
    dfs();
    ll ans = 0;
    for(auto u: dp[1]) ans += u.S;
    cout << ans << '\n';

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...