#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=5e5;
int n, a[mxN], b[mxN];
priority_queue<ll> pq;
ll ans, shift;
void dbg() {
priority_queue<ll> pq2=pq;
vector<ll> vals;
while(pq2.size()) {
vals.push_back(pq2.top()+shift);
pq2.pop();
}
reverse(vals.begin(), vals.end());
cout << ans << "\n";
for (ll i : vals)
cout << i << " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
pq.push(0);
for (int i=0; i<n; ++i) {
cin >> a[i] >> b[i];
int d=b[i]-a[i];
pq.pop();
shift+=d;
ans+=max(0ll, pq.top()+shift);
if (shift>0)
pq.push(0);
else {
pq.push(-shift);
pq.push(-shift);
}
//dbg();
}
pq.pop();
ll last=pq.top()+shift, slope=0;
for (; last; pq.pop()) {
ll x=pq.top()+shift;
ans+=(last-x)*slope;
++slope;
last=x;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
12 ms |
1368 KB |
Output is correct |
5 |
Correct |
28 ms |
2816 KB |
Output is correct |
6 |
Correct |
90 ms |
7612 KB |
Output is correct |
7 |
Correct |
93 ms |
11464 KB |
Output is correct |
8 |
Correct |
161 ms |
13152 KB |
Output is correct |
9 |
Correct |
111 ms |
10104 KB |
Output is correct |
10 |
Correct |
94 ms |
10168 KB |
Output is correct |
11 |
Correct |
99 ms |
10136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
12 ms |
1368 KB |
Output is correct |
5 |
Correct |
28 ms |
2816 KB |
Output is correct |
6 |
Correct |
90 ms |
7612 KB |
Output is correct |
7 |
Correct |
93 ms |
11464 KB |
Output is correct |
8 |
Correct |
161 ms |
13152 KB |
Output is correct |
9 |
Correct |
111 ms |
10104 KB |
Output is correct |
10 |
Correct |
94 ms |
10168 KB |
Output is correct |
11 |
Correct |
99 ms |
10136 KB |
Output is correct |
12 |
Correct |
40 ms |
3848 KB |
Output is correct |
13 |
Correct |
111 ms |
8816 KB |
Output is correct |
14 |
Correct |
118 ms |
11724 KB |
Output is correct |
15 |
Correct |
159 ms |
13064 KB |
Output is correct |
16 |
Correct |
171 ms |
11708 KB |
Output is correct |
17 |
Correct |
85 ms |
10168 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
12 ms |
1368 KB |
Output is correct |
12 |
Correct |
28 ms |
2816 KB |
Output is correct |
13 |
Correct |
90 ms |
7612 KB |
Output is correct |
14 |
Correct |
93 ms |
11464 KB |
Output is correct |
15 |
Correct |
161 ms |
13152 KB |
Output is correct |
16 |
Correct |
111 ms |
10104 KB |
Output is correct |
17 |
Correct |
94 ms |
10168 KB |
Output is correct |
18 |
Correct |
99 ms |
10136 KB |
Output is correct |
19 |
Correct |
40 ms |
3848 KB |
Output is correct |
20 |
Correct |
111 ms |
8816 KB |
Output is correct |
21 |
Correct |
118 ms |
11724 KB |
Output is correct |
22 |
Correct |
159 ms |
13064 KB |
Output is correct |
23 |
Correct |
171 ms |
11708 KB |
Output is correct |
24 |
Correct |
85 ms |
10168 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
400 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
31 ms |
4032 KB |
Output is correct |
34 |
Correct |
85 ms |
10252 KB |
Output is correct |
35 |
Correct |
129 ms |
14940 KB |
Output is correct |
36 |
Correct |
122 ms |
12612 KB |
Output is correct |
37 |
Correct |
143 ms |
13112 KB |
Output is correct |
38 |
Correct |
129 ms |
14944 KB |
Output is correct |
39 |
Correct |
90 ms |
11188 KB |
Output is correct |
40 |
Correct |
95 ms |
10284 KB |
Output is correct |
41 |
Correct |
83 ms |
10140 KB |
Output is correct |
42 |
Correct |
81 ms |
10128 KB |
Output is correct |
43 |
Correct |
81 ms |
10272 KB |
Output is correct |
44 |
Correct |
86 ms |
10136 KB |
Output is correct |
45 |
Correct |
146 ms |
14120 KB |
Output is correct |
46 |
Correct |
106 ms |
10564 KB |
Output is correct |
47 |
Correct |
85 ms |
9144 KB |
Output is correct |