Submission #1270081

#TimeUsernameProblemLanguageResultExecution timeMemory
1270081Born_To_LaughMagic Tree (CEOI19_magictree)C++17
0 / 100
40 ms17216 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 1e9 + 7;
const int maxn = 1e5 + 10;
map<int,int> dp[maxn];
vector<int> adj[maxn];
int d[maxn], w[maxn];
int n, m, k;
void dfs(int a, int par){
    for(auto &elm: adj[a]){
        if(elm == par)continue;
        dfs(elm, a);
        if(dp[a].size() < dp[elm].size()) swap(dp[a], dp[elm]);
        for(auto &sth: dp[elm]){
            dp[a][sth.fi] += sth.se;
        }
    }
    if(!d[a])return;
    dp[a][d[a]] += w[a];
    auto it = dp[a].upper_bound(d[a]);
    while(it != dp[a].end()){
        if((*it).se <= w[a]){
            w[a] -= (*it).se;
            dp[a].erase(it);
            it = next(it);
        }
        else{
            (*it).se -= w[a];
            break;
        }
    }
}
void solve(){
    cin >> n >> m >> k;
    for(int i=2; i<=n; ++i){
        int x;cin >> x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }
    for(int i=1; i<=m; ++i){
        int a, b, c;cin >> a >> b >> c;
        d[a] = b;
        w[a] = c;
    }
    dfs(1, -1);
    ll ans = 0;
    for(auto &elm: dp[1]){
        ans += elm.se;
    }
    cout << ans << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#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...