This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
    std::cerr << "\033[1;32m[ " << s << " ] = [ ";
    for (auto it = L; it != R; ++it)
        std::cerr << *it << ' ';
    std::cerr << "]\033[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
 
using namespace std;
using ll = int64_t;
const int maxn = 200025, maxm = 10000;
 
int H[maxn], C[maxn];
vector<int> g[maxn];
map<int,ll,greater<>> sub[maxn];
int cyc[maxn];
void join(int a, int b) {
    if (sub[a].size() > sub[b].size())
        swap(sub[a], sub[b]);
    for (auto [h, val]: sub[a])
        sub[b][h] += val;
}
void dfs(int i) {
    for (int j: g[i]) {
        dfs(j);
        join(j, i);
    }
    sub[i][H[i]] += C[i];
    auto it = sub[i].upper_bound(H[i]);
    ll v = C[i];
    while (it != sub[i].end()) {
        if (it->second < v) {
            v -= it->second;
            sub[i].erase(it++);
        } else {
            it->second -= v;
            break;
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    ll tot = 0;
    vector<int> A(n);
    vector<int> indeg(n);
    for (int i = 0; i < n; i++) {
        cin >> A[i] >> H[i] >> C[i];
        tot += C[i];
        --A[i];
        ++indeg[A[i]];
    }
    for (int i = 0; i < n; i++) cyc[i] = 1;
    queue<int> que;
    for (int i = 0; i < n; i++) if (indeg[i] == 0) que.push(i), cyc[i] = 0;
    while (!que.empty()) {
        int i = que.front(); que.pop();
        int j = A[i];
        g[j].push_back(i);
        if (--indeg[j] == 0)
            que.push(j), cyc[j] = 0;
    }
    ll ans = 0;
    for (int i = 0, tot = 0; i < n; i++) if (cyc[i] == 1) {
        vector<int> cycle;
        cycle.emplace_back(i);
        cyc[i] = -1;
        for (int x = A[i]; x != i; x = A[x]) {
            cycle.emplace_back(x);
            cyc[x] = -1;
        }
        sort(all(cycle), [&](int a, int b){ return H[a] < H[b]; });
        int last = -1;
        for (int x: cycle) {
            if (x != cycle[0]) {
                for (int y: g[x]) {
                    g[cycle[0]].push_back(y);
                }
                g[x].clear();
                g[x].push_back(last);
            }
            last = x;
        }
        dfs(cycle.back());
        for (auto [h, val]: sub[cycle.back()]) ans += val;
    }
    cout << tot - ans << '\n';
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'void join(int, int)':
worst_reporter2.cpp:34:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |     for (auto [h, val]: sub[a])
      |               ^
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:100:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  100 |         for (auto [h, val]: sub[cycle.back()]) ans += val;
      |                   ^
worst_reporter2.cpp:79:21: warning: unused variable 'tot' [-Wunused-variable]
   79 |     for (int i = 0, tot = 0; i < n; i++) if (cyc[i] == 1) {
      |                     ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |