#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
const int MOD = 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
int main(){
std::ios::sync_with_stdio(false); cin.tie(NULL);
int N; cin >> N;
vector<pair<ll, ll>> arts;
F0R(i, N){
ll a, b; cin >> a >> b;
arts.pb({a,b});
}
sort(all(arts));
vl A,B;
for(auto&[a, b]:arts){
A.pb(a); B.pb(b);
}
vl prefs = {0};
F0R(i, N) prefs.pb(prefs.back()+B[i]);
vl prefs_orig(all(prefs));
F0R(i, N){
prefs[i+1] -= A[i];
}
vl suffix_maxs(N+1, -INF);
suffix_maxs.back() = prefs.back();
for(int i=N-1;i>-1;--i){
suffix_maxs[i] = max(suffix_maxs[i+1],prefs[i]);
}
ll mx = *max_element(all(B));
F0R(st, N){
ll bestmx = suffix_maxs[st+1];
mx = max(mx, bestmx-prefs_orig[st]+A[st]);
}
cout <<mx << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
848 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
840 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
3 ms |
852 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
3 ms |
788 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
2 ms |
852 KB |
Output is correct |
38 |
Correct |
3 ms |
824 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
832 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
2 ms |
840 KB |
Output is correct |
44 |
Correct |
2 ms |
832 KB |
Output is correct |
45 |
Correct |
2 ms |
832 KB |
Output is correct |
46 |
Correct |
2 ms |
852 KB |
Output is correct |
47 |
Correct |
2 ms |
852 KB |
Output is correct |
48 |
Correct |
2 ms |
828 KB |
Output is correct |
49 |
Correct |
2 ms |
852 KB |
Output is correct |
50 |
Correct |
2 ms |
852 KB |
Output is correct |
51 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
852 KB |
Output is correct |
28 |
Correct |
2 ms |
848 KB |
Output is correct |
29 |
Correct |
2 ms |
852 KB |
Output is correct |
30 |
Correct |
2 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
840 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
3 ms |
852 KB |
Output is correct |
34 |
Correct |
2 ms |
852 KB |
Output is correct |
35 |
Correct |
3 ms |
788 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
2 ms |
852 KB |
Output is correct |
38 |
Correct |
3 ms |
824 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
852 KB |
Output is correct |
41 |
Correct |
2 ms |
832 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
2 ms |
840 KB |
Output is correct |
44 |
Correct |
2 ms |
832 KB |
Output is correct |
45 |
Correct |
2 ms |
832 KB |
Output is correct |
46 |
Correct |
2 ms |
852 KB |
Output is correct |
47 |
Correct |
2 ms |
852 KB |
Output is correct |
48 |
Correct |
2 ms |
828 KB |
Output is correct |
49 |
Correct |
2 ms |
852 KB |
Output is correct |
50 |
Correct |
2 ms |
852 KB |
Output is correct |
51 |
Correct |
2 ms |
852 KB |
Output is correct |
52 |
Correct |
156 ms |
29820 KB |
Output is correct |
53 |
Correct |
167 ms |
38588 KB |
Output is correct |
54 |
Correct |
153 ms |
38668 KB |
Output is correct |
55 |
Correct |
164 ms |
38652 KB |
Output is correct |
56 |
Correct |
150 ms |
38612 KB |
Output is correct |
57 |
Correct |
151 ms |
38612 KB |
Output is correct |
58 |
Correct |
160 ms |
38676 KB |
Output is correct |
59 |
Correct |
144 ms |
41928 KB |
Output is correct |
60 |
Correct |
162 ms |
41916 KB |
Output is correct |
61 |
Correct |
148 ms |
41820 KB |
Output is correct |
62 |
Correct |
146 ms |
42028 KB |
Output is correct |
63 |
Correct |
143 ms |
41940 KB |
Output is correct |
64 |
Correct |
143 ms |
41928 KB |
Output is correct |
65 |
Correct |
149 ms |
41944 KB |
Output is correct |
66 |
Correct |
143 ms |
41928 KB |
Output is correct |
67 |
Correct |
145 ms |
41856 KB |
Output is correct |
68 |
Correct |
146 ms |
41956 KB |
Output is correct |