#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define inf 1000000010
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
ll dp0 = 0; // y-intercept
// slope increases by one at each of these points
priority_queue<ll> slopeChanges;
ll initialSlope = 0;
ll totA = 0, totB = 0;
ll diff;
F0R(i, n) {
int a, b; cin >> a >> b; totA += a; totB += b;
diff = totA - totB;
dp0 += abs(diff);
initialSlope--;
// at x = diff, the function slope changes by two
slopeChanges.push(diff);
slopeChanges.push(diff);
// remove the final slope change that will cause the slope to become positive
slopeChanges.pop();
}
// Want to calculate dp value where x = diff (the final difference)
vector<ll> changes;
while (!slopeChanges.empty()) { changes.pb(slopeChanges.top()); slopeChanges.pop(); }
reverse(all(changes));
ll curX = 0, slope = initialSlope;
for (ll x : changes) {
ll dist = x - curX;
if (x >= diff) {
dp0 += slope*dist;
break;
}
if (x >= 0) {
dp0 += slope*dist;
curX = x;
}
slope++;
}
cout << dp0 << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
21 ms |
1920 KB |
Output is correct |
5 |
Correct |
53 ms |
3304 KB |
Output is correct |
6 |
Correct |
118 ms |
8824 KB |
Output is correct |
7 |
Correct |
244 ms |
17400 KB |
Output is correct |
8 |
Correct |
206 ms |
15488 KB |
Output is correct |
9 |
Correct |
245 ms |
14684 KB |
Output is correct |
10 |
Correct |
149 ms |
12380 KB |
Output is correct |
11 |
Correct |
146 ms |
12508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
21 ms |
1920 KB |
Output is correct |
5 |
Correct |
53 ms |
3304 KB |
Output is correct |
6 |
Correct |
118 ms |
8824 KB |
Output is correct |
7 |
Correct |
244 ms |
17400 KB |
Output is correct |
8 |
Correct |
206 ms |
15488 KB |
Output is correct |
9 |
Correct |
245 ms |
14684 KB |
Output is correct |
10 |
Correct |
149 ms |
12380 KB |
Output is correct |
11 |
Correct |
146 ms |
12508 KB |
Output is correct |
12 |
Correct |
70 ms |
4584 KB |
Output is correct |
13 |
Correct |
150 ms |
12892 KB |
Output is correct |
14 |
Correct |
270 ms |
17244 KB |
Output is correct |
15 |
Correct |
224 ms |
15324 KB |
Output is correct |
16 |
Correct |
240 ms |
14816 KB |
Output is correct |
17 |
Correct |
143 ms |
12556 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
21 ms |
1920 KB |
Output is correct |
12 |
Correct |
53 ms |
3304 KB |
Output is correct |
13 |
Correct |
118 ms |
8824 KB |
Output is correct |
14 |
Correct |
244 ms |
17400 KB |
Output is correct |
15 |
Correct |
206 ms |
15488 KB |
Output is correct |
16 |
Correct |
245 ms |
14684 KB |
Output is correct |
17 |
Correct |
149 ms |
12380 KB |
Output is correct |
18 |
Correct |
146 ms |
12508 KB |
Output is correct |
19 |
Correct |
70 ms |
4584 KB |
Output is correct |
20 |
Correct |
150 ms |
12892 KB |
Output is correct |
21 |
Correct |
270 ms |
17244 KB |
Output is correct |
22 |
Correct |
224 ms |
15324 KB |
Output is correct |
23 |
Correct |
240 ms |
14816 KB |
Output is correct |
24 |
Correct |
143 ms |
12556 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
2 ms |
492 KB |
Output is correct |
32 |
Correct |
56 ms |
4584 KB |
Output is correct |
33 |
Correct |
141 ms |
12892 KB |
Output is correct |
34 |
Correct |
247 ms |
17116 KB |
Output is correct |
35 |
Correct |
238 ms |
14812 KB |
Output is correct |
36 |
Correct |
201 ms |
15504 KB |
Output is correct |
37 |
Correct |
238 ms |
17392 KB |
Output is correct |
38 |
Correct |
182 ms |
13532 KB |
Output is correct |
39 |
Correct |
179 ms |
12892 KB |
Output is correct |
40 |
Correct |
143 ms |
12600 KB |
Output is correct |
41 |
Correct |
140 ms |
12380 KB |
Output is correct |
42 |
Correct |
143 ms |
12380 KB |
Output is correct |
43 |
Correct |
155 ms |
12620 KB |
Output is correct |
44 |
Correct |
255 ms |
17244 KB |
Output is correct |
45 |
Correct |
171 ms |
12764 KB |
Output is correct |
46 |
Correct |
156 ms |
12400 KB |
Output is correct |
47 |
Correct |
1 ms |
492 KB |
Output is correct |