#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node{
int s,e,m;
pair<long long,pair<int,bool> > val;
pair<long long,int> leaf;
long long lazy;
node *l, *r;
node(int S, int E){
s=S; e=E; m=(s+e)/2;
val={-1e16,{s,0}};
leaf={-1e16,-1};
lazy=0;
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void prop(){
if(s!=e&&lazy){
l->val.first+=lazy;
l->leaf.first+=lazy;
r->leaf.first+=lazy;
l->lazy+=lazy;
r->val.first+=lazy;
r->lazy+=lazy;
lazy=0;
}
}
void update(int S, int E, long long V){
if(S<=s&&e<=E){
val.first+=V;
lazy+=V;
leaf.first+=V;
return;
}
prop();
if(E<=m) l->update(S,E,V);
else if(S>m) r->update(S,E,V);
else l->update(S,m,V),r->update(m+1,E,V);
val=max(l->val,r->val);
leaf=max(l->leaf,r->leaf);
}
void pset(int S, long long V, bool L){
if(s==e){
val.first=V;
val.second.second=L;
if(L) leaf={V,s};
else leaf={-1e16,-1};
return;
}
prop();
if(S<=m) l->pset(S,V,L);
else r->pset(S,V,L);
val=max(l->val,r->val);
leaf=max(l->leaf,r->leaf);
}
} *root;
int n;
long long mon;
vector<pair<int,long long> > adj[300005];
int pre[300005],to[300005],post[300005];
int cur;
void dfs(int x){
cur++;
pre[x]=cur;
to[cur]=x;
for(auto i:adj[x]) dfs(i.first);
post[x]=cur;
}
int done[300005];
pair<int,long long> par[300005];
int pos[300005];
int p[300005];
int find(int x){
if(p[x]==x) return x;
return p[x]=find(p[x]);
}
void merge(int x, int y){
x=find(x); y=find(y);
p[x]=y;
}
long long dp(int x){
long long ret=0;
for(auto i:adj[x]){
ret+=max(dp(i.first)+i.second,0ll);
}
return ret;
}
void take(pair<long long,pair<int,bool> > x){
root->update(1,cur,x.first-mon);
mon=x.first;
int at=x.second.first;
while(!done[at]){
done[at]=1;
root->update(pre[at],post[at],-par[at].second);
root->pset(pre[at],-1e16,0);
at=par[at].first;
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> mon;
for(int i=0; i<=n; i++) p[i]=i;
long long org=mon;
memset(pos,-1,sizeof(pos));
for(int i=1; i<=n; i++){
long long a,b;
cin >> a >> b;
b=find(b);
if(a>=0&&pos[b]!=-1){
merge(i,adj[b].back().first);
par[adj[b].back().first].second+=a;
adj[b].back().second+=a;
}
else{
adj[b].push_back({i,a});
par[i]={b,a};
}
if(pos[b]==-1&&adj[b].back().second>=0) pos[b]=adj[b].back().first;
else if(pos[b]!=-1&&adj[b].back().second<0) swap(adj[b][adj[b].size()-1],adj[b][adj[b].size()-2]);
}
if(mon==1e18){
cout << dp(0);
return 0;
}
dfs(0);
root=new node(0,n+5);
root->pset(pre[0],mon,1);
while(true){
pair<long long,pair<int,bool> > x=root->val;
if(x.first<0) break;
x.second.first=to[x.second.first];
//cout << x.second.first << ' ' << x.first << ' ' << x.second.second <<'\n';
if(x.first>=mon){
take(x);
if(!x.second.second) continue;
}
else if(!x.second.second){
pair<long long,int> l=root->leaf;
x={l.first,{to[l.second],1}};
root->pset(pre[x.second.first],x.first,0);
}
else root->pset(pre[x.second.first],x.first,0);
if(x.first<0) break;
//cout << x.second.first << ' ' << x.first << ' ' << x.second.second <<'\n';
for(auto i:adj[x.second.first]){
root->pset(pre[i.first],i.second+x.first,1);
}
}
cout << mon-org;
}
/*6 1
3 0
-3 1
-5 0
2 1
6 3
-4 5*/
/*10 0
-3 0
1 1
2 1
1 1
2 0
-1 5
2 6*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
26708 KB |
Output is correct |
2 |
Correct |
68 ms |
26704 KB |
Output is correct |
3 |
Correct |
73 ms |
25748 KB |
Output is correct |
4 |
Correct |
56 ms |
29212 KB |
Output is correct |
5 |
Correct |
57 ms |
30804 KB |
Output is correct |
6 |
Correct |
40 ms |
20304 KB |
Output is correct |
7 |
Correct |
63 ms |
26352 KB |
Output is correct |
8 |
Correct |
69 ms |
25652 KB |
Output is correct |
9 |
Correct |
49 ms |
29732 KB |
Output is correct |
10 |
Correct |
51 ms |
30824 KB |
Output is correct |
11 |
Correct |
79 ms |
26588 KB |
Output is correct |
12 |
Correct |
61 ms |
25168 KB |
Output is correct |
13 |
Correct |
66 ms |
26704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15704 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
16220 KB |
Output is correct |
5 |
Correct |
3 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16372 KB |
Output is correct |
7 |
Correct |
4 ms |
16220 KB |
Output is correct |
8 |
Correct |
3 ms |
16220 KB |
Output is correct |
9 |
Correct |
4 ms |
16220 KB |
Output is correct |
10 |
Correct |
3 ms |
16220 KB |
Output is correct |
11 |
Correct |
3 ms |
16220 KB |
Output is correct |
12 |
Correct |
3 ms |
16220 KB |
Output is correct |
13 |
Correct |
4 ms |
16220 KB |
Output is correct |
14 |
Correct |
4 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
16220 KB |
Output is correct |
16 |
Correct |
4 ms |
16364 KB |
Output is correct |
17 |
Correct |
3 ms |
16220 KB |
Output is correct |
18 |
Correct |
4 ms |
16220 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
3 ms |
16220 KB |
Output is correct |
21 |
Correct |
3 ms |
16220 KB |
Output is correct |
22 |
Correct |
4 ms |
16216 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16220 KB |
Output is correct |
25 |
Correct |
3 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16220 KB |
Output is correct |
28 |
Correct |
4 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15704 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
16220 KB |
Output is correct |
5 |
Correct |
3 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16372 KB |
Output is correct |
7 |
Correct |
4 ms |
16220 KB |
Output is correct |
8 |
Correct |
3 ms |
16220 KB |
Output is correct |
9 |
Correct |
4 ms |
16220 KB |
Output is correct |
10 |
Correct |
3 ms |
16220 KB |
Output is correct |
11 |
Correct |
3 ms |
16220 KB |
Output is correct |
12 |
Correct |
3 ms |
16220 KB |
Output is correct |
13 |
Correct |
4 ms |
16220 KB |
Output is correct |
14 |
Correct |
4 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
16220 KB |
Output is correct |
16 |
Correct |
4 ms |
16364 KB |
Output is correct |
17 |
Correct |
3 ms |
16220 KB |
Output is correct |
18 |
Correct |
4 ms |
16220 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
3 ms |
16220 KB |
Output is correct |
21 |
Correct |
3 ms |
16220 KB |
Output is correct |
22 |
Correct |
4 ms |
16216 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16220 KB |
Output is correct |
25 |
Correct |
3 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16220 KB |
Output is correct |
28 |
Correct |
4 ms |
16220 KB |
Output is correct |
29 |
Correct |
431 ms |
91752 KB |
Output is correct |
30 |
Correct |
505 ms |
91340 KB |
Output is correct |
31 |
Correct |
519 ms |
89780 KB |
Output is correct |
32 |
Correct |
251 ms |
100692 KB |
Output is correct |
33 |
Correct |
105 ms |
95500 KB |
Output is correct |
34 |
Correct |
209 ms |
94292 KB |
Output is correct |
35 |
Correct |
94 ms |
56520 KB |
Output is correct |
36 |
Correct |
365 ms |
92100 KB |
Output is correct |
37 |
Correct |
448 ms |
91728 KB |
Output is correct |
38 |
Correct |
240 ms |
100436 KB |
Output is correct |
39 |
Correct |
154 ms |
96384 KB |
Output is correct |
40 |
Correct |
183 ms |
91476 KB |
Output is correct |
41 |
Correct |
98 ms |
91040 KB |
Output is correct |
42 |
Correct |
76 ms |
57548 KB |
Output is correct |
43 |
Correct |
348 ms |
94032 KB |
Output is correct |
44 |
Correct |
148 ms |
98836 KB |
Output is correct |
45 |
Correct |
132 ms |
96340 KB |
Output is correct |
46 |
Correct |
283 ms |
94408 KB |
Output is correct |
47 |
Correct |
372 ms |
91988 KB |
Output is correct |
48 |
Correct |
508 ms |
90556 KB |
Output is correct |
49 |
Correct |
455 ms |
90772 KB |
Output is correct |
50 |
Correct |
131 ms |
98644 KB |
Output is correct |
51 |
Correct |
107 ms |
71764 KB |
Output is correct |
52 |
Correct |
77 ms |
92244 KB |
Output is correct |
53 |
Correct |
137 ms |
70228 KB |
Output is correct |
54 |
Correct |
493 ms |
90360 KB |
Output is correct |
55 |
Correct |
417 ms |
90704 KB |
Output is correct |
56 |
Correct |
93 ms |
98640 KB |
Output is correct |
57 |
Correct |
269 ms |
96304 KB |
Output is correct |
58 |
Correct |
120 ms |
92496 KB |
Output is correct |
59 |
Correct |
352 ms |
93580 KB |
Output is correct |
60 |
Correct |
116 ms |
90564 KB |
Output is correct |
61 |
Correct |
313 ms |
94980 KB |
Output is correct |
62 |
Correct |
340 ms |
93780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
15704 KB |
Output is correct |
2 |
Correct |
2 ms |
15708 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
16220 KB |
Output is correct |
5 |
Correct |
3 ms |
16220 KB |
Output is correct |
6 |
Correct |
5 ms |
16372 KB |
Output is correct |
7 |
Correct |
4 ms |
16220 KB |
Output is correct |
8 |
Correct |
3 ms |
16220 KB |
Output is correct |
9 |
Correct |
4 ms |
16220 KB |
Output is correct |
10 |
Correct |
3 ms |
16220 KB |
Output is correct |
11 |
Correct |
3 ms |
16220 KB |
Output is correct |
12 |
Correct |
3 ms |
16220 KB |
Output is correct |
13 |
Correct |
4 ms |
16220 KB |
Output is correct |
14 |
Correct |
4 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
16220 KB |
Output is correct |
16 |
Correct |
4 ms |
16364 KB |
Output is correct |
17 |
Correct |
3 ms |
16220 KB |
Output is correct |
18 |
Correct |
4 ms |
16220 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
3 ms |
16220 KB |
Output is correct |
21 |
Correct |
3 ms |
16220 KB |
Output is correct |
22 |
Correct |
4 ms |
16216 KB |
Output is correct |
23 |
Correct |
4 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16220 KB |
Output is correct |
25 |
Correct |
3 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16220 KB |
Output is correct |
28 |
Correct |
4 ms |
16220 KB |
Output is correct |
29 |
Correct |
3 ms |
15708 KB |
Output is correct |
30 |
Correct |
4 ms |
16220 KB |
Output is correct |
31 |
Correct |
4 ms |
16312 KB |
Output is correct |
32 |
Correct |
3 ms |
16220 KB |
Output is correct |
33 |
Incorrect |
5 ms |
16292 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
26708 KB |
Output is correct |
2 |
Correct |
68 ms |
26704 KB |
Output is correct |
3 |
Correct |
73 ms |
25748 KB |
Output is correct |
4 |
Correct |
56 ms |
29212 KB |
Output is correct |
5 |
Correct |
57 ms |
30804 KB |
Output is correct |
6 |
Correct |
40 ms |
20304 KB |
Output is correct |
7 |
Correct |
63 ms |
26352 KB |
Output is correct |
8 |
Correct |
69 ms |
25652 KB |
Output is correct |
9 |
Correct |
49 ms |
29732 KB |
Output is correct |
10 |
Correct |
51 ms |
30824 KB |
Output is correct |
11 |
Correct |
79 ms |
26588 KB |
Output is correct |
12 |
Correct |
61 ms |
25168 KB |
Output is correct |
13 |
Correct |
66 ms |
26704 KB |
Output is correct |
14 |
Correct |
2 ms |
15704 KB |
Output is correct |
15 |
Correct |
2 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
3 ms |
16220 KB |
Output is correct |
18 |
Correct |
3 ms |
16220 KB |
Output is correct |
19 |
Correct |
5 ms |
16372 KB |
Output is correct |
20 |
Correct |
4 ms |
16220 KB |
Output is correct |
21 |
Correct |
3 ms |
16220 KB |
Output is correct |
22 |
Correct |
4 ms |
16220 KB |
Output is correct |
23 |
Correct |
3 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16220 KB |
Output is correct |
25 |
Correct |
3 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16220 KB |
Output is correct |
28 |
Correct |
3 ms |
16220 KB |
Output is correct |
29 |
Correct |
4 ms |
16364 KB |
Output is correct |
30 |
Correct |
3 ms |
16220 KB |
Output is correct |
31 |
Correct |
4 ms |
16220 KB |
Output is correct |
32 |
Correct |
4 ms |
16220 KB |
Output is correct |
33 |
Correct |
3 ms |
16220 KB |
Output is correct |
34 |
Correct |
3 ms |
16220 KB |
Output is correct |
35 |
Correct |
4 ms |
16216 KB |
Output is correct |
36 |
Correct |
4 ms |
16220 KB |
Output is correct |
37 |
Correct |
3 ms |
16220 KB |
Output is correct |
38 |
Correct |
3 ms |
16220 KB |
Output is correct |
39 |
Correct |
4 ms |
16220 KB |
Output is correct |
40 |
Correct |
4 ms |
16220 KB |
Output is correct |
41 |
Correct |
4 ms |
16220 KB |
Output is correct |
42 |
Correct |
431 ms |
91752 KB |
Output is correct |
43 |
Correct |
505 ms |
91340 KB |
Output is correct |
44 |
Correct |
519 ms |
89780 KB |
Output is correct |
45 |
Correct |
251 ms |
100692 KB |
Output is correct |
46 |
Correct |
105 ms |
95500 KB |
Output is correct |
47 |
Correct |
209 ms |
94292 KB |
Output is correct |
48 |
Correct |
94 ms |
56520 KB |
Output is correct |
49 |
Correct |
365 ms |
92100 KB |
Output is correct |
50 |
Correct |
448 ms |
91728 KB |
Output is correct |
51 |
Correct |
240 ms |
100436 KB |
Output is correct |
52 |
Correct |
154 ms |
96384 KB |
Output is correct |
53 |
Correct |
183 ms |
91476 KB |
Output is correct |
54 |
Correct |
98 ms |
91040 KB |
Output is correct |
55 |
Correct |
76 ms |
57548 KB |
Output is correct |
56 |
Correct |
348 ms |
94032 KB |
Output is correct |
57 |
Correct |
148 ms |
98836 KB |
Output is correct |
58 |
Correct |
132 ms |
96340 KB |
Output is correct |
59 |
Correct |
283 ms |
94408 KB |
Output is correct |
60 |
Correct |
372 ms |
91988 KB |
Output is correct |
61 |
Correct |
508 ms |
90556 KB |
Output is correct |
62 |
Correct |
455 ms |
90772 KB |
Output is correct |
63 |
Correct |
131 ms |
98644 KB |
Output is correct |
64 |
Correct |
107 ms |
71764 KB |
Output is correct |
65 |
Correct |
77 ms |
92244 KB |
Output is correct |
66 |
Correct |
137 ms |
70228 KB |
Output is correct |
67 |
Correct |
493 ms |
90360 KB |
Output is correct |
68 |
Correct |
417 ms |
90704 KB |
Output is correct |
69 |
Correct |
93 ms |
98640 KB |
Output is correct |
70 |
Correct |
269 ms |
96304 KB |
Output is correct |
71 |
Correct |
120 ms |
92496 KB |
Output is correct |
72 |
Correct |
352 ms |
93580 KB |
Output is correct |
73 |
Correct |
116 ms |
90564 KB |
Output is correct |
74 |
Correct |
313 ms |
94980 KB |
Output is correct |
75 |
Correct |
340 ms |
93780 KB |
Output is correct |
76 |
Correct |
3 ms |
15708 KB |
Output is correct |
77 |
Correct |
4 ms |
16220 KB |
Output is correct |
78 |
Correct |
4 ms |
16312 KB |
Output is correct |
79 |
Correct |
3 ms |
16220 KB |
Output is correct |
80 |
Incorrect |
5 ms |
16292 KB |
Output isn't correct |
81 |
Halted |
0 ms |
0 KB |
- |