#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<ll,ll>
#define SIZE (1<<21)
#define Getchar() (pr1 == pr2 && (pr2 = (pr1 = fr) + fread(fr, 1, SIZE, stdin), pr1 == pr2) ? EOF : *pr1++)
#define Putchar(ch) (pw < SIZE ? fw[pw++] = (ch) : (fwrite(fw, 1, SIZE, stdout), fw[(pw = 0)++] = (ch)))
char fr[SIZE], *pr1 = fr, *pr2 = fr;
template<class T> inline void read(T& f) {
f = 0; T sgn = 1; char c = Getchar();
while (!isdigit(c)) {if (c == '-') sgn = -1; c = Getchar();}
while (isdigit(c)) {f = f*10+(c&15); c = Getchar();}
f *= sgn;
}
template<class T, class... V> inline void read(T& t, V&... v) {read(t); read(v...);}
const int mx = 500005;
pii a[mx];
int nxt[mx], prv[mx], las;
void unlink(int x) {
int y = nxt[x], z = prv[x];
if (y != 0) {
if (z != 0) {
prv[y] = z;
nxt[z] = y;
} else {
prv[y] = 0;
}
} else {
if (z != 0) {
las = z;
nxt[z] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
read(n);
map<ll,ll> ma;
for (int q = 0; q < n; q++) {
read(a[q].fi,a[q].se);
ma[a[q].fi] = 0;
}
sort(a, a+n);
int cur = 1;
auto it = ma.begin();
vector<ll> rma;
rma.push_back(0);
while (it != ma.end()) {
rma.push_back(it->fi);
it->se = cur++;
it++;
}
vector<ll> pref(cur+5);
int ptr = 0;
int upd = 1;
while (ptr < n) {
int pt = ptr;
ll tot = 0;
while (pt < n && a[pt].fi == a[ptr].fi) {
tot += a[pt].se;
pt++;
}
pref[upd] = pref[upd-1]+tot;
upd++;
ptr = pt;
}
vector<pii> f;
for (int q = 1; q < cur; q++) {
f.emplace_back(pref[q]-rma[q],q);
}
sort(f.begin(), f.end());
for (int q = 0; q < SZ(f); q++) {
if (q > 0) prv[f[q].se] = f[q-1].se;
if (q < SZ(f)-1) nxt[f[q].se] = f[q+1].se;
}
las = f[SZ(f)-1].se;
ll ans = LLONG_MIN;
for (int q = 0; q < cur-1; q++) {
ans = max(ans, pref[las]-rma[las]+rma[q+1]-pref[q]);
unlink(q+1);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
1100 KB |
Output is correct |
28 |
Correct |
3 ms |
1100 KB |
Output is correct |
29 |
Correct |
3 ms |
1100 KB |
Output is correct |
30 |
Correct |
3 ms |
1100 KB |
Output is correct |
31 |
Correct |
3 ms |
1100 KB |
Output is correct |
32 |
Correct |
3 ms |
1100 KB |
Output is correct |
33 |
Correct |
3 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
1100 KB |
Output is correct |
35 |
Correct |
3 ms |
1100 KB |
Output is correct |
36 |
Correct |
3 ms |
1100 KB |
Output is correct |
37 |
Correct |
4 ms |
1100 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1100 KB |
Output is correct |
40 |
Correct |
3 ms |
1100 KB |
Output is correct |
41 |
Correct |
3 ms |
1100 KB |
Output is correct |
42 |
Correct |
3 ms |
1128 KB |
Output is correct |
43 |
Correct |
3 ms |
1104 KB |
Output is correct |
44 |
Correct |
3 ms |
1100 KB |
Output is correct |
45 |
Correct |
3 ms |
1100 KB |
Output is correct |
46 |
Correct |
3 ms |
1100 KB |
Output is correct |
47 |
Correct |
3 ms |
1100 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1100 KB |
Output is correct |
50 |
Correct |
3 ms |
1100 KB |
Output is correct |
51 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
1100 KB |
Output is correct |
28 |
Correct |
3 ms |
1100 KB |
Output is correct |
29 |
Correct |
3 ms |
1100 KB |
Output is correct |
30 |
Correct |
3 ms |
1100 KB |
Output is correct |
31 |
Correct |
3 ms |
1100 KB |
Output is correct |
32 |
Correct |
3 ms |
1100 KB |
Output is correct |
33 |
Correct |
3 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
1100 KB |
Output is correct |
35 |
Correct |
3 ms |
1100 KB |
Output is correct |
36 |
Correct |
3 ms |
1100 KB |
Output is correct |
37 |
Correct |
4 ms |
1100 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1100 KB |
Output is correct |
40 |
Correct |
3 ms |
1100 KB |
Output is correct |
41 |
Correct |
3 ms |
1100 KB |
Output is correct |
42 |
Correct |
3 ms |
1128 KB |
Output is correct |
43 |
Correct |
3 ms |
1104 KB |
Output is correct |
44 |
Correct |
3 ms |
1100 KB |
Output is correct |
45 |
Correct |
3 ms |
1100 KB |
Output is correct |
46 |
Correct |
3 ms |
1100 KB |
Output is correct |
47 |
Correct |
3 ms |
1100 KB |
Output is correct |
48 |
Correct |
3 ms |
1100 KB |
Output is correct |
49 |
Correct |
3 ms |
1100 KB |
Output is correct |
50 |
Correct |
3 ms |
1100 KB |
Output is correct |
51 |
Correct |
4 ms |
1100 KB |
Output is correct |
52 |
Correct |
742 ms |
61188 KB |
Output is correct |
53 |
Correct |
752 ms |
61292 KB |
Output is correct |
54 |
Correct |
762 ms |
61140 KB |
Output is correct |
55 |
Correct |
773 ms |
61136 KB |
Output is correct |
56 |
Correct |
781 ms |
73896 KB |
Output is correct |
57 |
Correct |
786 ms |
74012 KB |
Output is correct |
58 |
Correct |
770 ms |
73884 KB |
Output is correct |
59 |
Correct |
736 ms |
73860 KB |
Output is correct |
60 |
Correct |
736 ms |
73872 KB |
Output is correct |
61 |
Correct |
809 ms |
73804 KB |
Output is correct |
62 |
Correct |
735 ms |
73896 KB |
Output is correct |
63 |
Correct |
788 ms |
73768 KB |
Output is correct |
64 |
Correct |
758 ms |
73884 KB |
Output is correct |
65 |
Correct |
775 ms |
73776 KB |
Output is correct |
66 |
Correct |
739 ms |
73860 KB |
Output is correct |
67 |
Correct |
734 ms |
73848 KB |
Output is correct |
68 |
Correct |
782 ms |
73864 KB |
Output is correct |