Submission #1324672

#TimeUsernameProblemLanguageResultExecution timeMemory
1324672Zbyszek99Worst Reporter 4 (JOI21_worst_reporter4)C++20
100 / 100
337 ms108608 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

ll H[200001];
ll C[200001];
int nxt[200001];
vi graph[200001];
int odw[200001];
map<int,ll> changes[200001];
bool is_cycle[200001];
vector<vi> cycles;
vi ls;
ll fin_ans = 0;
int n;

void dfs(int v)
{
    ls.pb(v);
    odw[v] = 1;
    if(odw[nxt[v]] == 0) dfs(nxt[v]);
    else
    {
        if(odw[nxt[v]] == 1)
        {
            vi c;
            while(ls.back() != nxt[v])
            {
                c.pb(ls.back());
                ls.pop_back();
            }
            forall(it,c) ls.pb(it);
            c.pb(nxt[v]);
            cycles.pb(c);
        }
    }
    odw[v] = 2;
    ls.pop_back();
}

vi pom;

void merge(int v, int it)
{
    if(siz(changes[v]) < siz(changes[it])) swap(changes[v],changes[it]);
    forall(it2,changes[it]) changes[v][it2.ff]+=it2.ss;
}

void dfs_dp(int v, int pop)
{
    forall(it,graph[v])
    {
        if(it == pop) continue;
        dfs_dp(it,v);
        merge(v,it);
    }
    ll cur_add = 0;
    changes[v][-H[v]] += C[v];
    while(true)
    {
        auto nxt = changes[v].upper_bound(-H[v]);
        if(nxt == changes[v].end()) break;
        if(cur_add+nxt->ss <= C[v])
        {
            cur_add += nxt->ss;
            changes[v].erase(nxt);
        }
        else
        {
            nxt->ss = (cur_add+nxt->ss)-C[v];
            break;
        }
    }
}

void solve_cycle(vi c)
{
    changes[0] = {};
    forall(it,c) is_cycle[it] = 1;
    map<int,ll> Hcosts;
    forall(it,c)
    {
        Hcosts[H[it]] += C[it];
        forall(it2,graph[it])
        {
            if(is_cycle[it2]) continue;
            dfs_dp(it2,it);
            merge(0,it2);
        }
    }
    map<int,ll> vals;
    ll cur = 0;
    forall(it,changes[0])
    {
        cur += it.ss;
        vals[-it.ff] = cur;
    }
    vals[1e9+1] = 0;
    ll ans = 0;
    forall(it,vals) ans = max(ans,it.ss);
    forall(it,Hcosts)
    {
        auto it2 = vals.lower_bound(it.ff);
        ans = max(ans,it.ss+it2->ss);
    }
    fin_ans += ans;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    cin >> n;
    rep2(i,1,n)
    {
        cin >> nxt[i] >> H[i] >> C[i];
        graph[nxt[i]].pb(i);
    }
    rep2(i,1,n) if(odw[i] == 0) dfs(i);
    forall(it,cycles) solve_cycle(it);
    ll ans2 = 0;
    rep2(i,1,n) ans2 += C[i];
    cout << ans2-fin_ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...