#include <bits/stdc++.h>
#define int ll
#undef BALBIT
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<ll, ll>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 2e5+5;
int C[maxn], H[maxn];
// BIT
struct BIT{
vector<ll> s;
int MXN;
ll QU(int e) {ll re = 0; for (++e; e>0; e-=e&-e) re += s[e]; return re;}
void MO(int e, ll v) {for (++e; e<MXN; e+=e&-e) s[e] += v;}
BIT(int ss){
s.resize(ss+1,0); MXN = ss+1;
}
BIT(){}
};
vector<int> g[maxn];
int to[maxn];
bool oncyc[maxn];
ll curval[maxn];
int fa[18][maxn];
int inid[maxn], outid[maxn]; // get(i) = inbit(inid[i]) - outbit(outid[i])
int inpos[maxn], outpos[maxn]; // where to update
vector<int> inord;
vector<int> outord;
BIT inbit, outbit;
void dfs(int v, int p) {
if (p == -1) fa[0][v] = v;
// bug(v,p);
inord.pb(v);
inid[v] = SZ(inord)-1;
outid[v] = SZ(outord)-1;
inpos[v] = SZ(inord)-1;
for (int u : g[v]) {
if (!oncyc[u] && u != p) {
fa[0][u] = v;
dfs(u,v);
}
}
outord.pb(v);
outpos[v] = SZ(outord)-1;
}
ll get(int v) {
// get prefix sum!!
return inbit.QU(inid[v]) - outbit.QU(outid[v]);
}
void upd(int v, ll w) {
curval[v] += w;
inbit.MO(inpos[v], w);
outbit.MO(outpos[v], w);
}
int nxtset(int v) { // lowest (strict) ancestor of v that is set
if (oncyc[v]) return -1;
v = fa[0][v];
int gv = get(v);
for (int j = 18-1; j>=0; --j) {
if (gv - get(fa[j][v]) == 0) {
v = fa[j][v];
}
}
if (curval[v] == 0) {
assert(oncyc[v]); return -1;
}
return v;
}
vector<pair<int, pii> > ha;
void run(int v) {
inord.clear(); outord.clear();
dfs(v, -1);
for( int e : inord) curval[e] = 0;
for (int j = 1; j<18; ++j) {
for (int tmp : inord){
fa[j][tmp] = fa[j-1][fa[j-1][tmp]];
}
}
int sz = SZ(inord);
inbit = BIT(sz);
outbit = BIT(sz);
vector<pii> run;
for (int x : inord) {
run.pb({H[x], x});
}
sort(ALL(run), [&](pii a, pii b){return a.f != b.f? a.f > b.f : inid[a.s] > inid[b.s];});
ll re = 0;
for (pii p : run) {
int x = p.s;
int y = nxtset(x);
ll tochg = C[x];
upd(x, tochg);
bug(x,y,"plus", tochg);
re += tochg;
while (y != -1) {
int yo = min(tochg, curval[y]);
upd(y, -yo); tochg -= yo;
bug(x,y,-yo);
re -= yo;
if (curval[y] > 0 || tochg == 0) break;
y = nxtset(y);
}
bug(re);
if (SZ(ha) && ha.back().f==H[x] && ha.back().s.f == v) ha.pop_back();
ha.pb({H[x], {v,re}});
if (x == v) {
upd(x, -C[x]); re -= C[x];
ha.pb({H[x]+1, {v,re}});
}
}
// return {re, re2};
}
bool done[maxn], seen[maxn];
void clr(int v) {
done[v] = 1;
for (int u : g[v]) {
if (!done[u]) {
clr(u);
}
}
}
int tmpnow[maxn];
ll getans(int v) {
assert(!done[v]);
bug("working", v);
clr(v);
int at = v;
vector<int> walk;
while (!seen[at]) {
walk.pb(at);
seen[at] = 1;
at = to[at];
}
vector<int> cyc;
while (walk.back() != at) {
cyc.pb(walk.back()); walk.pop_back();
}
cyc.pb(at);
#ifdef BALBIT
bug(SZ(cyc));
for (int e : cyc) cout<<e<<' ';
cout<<endl;
#endif // BALBIT
// case 1: make everything the minimum
const ll BIG = 0;
ll re = 0;
ha.clear();
for (int t : cyc) oncyc[t] = 1;
for (int t : cyc) {
run(t);
// ans1 += run(t) - BIG;
}
sort(ALL(ha));
ll ans = 0;
REP(i, SZ(ha)) {
auto e = ha[i].s;
re -= tmpnow[e.f];
tmpnow[e.f] = e.s;
re += tmpnow[e.f];
if (i+1==SZ(ha) || ha[i+1].f != ha[i].f) {
bug(re);
MX(ans, re);
}
}
// bug(v, ans1, ans2);
// for (int t : cyc) {
// C[t] = 0;
// ans2 += run(t);
// }
//
// bug(ans2);
return ans;
}
signed main(){
IOS();
int n; cin>>n;
ll totc = 0;
REP(i,n) {
cin>>to[i]; --to[i];
g[i].pb(to[i]); g[to[i]].pb(i);
cin>>H[i]>>C[i];
totc += C[i];
}
ll re = 0;
REP(i,n) {
if (!done[i]) {
re += getans(i);
}
}
bug(totc, re);
cout<<totc - re<<endl;
}
/*
4
2 3 2
3 3 1
4 1 2
1 1 2
*/
Compilation message
worst_reporter2.cpp: In function 'long long int getans(long long int)':
worst_reporter2.cpp:229:14: warning: unused variable 'BIG' [-Wunused-variable]
229 | const ll BIG = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5164 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5164 KB |
Output is correct |
5 |
Correct |
8 ms |
7064 KB |
Output is correct |
6 |
Correct |
9 ms |
6868 KB |
Output is correct |
7 |
Correct |
8 ms |
6848 KB |
Output is correct |
8 |
Correct |
9 ms |
7064 KB |
Output is correct |
9 |
Correct |
9 ms |
6840 KB |
Output is correct |
10 |
Correct |
9 ms |
6844 KB |
Output is correct |
11 |
Correct |
7 ms |
6868 KB |
Output is correct |
12 |
Correct |
11 ms |
7380 KB |
Output is correct |
13 |
Correct |
10 ms |
7124 KB |
Output is correct |
14 |
Correct |
12 ms |
7200 KB |
Output is correct |
15 |
Correct |
13 ms |
6996 KB |
Output is correct |
16 |
Correct |
9 ms |
7064 KB |
Output is correct |
17 |
Correct |
8 ms |
6868 KB |
Output is correct |
18 |
Correct |
6 ms |
6844 KB |
Output is correct |
19 |
Correct |
9 ms |
7252 KB |
Output is correct |
20 |
Correct |
9 ms |
6996 KB |
Output is correct |
21 |
Correct |
8 ms |
6996 KB |
Output is correct |
22 |
Correct |
6 ms |
6996 KB |
Output is correct |
23 |
Correct |
8 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
7252 KB |
Output is correct |
25 |
Correct |
9 ms |
6996 KB |
Output is correct |
26 |
Correct |
7 ms |
7380 KB |
Output is correct |
27 |
Correct |
8 ms |
7124 KB |
Output is correct |
28 |
Correct |
8 ms |
7252 KB |
Output is correct |
29 |
Correct |
8 ms |
7252 KB |
Output is correct |
30 |
Correct |
9 ms |
7228 KB |
Output is correct |
31 |
Correct |
9 ms |
7208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5164 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5164 KB |
Output is correct |
5 |
Correct |
8 ms |
7064 KB |
Output is correct |
6 |
Correct |
9 ms |
6868 KB |
Output is correct |
7 |
Correct |
8 ms |
6848 KB |
Output is correct |
8 |
Correct |
9 ms |
7064 KB |
Output is correct |
9 |
Correct |
9 ms |
6840 KB |
Output is correct |
10 |
Correct |
9 ms |
6844 KB |
Output is correct |
11 |
Correct |
7 ms |
6868 KB |
Output is correct |
12 |
Correct |
11 ms |
7380 KB |
Output is correct |
13 |
Correct |
10 ms |
7124 KB |
Output is correct |
14 |
Correct |
12 ms |
7200 KB |
Output is correct |
15 |
Correct |
13 ms |
6996 KB |
Output is correct |
16 |
Correct |
9 ms |
7064 KB |
Output is correct |
17 |
Correct |
8 ms |
6868 KB |
Output is correct |
18 |
Correct |
6 ms |
6844 KB |
Output is correct |
19 |
Correct |
9 ms |
7252 KB |
Output is correct |
20 |
Correct |
9 ms |
6996 KB |
Output is correct |
21 |
Correct |
8 ms |
6996 KB |
Output is correct |
22 |
Correct |
6 ms |
6996 KB |
Output is correct |
23 |
Correct |
8 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
7252 KB |
Output is correct |
25 |
Correct |
9 ms |
6996 KB |
Output is correct |
26 |
Correct |
7 ms |
7380 KB |
Output is correct |
27 |
Correct |
8 ms |
7124 KB |
Output is correct |
28 |
Correct |
8 ms |
7252 KB |
Output is correct |
29 |
Correct |
8 ms |
7252 KB |
Output is correct |
30 |
Correct |
9 ms |
7228 KB |
Output is correct |
31 |
Correct |
9 ms |
7208 KB |
Output is correct |
32 |
Correct |
9 ms |
7072 KB |
Output is correct |
33 |
Correct |
551 ms |
73944 KB |
Output is correct |
34 |
Correct |
539 ms |
65824 KB |
Output is correct |
35 |
Correct |
542 ms |
74000 KB |
Output is correct |
36 |
Correct |
524 ms |
65756 KB |
Output is correct |
37 |
Correct |
450 ms |
65752 KB |
Output is correct |
38 |
Correct |
290 ms |
65756 KB |
Output is correct |
39 |
Correct |
1180 ms |
85284 KB |
Output is correct |
40 |
Correct |
1138 ms |
77444 KB |
Output is correct |
41 |
Correct |
375 ms |
77416 KB |
Output is correct |
42 |
Correct |
1228 ms |
79292 KB |
Output is correct |
43 |
Correct |
1182 ms |
71272 KB |
Output is correct |
44 |
Correct |
512 ms |
74720 KB |
Output is correct |
45 |
Correct |
488 ms |
66476 KB |
Output is correct |
46 |
Correct |
157 ms |
66528 KB |
Output is correct |
47 |
Correct |
603 ms |
80960 KB |
Output is correct |
48 |
Correct |
611 ms |
72776 KB |
Output is correct |
49 |
Correct |
293 ms |
72760 KB |
Output is correct |
50 |
Correct |
201 ms |
75184 KB |
Output is correct |
51 |
Correct |
192 ms |
67000 KB |
Output is correct |
52 |
Correct |
515 ms |
80156 KB |
Output is correct |
53 |
Correct |
533 ms |
71964 KB |
Output is correct |
54 |
Correct |
255 ms |
85584 KB |
Output is correct |
55 |
Correct |
420 ms |
80656 KB |
Output is correct |
56 |
Correct |
447 ms |
83376 KB |
Output is correct |
57 |
Correct |
455 ms |
84144 KB |
Output is correct |
58 |
Correct |
434 ms |
79376 KB |
Output is correct |
59 |
Correct |
437 ms |
79456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5164 KB |
Output is correct |
3 |
Correct |
3 ms |
5204 KB |
Output is correct |
4 |
Correct |
3 ms |
5164 KB |
Output is correct |
5 |
Correct |
8 ms |
7064 KB |
Output is correct |
6 |
Correct |
9 ms |
6868 KB |
Output is correct |
7 |
Correct |
8 ms |
6848 KB |
Output is correct |
8 |
Correct |
9 ms |
7064 KB |
Output is correct |
9 |
Correct |
9 ms |
6840 KB |
Output is correct |
10 |
Correct |
9 ms |
6844 KB |
Output is correct |
11 |
Correct |
7 ms |
6868 KB |
Output is correct |
12 |
Correct |
11 ms |
7380 KB |
Output is correct |
13 |
Correct |
10 ms |
7124 KB |
Output is correct |
14 |
Correct |
12 ms |
7200 KB |
Output is correct |
15 |
Correct |
13 ms |
6996 KB |
Output is correct |
16 |
Correct |
9 ms |
7064 KB |
Output is correct |
17 |
Correct |
8 ms |
6868 KB |
Output is correct |
18 |
Correct |
6 ms |
6844 KB |
Output is correct |
19 |
Correct |
9 ms |
7252 KB |
Output is correct |
20 |
Correct |
9 ms |
6996 KB |
Output is correct |
21 |
Correct |
8 ms |
6996 KB |
Output is correct |
22 |
Correct |
6 ms |
6996 KB |
Output is correct |
23 |
Correct |
8 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
7252 KB |
Output is correct |
25 |
Correct |
9 ms |
6996 KB |
Output is correct |
26 |
Correct |
7 ms |
7380 KB |
Output is correct |
27 |
Correct |
8 ms |
7124 KB |
Output is correct |
28 |
Correct |
8 ms |
7252 KB |
Output is correct |
29 |
Correct |
8 ms |
7252 KB |
Output is correct |
30 |
Correct |
9 ms |
7228 KB |
Output is correct |
31 |
Correct |
9 ms |
7208 KB |
Output is correct |
32 |
Correct |
9 ms |
7072 KB |
Output is correct |
33 |
Correct |
551 ms |
73944 KB |
Output is correct |
34 |
Correct |
539 ms |
65824 KB |
Output is correct |
35 |
Correct |
542 ms |
74000 KB |
Output is correct |
36 |
Correct |
524 ms |
65756 KB |
Output is correct |
37 |
Correct |
450 ms |
65752 KB |
Output is correct |
38 |
Correct |
290 ms |
65756 KB |
Output is correct |
39 |
Correct |
1180 ms |
85284 KB |
Output is correct |
40 |
Correct |
1138 ms |
77444 KB |
Output is correct |
41 |
Correct |
375 ms |
77416 KB |
Output is correct |
42 |
Correct |
1228 ms |
79292 KB |
Output is correct |
43 |
Correct |
1182 ms |
71272 KB |
Output is correct |
44 |
Correct |
512 ms |
74720 KB |
Output is correct |
45 |
Correct |
488 ms |
66476 KB |
Output is correct |
46 |
Correct |
157 ms |
66528 KB |
Output is correct |
47 |
Correct |
603 ms |
80960 KB |
Output is correct |
48 |
Correct |
611 ms |
72776 KB |
Output is correct |
49 |
Correct |
293 ms |
72760 KB |
Output is correct |
50 |
Correct |
201 ms |
75184 KB |
Output is correct |
51 |
Correct |
192 ms |
67000 KB |
Output is correct |
52 |
Correct |
515 ms |
80156 KB |
Output is correct |
53 |
Correct |
533 ms |
71964 KB |
Output is correct |
54 |
Correct |
255 ms |
85584 KB |
Output is correct |
55 |
Correct |
420 ms |
80656 KB |
Output is correct |
56 |
Correct |
447 ms |
83376 KB |
Output is correct |
57 |
Correct |
455 ms |
84144 KB |
Output is correct |
58 |
Correct |
434 ms |
79376 KB |
Output is correct |
59 |
Correct |
437 ms |
79456 KB |
Output is correct |
60 |
Correct |
3 ms |
5168 KB |
Output is correct |
61 |
Correct |
3 ms |
5204 KB |
Output is correct |
62 |
Correct |
4 ms |
5164 KB |
Output is correct |
63 |
Correct |
3 ms |
5204 KB |
Output is correct |
64 |
Incorrect |
819 ms |
74952 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |