제출 #973751

#제출 시각아이디문제언어결과실행 시간메모리
973751phoenixWorst Reporter 4 (JOI21_worst_reporter4)C++17
79 / 100
338 ms109412 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].upper_bound(h[v]);
        while (it != mp[v].begin()) {
            it--;
            if (s >= it->second) {
                s -= it->second;
                mp[v].erase(it);
            } else {
                mp[v][it->first] = it->second - s;
                s = 0;
                break;
            }
            it = mp[v].upper_bound(h[v]);
        }
        mp[v][0] += c[v] - s;
        mp[v][h[v] + 1] += c[v]; 
        // cerr << "vertex " << v << ": ";
        // cerr << '{';
        // for (auto c : mp[v]) {
        //     cout << '(' << c.first << ' ' << c.second << ") ";
        // }
        // cout << "}\n";
        
        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...