Submission #1091799

#TimeUsernameProblemLanguageResultExecution timeMemory
1091799hahahahaWorst Reporter 4 (JOI21_worst_reporter4)C++17
79 / 100
319 ms109136 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200200;

int n;

int p[N];
int h[N];
int c[N];

map<int, ll> mp[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> p[i] >> h[i] >> c[i];

    for (int i = 1; i <= n; i++)
        mp[i][0] = 0;

    auto join = [&](int u, int v) {
        if ((int)mp[u].size() < (int)mp[v].size()) swap(mp[u], mp[v]);
        for (auto c : mp[v])
            mp[u][c.first] += c.second;
    };

    for (int v = n; v >= 1; v--) {
        int s = c[v];
        auto it = mp[v].lower_bound(h[v]);
        while (it != mp[v].begin()) {
            --it;
            if (s >= it->second) {
                s -= it->second;
                it=mp[v].erase(it);
                if(it==mp[v].begin())break;
            } else {
                mp[v][it->first] = it->second - s;
                s = 0;
                break;
            }
        }
        mp[v][0] += c[v] - s;
        mp[v][h[v]] += c[v];

        if (v != 1) join(p[v], v);
    }

    cout << mp[1][0];
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...