#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define siz(x) int(x.size())
#define ll long long
#define ar array
#define vt vector
#define inf INT_MAX
#define lnf LLONG_MAX
const int nxm = int(5e5) + 7;
int n;
pair<ll, ll> a[nxm];
namespace sub3 {
void solve() {
sort(a, a + n);
for (int i = 1; i < n; ++i) {
a[i].second += a[i - 1].second;
}
ll ans = -lnf;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
ans = max(ans, a[j].second - (i - 1 >= 0 ? a[i - 1].second : 0) - a[j].first + a[i].first);
}
}
cout << ans << "\n";
}
};
namespace sub4 {
void solve() {
sort(a, a + n);
for (int i = 1; i < n; ++i) {
a[i].second += a[i - 1].second;
}
ll ans = -lnf;
ll prf = -lnf;
for (int i = 0; i < n; ++i) {
prf = max(prf, a[i].first - (i - 1 >= 0 ? a[i - 1].second : 0));
ans = max(ans, a[i].second - a[i].first + prf);
}
cout << ans << "\n";
}
};
int subtask() {
if (n <= 5000) {
return 3;
}
return 4;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
if (subtask() == 3) {
sub3::solve();
} else {
sub4::solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
260 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
280 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
260 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
280 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
17 ms |
340 KB |
Output is correct |
28 |
Correct |
16 ms |
408 KB |
Output is correct |
29 |
Correct |
16 ms |
400 KB |
Output is correct |
30 |
Correct |
15 ms |
340 KB |
Output is correct |
31 |
Correct |
15 ms |
404 KB |
Output is correct |
32 |
Correct |
15 ms |
400 KB |
Output is correct |
33 |
Correct |
16 ms |
468 KB |
Output is correct |
34 |
Correct |
15 ms |
404 KB |
Output is correct |
35 |
Correct |
15 ms |
404 KB |
Output is correct |
36 |
Correct |
15 ms |
404 KB |
Output is correct |
37 |
Correct |
15 ms |
340 KB |
Output is correct |
38 |
Correct |
20 ms |
404 KB |
Output is correct |
39 |
Correct |
19 ms |
400 KB |
Output is correct |
40 |
Correct |
15 ms |
340 KB |
Output is correct |
41 |
Correct |
15 ms |
408 KB |
Output is correct |
42 |
Correct |
15 ms |
404 KB |
Output is correct |
43 |
Correct |
15 ms |
340 KB |
Output is correct |
44 |
Correct |
15 ms |
400 KB |
Output is correct |
45 |
Correct |
15 ms |
340 KB |
Output is correct |
46 |
Correct |
16 ms |
404 KB |
Output is correct |
47 |
Correct |
16 ms |
340 KB |
Output is correct |
48 |
Correct |
17 ms |
468 KB |
Output is correct |
49 |
Correct |
15 ms |
404 KB |
Output is correct |
50 |
Correct |
15 ms |
412 KB |
Output is correct |
51 |
Correct |
16 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
260 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
280 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
17 ms |
340 KB |
Output is correct |
28 |
Correct |
16 ms |
408 KB |
Output is correct |
29 |
Correct |
16 ms |
400 KB |
Output is correct |
30 |
Correct |
15 ms |
340 KB |
Output is correct |
31 |
Correct |
15 ms |
404 KB |
Output is correct |
32 |
Correct |
15 ms |
400 KB |
Output is correct |
33 |
Correct |
16 ms |
468 KB |
Output is correct |
34 |
Correct |
15 ms |
404 KB |
Output is correct |
35 |
Correct |
15 ms |
404 KB |
Output is correct |
36 |
Correct |
15 ms |
404 KB |
Output is correct |
37 |
Correct |
15 ms |
340 KB |
Output is correct |
38 |
Correct |
20 ms |
404 KB |
Output is correct |
39 |
Correct |
19 ms |
400 KB |
Output is correct |
40 |
Correct |
15 ms |
340 KB |
Output is correct |
41 |
Correct |
15 ms |
408 KB |
Output is correct |
42 |
Correct |
15 ms |
404 KB |
Output is correct |
43 |
Correct |
15 ms |
340 KB |
Output is correct |
44 |
Correct |
15 ms |
400 KB |
Output is correct |
45 |
Correct |
15 ms |
340 KB |
Output is correct |
46 |
Correct |
16 ms |
404 KB |
Output is correct |
47 |
Correct |
16 ms |
340 KB |
Output is correct |
48 |
Correct |
17 ms |
468 KB |
Output is correct |
49 |
Correct |
15 ms |
404 KB |
Output is correct |
50 |
Correct |
15 ms |
412 KB |
Output is correct |
51 |
Correct |
16 ms |
400 KB |
Output is correct |
52 |
Correct |
179 ms |
15096 KB |
Output is correct |
53 |
Correct |
172 ms |
15412 KB |
Output is correct |
54 |
Correct |
198 ms |
15348 KB |
Output is correct |
55 |
Correct |
172 ms |
15516 KB |
Output is correct |
56 |
Correct |
166 ms |
15448 KB |
Output is correct |
57 |
Correct |
180 ms |
15452 KB |
Output is correct |
58 |
Correct |
173 ms |
15436 KB |
Output is correct |
59 |
Correct |
169 ms |
15380 KB |
Output is correct |
60 |
Correct |
169 ms |
15440 KB |
Output is correct |
61 |
Correct |
171 ms |
15380 KB |
Output is correct |
62 |
Correct |
195 ms |
15376 KB |
Output is correct |
63 |
Correct |
174 ms |
15452 KB |
Output is correct |
64 |
Correct |
167 ms |
15352 KB |
Output is correct |
65 |
Correct |
165 ms |
15436 KB |
Output is correct |
66 |
Correct |
167 ms |
15620 KB |
Output is correct |
67 |
Correct |
163 ms |
15664 KB |
Output is correct |
68 |
Correct |
184 ms |
15444 KB |
Output is correct |