#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=5e5+5;
int seg[4*mxn],prefix[mxn],tag[4*mxn];
pii v[mxn];
void push(int id){
seg[id*2]-=tag[id];
seg[id*2+1]-=tag[id];
tag[id*2]+=tag[id];
tag[id*2+1]+=tag[id];
tag[id]=0;
}
void build(int l,int r,int id){
if(l==r){
seg[id]=prefix[l]-v[l].F;
return;
}
int mm=l+r>>1;
build(l,mm,id*2);
build(mm+1,r,id*2+1);
seg[id]=max(seg[id*2],seg[id*2+1]);
}
void modify(int l,int r,int id,int ql,int qr,int v){
if(ql<=l and r<=qr){
seg[id]-=v;
tag[id]=v;
return;
}
int mm=l+r>>1;
push(id);
if(qr<=mm){
modify(l,mm,id*2,ql,qr,v);
}
else if(ql>mm){
modify(mm+1,r,id*2+1,ql,qr,v);
}
else{
modify(l,mm,id*2,ql,mm,v);
modify(mm+1,r,id*2+1,mm+1,qr,v);
}
seg[id]=max(seg[id*2],seg[id*2+1]);
}
int query(int l,int r,int id,int ql,int qr){
if(ql<=l and r<=qr){
return seg[id];
}
int mm=l+r>>1;
push(id);
if(qr<=mm){
return query(l,mm,id*2,ql,qr);
}
else if(ql>mm){
return query(mm+1,r,id*2+1,ql,qr);
}
else{
return max(query(l,mm,id*2,ql,mm),query(mm+1,r,id*2+1,mm+1,qr));
}
}
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>v[i].F>>v[i].S;
}
sort(v+1,v+n+1);
for(int i=1;i<=n;i++){
prefix[i]=prefix[i-1]+v[i].S;
}
build(1,n,1);
int ans=0;
for(int i=1;i<=n;i++){
chmax(ans,query(1,n,1,i,n)-prefix[i-1]+v[i].F);
}
cout<<ans<<'\n';
}
/*
input:
*/
Compilation message
art.cpp: In function 'void build(long long int, long long int, long long int)':
art.cpp:67:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mm=l+r>>1;
| ~^~
art.cpp: In function 'void modify(long long int, long long int, long long int, long long int, long long int, long long int)':
art.cpp:78:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | int mm=l+r>>1;
| ~^~
art.cpp: In function 'long long int query(long long int, long long int, long long int, long long int, long long int)':
art.cpp:96:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
96 | int mm=l+r>>1;
| ~^~
art.cpp: In function 'void setIO(std::string)':
art.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:41:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2692 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2784 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2904 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2652 KB |
Output is correct |
44 |
Correct |
2 ms |
2908 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
2 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2652 KB |
Output is correct |
48 |
Correct |
2 ms |
2788 KB |
Output is correct |
49 |
Correct |
2 ms |
2908 KB |
Output is correct |
50 |
Correct |
2 ms |
2780 KB |
Output is correct |
51 |
Correct |
2 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2692 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2784 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2904 KB |
Output is correct |
41 |
Correct |
2 ms |
2652 KB |
Output is correct |
42 |
Correct |
2 ms |
2652 KB |
Output is correct |
43 |
Correct |
2 ms |
2652 KB |
Output is correct |
44 |
Correct |
2 ms |
2908 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
2 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2652 KB |
Output is correct |
48 |
Correct |
2 ms |
2788 KB |
Output is correct |
49 |
Correct |
2 ms |
2908 KB |
Output is correct |
50 |
Correct |
2 ms |
2780 KB |
Output is correct |
51 |
Correct |
2 ms |
2904 KB |
Output is correct |
52 |
Correct |
179 ms |
40436 KB |
Output is correct |
53 |
Correct |
175 ms |
40276 KB |
Output is correct |
54 |
Correct |
174 ms |
40272 KB |
Output is correct |
55 |
Correct |
175 ms |
40276 KB |
Output is correct |
56 |
Correct |
173 ms |
40412 KB |
Output is correct |
57 |
Correct |
177 ms |
40276 KB |
Output is correct |
58 |
Correct |
175 ms |
40492 KB |
Output is correct |
59 |
Correct |
181 ms |
40528 KB |
Output is correct |
60 |
Correct |
174 ms |
40272 KB |
Output is correct |
61 |
Correct |
176 ms |
40272 KB |
Output is correct |
62 |
Correct |
178 ms |
40672 KB |
Output is correct |
63 |
Correct |
177 ms |
40272 KB |
Output is correct |
64 |
Correct |
174 ms |
40408 KB |
Output is correct |
65 |
Correct |
180 ms |
40544 KB |
Output is correct |
66 |
Correct |
175 ms |
40428 KB |
Output is correct |
67 |
Correct |
175 ms |
40404 KB |
Output is correct |
68 |
Correct |
180 ms |
40744 KB |
Output is correct |