#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MAXN = 500100;
const ll INF = 1e18;
const ll MOD = 1e9+7;
pi A[MAXN];
vpi V;
ll N,a,b;
struct node{
ll s,e,m,v,lazy;
node *l,*r;
node(int _s,int _e):s(_s), e(_e){
m=(s+e)/2;v=-V[s].f;lazy=0;
// cout<<"Original "<<v<<' '<<V[]
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
v=max(l->v,r->v);
}
}
void up(int x, int y, int val){
if (s==x&&e==y){lazy+=val;return;}
if (y<=m)l->up(x,y,val);
else if (x>m)r->up(x,y,val);
else {l->up(x,m,val);r->up(m+1,y,val);}
v=max(l->v+l->lazy, r->v+r->lazy);
}
ll query(ll x, ll y){
if (s==x&&e==y)return v+lazy;
if (y<=m)return l->query(x,y);
if (x>m)return r->query(x,y);
return max(l->query(x,m), r->query(m+1,y));
}
}*root;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>N;
for (int i=1;i<=N;++i){
cin>>A[i].f>>A[i].s;
}
sort(A+1,A+N+1);
for(int i=1;i<=N;++i){
if (SZ(V) && V.back().f == A[i].f){
V.back().s += A[i].s;
}else V.pb(A[i]);
}
ll ans = 0;
N = SZ(V);
root = new node(0,N-1);
for (int i=0;i<N;++i){
// cout<<i<<' '<<V[i].s<<'\n';
root->up(i,N-1,V[i].s);
}
for (int i=0;i<N;++i){
ll offset = V[i].s+V[i].f;
// cout<<"At "<<V[i].f<< " offset is "<<offset<<'\n';
root->up(i,N-1,-V[i].s);
ll v = root->query(i,N-1);
// cout<<"Value "<<v<<'\n';
ans=max(ans,v+offset);
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
1152 KB |
Output is correct |
28 |
Correct |
8 ms |
1152 KB |
Output is correct |
29 |
Correct |
8 ms |
1152 KB |
Output is correct |
30 |
Correct |
9 ms |
1152 KB |
Output is correct |
31 |
Correct |
8 ms |
1152 KB |
Output is correct |
32 |
Correct |
10 ms |
1152 KB |
Output is correct |
33 |
Correct |
8 ms |
1152 KB |
Output is correct |
34 |
Correct |
8 ms |
1152 KB |
Output is correct |
35 |
Correct |
8 ms |
1152 KB |
Output is correct |
36 |
Correct |
8 ms |
1152 KB |
Output is correct |
37 |
Correct |
8 ms |
1152 KB |
Output is correct |
38 |
Correct |
8 ms |
1152 KB |
Output is correct |
39 |
Correct |
8 ms |
1152 KB |
Output is correct |
40 |
Correct |
8 ms |
1152 KB |
Output is correct |
41 |
Correct |
8 ms |
1152 KB |
Output is correct |
42 |
Correct |
9 ms |
1152 KB |
Output is correct |
43 |
Correct |
8 ms |
1152 KB |
Output is correct |
44 |
Correct |
8 ms |
1152 KB |
Output is correct |
45 |
Correct |
9 ms |
1152 KB |
Output is correct |
46 |
Correct |
8 ms |
1152 KB |
Output is correct |
47 |
Correct |
8 ms |
1152 KB |
Output is correct |
48 |
Correct |
8 ms |
1152 KB |
Output is correct |
49 |
Correct |
8 ms |
1152 KB |
Output is correct |
50 |
Correct |
9 ms |
1152 KB |
Output is correct |
51 |
Correct |
8 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
1152 KB |
Output is correct |
28 |
Correct |
8 ms |
1152 KB |
Output is correct |
29 |
Correct |
8 ms |
1152 KB |
Output is correct |
30 |
Correct |
9 ms |
1152 KB |
Output is correct |
31 |
Correct |
8 ms |
1152 KB |
Output is correct |
32 |
Correct |
10 ms |
1152 KB |
Output is correct |
33 |
Correct |
8 ms |
1152 KB |
Output is correct |
34 |
Correct |
8 ms |
1152 KB |
Output is correct |
35 |
Correct |
8 ms |
1152 KB |
Output is correct |
36 |
Correct |
8 ms |
1152 KB |
Output is correct |
37 |
Correct |
8 ms |
1152 KB |
Output is correct |
38 |
Correct |
8 ms |
1152 KB |
Output is correct |
39 |
Correct |
8 ms |
1152 KB |
Output is correct |
40 |
Correct |
8 ms |
1152 KB |
Output is correct |
41 |
Correct |
8 ms |
1152 KB |
Output is correct |
42 |
Correct |
9 ms |
1152 KB |
Output is correct |
43 |
Correct |
8 ms |
1152 KB |
Output is correct |
44 |
Correct |
8 ms |
1152 KB |
Output is correct |
45 |
Correct |
9 ms |
1152 KB |
Output is correct |
46 |
Correct |
8 ms |
1152 KB |
Output is correct |
47 |
Correct |
8 ms |
1152 KB |
Output is correct |
48 |
Correct |
8 ms |
1152 KB |
Output is correct |
49 |
Correct |
8 ms |
1152 KB |
Output is correct |
50 |
Correct |
9 ms |
1152 KB |
Output is correct |
51 |
Correct |
8 ms |
1152 KB |
Output is correct |
52 |
Correct |
448 ms |
78548 KB |
Output is correct |
53 |
Correct |
447 ms |
91316 KB |
Output is correct |
54 |
Correct |
441 ms |
91348 KB |
Output is correct |
55 |
Correct |
430 ms |
91220 KB |
Output is correct |
56 |
Correct |
445 ms |
91348 KB |
Output is correct |
57 |
Correct |
425 ms |
91220 KB |
Output is correct |
58 |
Correct |
446 ms |
91224 KB |
Output is correct |
59 |
Correct |
443 ms |
91220 KB |
Output is correct |
60 |
Correct |
434 ms |
91220 KB |
Output is correct |
61 |
Correct |
447 ms |
91092 KB |
Output is correct |
62 |
Correct |
449 ms |
91220 KB |
Output is correct |
63 |
Correct |
438 ms |
91216 KB |
Output is correct |
64 |
Correct |
435 ms |
91220 KB |
Output is correct |
65 |
Correct |
445 ms |
91348 KB |
Output is correct |
66 |
Correct |
456 ms |
91220 KB |
Output is correct |
67 |
Correct |
440 ms |
91288 KB |
Output is correct |
68 |
Correct |
432 ms |
91216 KB |
Output is correct |