Submission #542095

#TimeUsernameProblemLanguageResultExecution timeMemory
542095someoneWorst Reporter 4 (JOI21_worst_reporter4)C++14
0 / 100
10 ms5588 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct Arc {
    int nex, pds;
};

const int N = 2e5 + 42, INF = 1e18 + 42;

bool vu[N];
vector<int> adj[N];
int n, h[N], cost[N];

multiset<pair<int, int>> DFS(int i, int pre) {
    vu[i] = true;
    multiset<pair<int, int>> ans;
    ans.insert({0, cost[i]});
    ans.insert({h[i]+1, cost[i]});
    
    for(int j : adj[i])
        if(j != pre) {
            multiset<pair<int, int>> fils = DFS(j, i);
            if(ans.size() < fils.size())
                swap(ans, fils);
            for(auto p : fils)
                ans.insert(p);
        }
    
    int suppr = cost[i];
    while(suppr) {
        auto it = ans.lower_bound({h[i]+1, 0});
        it--;
        if((*it).second <= suppr) {
            suppr -= (*it).second;
            ans.erase(it);
        } else {
            auto copy = (*it);
            ans.erase(it);
            ans.insert({copy.first, copy.second - suppr});
            suppr = 0;
        }
    }
    
    return ans;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n;
    for(int i = 0; i < n; i++) {
        int a;
        cin >> a >> h[i] >> cost[i];
        if(i != a-1)
            adj[a-1].push_back(i);
    }
    
    int ans = 0;
    for(int i = 0; i < n; i++)
        if(!vu[i]) {
            multiset<pair<int, int>> slope = DFS(i, i);
            int x = (*slope.begin()).first;
            for(auto p : slope)
                if(p.first != x) {
                    break;
                } else {
                    ans += p.second;
                }
        }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...