#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
void read(T &x)
{
x = 0;
register int c;
while ((c = getchar()) && (c > '9' || c < '0'))
;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
constexpr bool typetest = 0;
constexpr int N = 5e5 + 5;
/*https://usaco.guide/adv/slope-trick/#potatoes--fertilizers*/
/*https://www.youtube.com/watch?v=p8RxN6Y9OOA*/
int n;
ll a[N], b[N];
void Read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i] >> b[i];
a[i] = a[i - 1] + a[i] - b[i];
}
}
void Solve()
{
ll res(0);
for (int i = 1; i <= n; ++i)
{
if (a[i] < 0)
{ // First, assume that all have to be non-negative
res += -a[i];
a[i] = 0;
}
if (a[i] > a[n])
{
// assume that all are less or equal to a[n]
res += a[i] - a[n];
a[i] = a[n];
}
}
priority_queue<ll> s;
for (int i = 1; i <= n; ++i)
{
s.emplace(a[i]);
if (i == 1)
continue;
ll ontop = s.top();
if (ontop > a[i])
{
res += ontop - a[i];
s.pop();
s.emplace(a[i]);
}
}
cout << res;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("digits.inp", "r", stdin);
//freopen("digits.out", "w", stdout);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
//cout << "Case #" << _ << ":\n";
Read();
Solve();
}
//cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
Compilation message
bulves.cpp: In function 'void read(T&)':
bulves.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
12 | register int c;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
8 ms |
2100 KB |
Output is correct |
5 |
Correct |
16 ms |
3588 KB |
Output is correct |
6 |
Correct |
47 ms |
8340 KB |
Output is correct |
7 |
Correct |
93 ms |
14268 KB |
Output is correct |
8 |
Correct |
121 ms |
14396 KB |
Output is correct |
9 |
Correct |
86 ms |
14268 KB |
Output is correct |
10 |
Correct |
72 ms |
13372 KB |
Output is correct |
11 |
Correct |
58 ms |
13372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
8 ms |
2100 KB |
Output is correct |
5 |
Correct |
16 ms |
3588 KB |
Output is correct |
6 |
Correct |
47 ms |
8340 KB |
Output is correct |
7 |
Correct |
93 ms |
14268 KB |
Output is correct |
8 |
Correct |
121 ms |
14396 KB |
Output is correct |
9 |
Correct |
86 ms |
14268 KB |
Output is correct |
10 |
Correct |
72 ms |
13372 KB |
Output is correct |
11 |
Correct |
58 ms |
13372 KB |
Output is correct |
12 |
Correct |
27 ms |
4152 KB |
Output is correct |
13 |
Correct |
62 ms |
11260 KB |
Output is correct |
14 |
Correct |
110 ms |
14336 KB |
Output is correct |
15 |
Correct |
79 ms |
14344 KB |
Output is correct |
16 |
Correct |
82 ms |
14380 KB |
Output is correct |
17 |
Correct |
76 ms |
13340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 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 |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
8 ms |
2100 KB |
Output is correct |
12 |
Correct |
16 ms |
3588 KB |
Output is correct |
13 |
Correct |
47 ms |
8340 KB |
Output is correct |
14 |
Correct |
93 ms |
14268 KB |
Output is correct |
15 |
Correct |
121 ms |
14396 KB |
Output is correct |
16 |
Correct |
86 ms |
14268 KB |
Output is correct |
17 |
Correct |
72 ms |
13372 KB |
Output is correct |
18 |
Correct |
58 ms |
13372 KB |
Output is correct |
19 |
Correct |
27 ms |
4152 KB |
Output is correct |
20 |
Correct |
62 ms |
11260 KB |
Output is correct |
21 |
Correct |
110 ms |
14336 KB |
Output is correct |
22 |
Correct |
79 ms |
14344 KB |
Output is correct |
23 |
Correct |
82 ms |
14380 KB |
Output is correct |
24 |
Correct |
76 ms |
13340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
388 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
42 ms |
4124 KB |
Output is correct |
34 |
Correct |
103 ms |
11200 KB |
Output is correct |
35 |
Correct |
155 ms |
14384 KB |
Output is correct |
36 |
Correct |
159 ms |
14292 KB |
Output is correct |
37 |
Correct |
85 ms |
14396 KB |
Output is correct |
38 |
Correct |
149 ms |
14396 KB |
Output is correct |
39 |
Correct |
96 ms |
14356 KB |
Output is correct |
40 |
Correct |
107 ms |
13500 KB |
Output is correct |
41 |
Correct |
78 ms |
13368 KB |
Output is correct |
42 |
Correct |
100 ms |
13372 KB |
Output is correct |
43 |
Correct |
86 ms |
13328 KB |
Output is correct |
44 |
Correct |
103 ms |
13456 KB |
Output is correct |
45 |
Correct |
120 ms |
14524 KB |
Output is correct |
46 |
Correct |
76 ms |
13628 KB |
Output is correct |
47 |
Correct |
101 ms |
12328 KB |
Output is correct |