#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const ll N = 1e6 + 1;
ll n;
ll a[N], b[N];
namespace sub1 {
const ll N = 1e6 + 1;
ll f[N];
void solve() {
ll res = 0;
for (int i = 1; i <= n; i ++) f[i] = f[i - 1] + (a[i] - b[i]);
for (int i = 1; i <= n; i ++) res += abs(f[i]);
cout << res;
}
}
namespace sub3 {
const ll N = 3e3 + 1;
ll d[N];
ll f[(ll)3e4 + 1], of[(ll)3e4 + 1];
void solve() {
for (int i = 1; i <= n; i ++) d[i] = d[i - 1] + a[i] - b[i];
memset(f, 0x3f, sizeof f);
of[0] = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 0; j <= d[n]; j ++) {
if (j != 0) f[j] = f[j - 1];
f[j] = min(f[j], of[j] + abs(d[i] - j));
}
for (int j = 0; j <= d[n]; j ++) {
of[j] = f[j];
f[j] = 1e18;
}
}
cout << of[d[n]];
}
}
namespace sub5 {
const ll N = 1e6 + 1;
ll d[N];
void solve() {
for (int i = 1; i <= n; i ++) {
d[i] = d[i - 1] + a[i] - b[i];
}
priority_queue<ll> pq; ll res = 0;
for (int i = 1; i <= n; i ++) {
if (d[i] < 0) res += -d[i], d[i] = 0;
if (d[i] >= d[n]) res += d[i] - d[n], d[i] = d[n];
pq.push(d[i]); ll top = pq.top();
if (pq.top() > d[i]) {
res += top - d[i];
pq.pop(); pq.push(d[i]);
}
// cout << "Debug: " << i << ' ' << d[i] << ' ' << res << endl;
}
cout << res;
}
}
void doTest(ll testID) {
cin >> n; ll sa = 0, sb = 0;
for (int i = 1; i <= n; i ++) {
cin >> a[i] >> b[i];
sa += a[i], sb += b[i];
}
// if (sa == sb) {sub1::solve(); return;}
// if (n <= 3e3 && max(sa, sb) <= 3e4) {sub3::solve(); return;}
sub5::solve();
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int test = 1;
// cin >> test;
for (int _ = 1; _ <= test; _ ++) doTest(_);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
6 ms |
7380 KB |
Output is correct |
5 |
Correct |
10 ms |
9880 KB |
Output is correct |
6 |
Correct |
29 ms |
14020 KB |
Output is correct |
7 |
Correct |
55 ms |
23128 KB |
Output is correct |
8 |
Correct |
46 ms |
23128 KB |
Output is correct |
9 |
Correct |
44 ms |
23236 KB |
Output is correct |
10 |
Correct |
34 ms |
23236 KB |
Output is correct |
11 |
Correct |
38 ms |
23392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
6 ms |
7380 KB |
Output is correct |
5 |
Correct |
10 ms |
9880 KB |
Output is correct |
6 |
Correct |
29 ms |
14020 KB |
Output is correct |
7 |
Correct |
55 ms |
23128 KB |
Output is correct |
8 |
Correct |
46 ms |
23128 KB |
Output is correct |
9 |
Correct |
44 ms |
23236 KB |
Output is correct |
10 |
Correct |
34 ms |
23236 KB |
Output is correct |
11 |
Correct |
38 ms |
23392 KB |
Output is correct |
12 |
Correct |
16 ms |
11984 KB |
Output is correct |
13 |
Correct |
41 ms |
23232 KB |
Output is correct |
14 |
Correct |
66 ms |
30012 KB |
Output is correct |
15 |
Correct |
54 ms |
28108 KB |
Output is correct |
16 |
Correct |
58 ms |
27328 KB |
Output is correct |
17 |
Correct |
47 ms |
25276 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
1 ms |
4688 KB |
Output is correct |
17 |
Correct |
2 ms |
4432 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
6 ms |
7380 KB |
Output is correct |
12 |
Correct |
10 ms |
9880 KB |
Output is correct |
13 |
Correct |
29 ms |
14020 KB |
Output is correct |
14 |
Correct |
55 ms |
23128 KB |
Output is correct |
15 |
Correct |
46 ms |
23128 KB |
Output is correct |
16 |
Correct |
44 ms |
23236 KB |
Output is correct |
17 |
Correct |
34 ms |
23236 KB |
Output is correct |
18 |
Correct |
38 ms |
23392 KB |
Output is correct |
19 |
Correct |
16 ms |
11984 KB |
Output is correct |
20 |
Correct |
41 ms |
23232 KB |
Output is correct |
21 |
Correct |
66 ms |
30012 KB |
Output is correct |
22 |
Correct |
54 ms |
28108 KB |
Output is correct |
23 |
Correct |
58 ms |
27328 KB |
Output is correct |
24 |
Correct |
47 ms |
25276 KB |
Output is correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Correct |
2 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
4688 KB |
Output is correct |
28 |
Correct |
2 ms |
4688 KB |
Output is correct |
29 |
Correct |
1 ms |
4688 KB |
Output is correct |
30 |
Correct |
2 ms |
4432 KB |
Output is correct |
31 |
Correct |
2 ms |
4688 KB |
Output is correct |
32 |
Correct |
1 ms |
4432 KB |
Output is correct |
33 |
Correct |
28 ms |
13624 KB |
Output is correct |
34 |
Correct |
63 ms |
23116 KB |
Output is correct |
35 |
Correct |
104 ms |
30032 KB |
Output is correct |
36 |
Correct |
95 ms |
27584 KB |
Output is correct |
37 |
Correct |
53 ms |
28112 KB |
Output is correct |
38 |
Correct |
92 ms |
29884 KB |
Output is correct |
39 |
Correct |
57 ms |
26196 KB |
Output is correct |
40 |
Correct |
59 ms |
26052 KB |
Output is correct |
41 |
Correct |
52 ms |
25288 KB |
Output is correct |
42 |
Correct |
54 ms |
25280 KB |
Output is correct |
43 |
Correct |
62 ms |
25160 KB |
Output is correct |
44 |
Correct |
58 ms |
25284 KB |
Output is correct |
45 |
Correct |
74 ms |
30016 KB |
Output is correct |
46 |
Correct |
50 ms |
25560 KB |
Output is correct |
47 |
Correct |
66 ms |
25932 KB |
Output is correct |