Submission #962646

#TimeUsernameProblemLanguageResultExecution timeMemory
962646CookieWorst Reporter 4 (JOI21_worst_reporter4)C++14
100 / 100
301 ms102272 KiB
#include<bits/stdc++.h> #include<fstream> using namespace std; #define sz(a) (int)a.size() #define ALL(v) v.begin(), v.end() #define ALLR(v) v.rbegin(), v.rend() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> #define mpp make_pair const ld PI = 3.14159265359, prec = 1e-9;; //using u128 = __uint128_t; //const int x[4] = {1, 0, -1, 0}; //const int y[4] = {0, -1, 0, 1}; const ll mod = 1e9 + 7, pr = 31; const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1; //const int base = (1 <<18); const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // have fun! int n; vt<int>adj[mxn + 1]; ll to[mxn + 1], h[mxn + 1], c[mxn + 1]; bool cyc[mxn + 1]; bool vis[mxn + 1]; map<ll, ll>mp[mxn + 1], cost; void dfs(int s, int pre){ vis[s] = 1; for(auto i: adj[s]){ if(i != pre && (!s || !cyc[i])){ dfs(i, s); if(sz(mp[i]) > sz(mp[s])){ swap(mp[s], mp[i]); } for(auto j: mp[i])mp[s][j.fi] += j.se; } } if(pre){ mp[s][h[s]] += c[s]; ll lft = c[s]; while(1){ auto it = mp[s].lower_bound(h[s]); if(it == mp[s].begin())break; --it; if((*it).se <= lft){ lft -= (*it).se; mp[s].erase(it); }else{ (*it).se -= lft; break; } } } } void solve(){ cin >> n; ll totc = 0; for(int i = 1; i <= n; i++){ cin >> to[i] >> h[i] >> c[i]; totc += c[i]; adj[to[i]].pb(i); } ll res = 0; for(int i = 1; i <= n; i++){ if(!vis[i]){ int a = to[i], b = to[to[i]]; while(a != b){ a = to[a]; b = to[to[b]]; } a = i; while(a != b){ a = to[a]; b = to[b]; } cyc[a] = 1; b = to[a]; adj[0].clear(); mp[0].clear(); cost.clear(); adj[0].pb(a); cost[h[a]] += c[a]; while(a != b){ cyc[b] = 1; adj[0].pb(b); cost[h[b]] += c[b]; b = to[b]; } dfs(0, -1); ll tot = 0, ans = 0; for(auto j: mp[0]){ tot += j.se; mp[0][j.fi] = tot; } ans = tot; for(auto j: cost){ auto it = mp[0].lower_bound(j.fi); if(it != mp[0].begin()){ --it; ans = max(ans, tot + (j.se - (*it).se)); } } res += ans; } } cout << totc - res; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("DATA.inp", "r", stdin); //freopen("DATA.out", "w", stdout); int tt; tt = 1; while(tt--){ solve(); } return(0); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...