#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> pll;
/*
cari Sum(R) - Sum(L-1) - W[R]+W[L];
= W[L] - Sum(L-1) + sum(r)-W[R];
1. hitung semua nilai Sum{i} - W[i];
Multi set>> pop setelah dipakai (L);
2. loop semua L, tambah jawaban untuk semua
*/
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vector<pair<ll, ll>> rocks(n + 1); // w, v
rocks[0] = {0, 0};
for (int i = 1; i <= n; i++)
{
cin >> rocks[i].first >> rocks[i].second;
}
sort(rocks.begin(), rocks.end());
for (int i = 1; i <= n; i++)
{
rocks[i].second += rocks[i - 1].second;
}
// cari max val of (Vi - Wi) - (Vj-1 - Wj); j > i;
// (Vi - Vj-1) - (Wi - Wj) > max;
// Vi - Vj-1 - Wi + Wj
// Vi - Wi - Vj-w + Wj > max;
// i > j
// n <= 500 000??
// maka nilai f(j) = (Vj-1 - Wj) harus minimal, berarti jika ada j1 and j2 such that j1 > j2 && f(j1)<f(j2);
// j2 becomes useless;
vector<ll> minFVal;
vector<ll> theJidx;
for (int j = n; j >= 1; j--)
{
ll f = rocks[j - 1].second - rocks[j].first;
if (minFVal.empty())
{
minFVal.push_back(f);
theJidx.push_back(j);
}
else
{
ll lobo = lower_bound(minFVal.begin(), minFVal.end(), f) - minFVal.begin();
if (lobo == minFVal.size())
{
minFVal.push_back(f);
theJidx.push_back(j);
}
else
{
while (lobo != minFVal.size() - 1)
{
minFVal.pop_back();
theJidx.pop_back();
}
minFVal[lobo] = f;
theJidx[lobo] = j;
}
}
}
ll curJ = theJidx.size() - 1, ans = 0;
// for (int i = 0; i <= curJ; i++)
// {
// cout << minFVal[i] << " " << theJidx[i] << "\n";
// }
for (int i = 1; i <= n; i++)
{
ll g = rocks[i].second - rocks[i].first;
while (curJ > 0 && theJidx[curJ - 1] <= i)
{
curJ--;
}
ans = max(ans, g - minFVal[curJ]);
}
cout << ans;
return 0;
}
Compilation message
art.cpp: In function 'int main()':
art.cpp:50:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if (lobo == minFVal.size())
| ~~~~~^~~~~~~~~~~~~~~~~
art.cpp:57:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | while (lobo != minFVal.size() - 1)
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
520 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
584 KB |
Output is correct |
35 |
Correct |
3 ms |
460 KB |
Output is correct |
36 |
Correct |
3 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
524 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
2 ms |
528 KB |
Output is correct |
49 |
Correct |
2 ms |
464 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
520 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
584 KB |
Output is correct |
35 |
Correct |
3 ms |
460 KB |
Output is correct |
36 |
Correct |
3 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
524 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
2 ms |
528 KB |
Output is correct |
49 |
Correct |
2 ms |
464 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
52 |
Correct |
214 ms |
27044 KB |
Output is correct |
53 |
Correct |
190 ms |
23976 KB |
Output is correct |
54 |
Correct |
180 ms |
21800 KB |
Output is correct |
55 |
Correct |
188 ms |
21068 KB |
Output is correct |
56 |
Correct |
178 ms |
20792 KB |
Output is correct |
57 |
Correct |
210 ms |
20788 KB |
Output is correct |
58 |
Correct |
180 ms |
20800 KB |
Output is correct |
59 |
Correct |
190 ms |
20668 KB |
Output is correct |
60 |
Correct |
190 ms |
20788 KB |
Output is correct |
61 |
Correct |
179 ms |
20680 KB |
Output is correct |
62 |
Correct |
189 ms |
20676 KB |
Output is correct |
63 |
Correct |
180 ms |
20784 KB |
Output is correct |
64 |
Correct |
184 ms |
20668 KB |
Output is correct |
65 |
Correct |
195 ms |
20672 KB |
Output is correct |
66 |
Correct |
182 ms |
20680 KB |
Output is correct |
67 |
Correct |
199 ms |
20668 KB |
Output is correct |
68 |
Correct |
181 ms |
20668 KB |
Output is correct |