#include <bits/stdc++.h>
using namespace std;
constexpr int NMAX = 5e5 + 5;
typedef long long LL;
struct SlopeTrick {
LL a, b;
priority_queue <LL> *H;
SlopeTrick () {
a = b = 0;
H = new priority_queue<LL>;
}
void operator += (SlopeTrick other) {
a += other.a;
b += other.b;
if (H->size() > other.H->size())
swap(H, other.H);
while (!other.H->empty()) {
H->push(other.H->top());
other.H->pop();
}
}
void Evaluate () {
while (a > 0) {
-- a;
b += H->top();
H->pop();
}
}
};
int N;
LL D[NMAX];
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 1; i <= N; ++ i ) {
LL A, B;
cin >> A >> B;
D[i] = D[i-1] + A - B;
}
}
void Solve () {
SlopeTrick ans;
for (int i = 1; i < N; ++ i ) {
ans.a ++;
if (D[i] > D[N]) {
ans.b += (D[i] - D[N]);
D[i] = D[N];
}
ans.b += (-D[i]);
if (D[i] < 0) D[i] = 0;
ans.H->push(D[i]);
ans.H->push(D[i]);
ans.Evaluate();
}
cout << ans.b << '\n';
}
int main () {
Read();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
10 ms |
1660 KB |
Output is correct |
5 |
Correct |
16 ms |
2844 KB |
Output is correct |
6 |
Correct |
52 ms |
7792 KB |
Output is correct |
7 |
Correct |
111 ms |
15176 KB |
Output is correct |
8 |
Correct |
117 ms |
13356 KB |
Output is correct |
9 |
Correct |
113 ms |
12592 KB |
Output is correct |
10 |
Correct |
72 ms |
10320 KB |
Output is correct |
11 |
Correct |
72 ms |
10380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
10 ms |
1660 KB |
Output is correct |
5 |
Correct |
16 ms |
2844 KB |
Output is correct |
6 |
Correct |
52 ms |
7792 KB |
Output is correct |
7 |
Correct |
111 ms |
15176 KB |
Output is correct |
8 |
Correct |
117 ms |
13356 KB |
Output is correct |
9 |
Correct |
113 ms |
12592 KB |
Output is correct |
10 |
Correct |
72 ms |
10320 KB |
Output is correct |
11 |
Correct |
72 ms |
10380 KB |
Output is correct |
12 |
Correct |
27 ms |
4136 KB |
Output is correct |
13 |
Correct |
81 ms |
10972 KB |
Output is correct |
14 |
Correct |
124 ms |
15172 KB |
Output is correct |
15 |
Correct |
88 ms |
13380 KB |
Output is correct |
16 |
Correct |
88 ms |
12732 KB |
Output is correct |
17 |
Correct |
83 ms |
10452 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 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 |
2 ms |
340 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 |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
1660 KB |
Output is correct |
12 |
Correct |
16 ms |
2844 KB |
Output is correct |
13 |
Correct |
52 ms |
7792 KB |
Output is correct |
14 |
Correct |
111 ms |
15176 KB |
Output is correct |
15 |
Correct |
117 ms |
13356 KB |
Output is correct |
16 |
Correct |
113 ms |
12592 KB |
Output is correct |
17 |
Correct |
72 ms |
10320 KB |
Output is correct |
18 |
Correct |
72 ms |
10380 KB |
Output is correct |
19 |
Correct |
27 ms |
4136 KB |
Output is correct |
20 |
Correct |
81 ms |
10972 KB |
Output is correct |
21 |
Correct |
124 ms |
15172 KB |
Output is correct |
22 |
Correct |
88 ms |
13380 KB |
Output is correct |
23 |
Correct |
88 ms |
12732 KB |
Output is correct |
24 |
Correct |
83 ms |
10452 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 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 |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
39 ms |
4044 KB |
Output is correct |
34 |
Correct |
86 ms |
10860 KB |
Output is correct |
35 |
Correct |
146 ms |
15152 KB |
Output is correct |
36 |
Correct |
129 ms |
12868 KB |
Output is correct |
37 |
Correct |
122 ms |
13260 KB |
Output is correct |
38 |
Correct |
132 ms |
15168 KB |
Output is correct |
39 |
Correct |
87 ms |
11320 KB |
Output is correct |
40 |
Correct |
98 ms |
10940 KB |
Output is correct |
41 |
Correct |
93 ms |
10436 KB |
Output is correct |
42 |
Correct |
95 ms |
10436 KB |
Output is correct |
43 |
Correct |
80 ms |
10392 KB |
Output is correct |
44 |
Correct |
85 ms |
10420 KB |
Output is correct |
45 |
Correct |
115 ms |
15144 KB |
Output is correct |
46 |
Correct |
137 ms |
10688 KB |
Output is correct |
47 |
Correct |
100 ms |
10328 KB |
Output is correct |