#include <bits/stdc++.h>
using namespace std;
struct Line {
long long m, c;
Line() {}
Line(long long mm, long long cc) : m(mm), c(cc) {}
inline long long getY(long long x) { return m*x+c; }
};
const int N = (int)5e5;
int n, a[N], b[N];
long long d[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0;i < n;i ++) {
cin >> a[i] >> b[i];
d[i] = a[i] - b[i];
if (i != 0) d[i] += d[i - 1];
}
Line line;
priority_queue<long long> pq;
if (d[0] > 0) {
pq.push(d[0]);
line = Line(-1, d[0]);
} else {
line = Line(0, -d[0]);
}
for (int i = 1;i < n - 1;i ++) {
if (d[i] >= 0) {
pq.push(d[i]);
pq.push(d[i]);
pq.pop();
line.m --;
line.c += d[i];
} else {
if (line.m + 1 > 0) {
line.m = 0;
line.c += -d[i];
while ((int)pq.size()) pq.pop();
} else {
line.m ++;
line.c += -d[i];
pq.pop();
}
}
}
vector<long long> val;
while ((int)pq.size()) {
val.push_back(pq.top());
pq.pop();
}
sort(val.begin(), val.end());
for (auto& e : val) {
if (e >= d[n - 1]) break;
long long y = line.getY(e);
line.m ++;
line.c = y - line.m * e;
}
cout << line.getY(d[n - 1]) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
16 ms |
1616 KB |
Output is correct |
5 |
Correct |
34 ms |
2988 KB |
Output is correct |
6 |
Correct |
117 ms |
9116 KB |
Output is correct |
7 |
Correct |
106 ms |
8516 KB |
Output is correct |
8 |
Correct |
217 ms |
18424 KB |
Output is correct |
9 |
Correct |
119 ms |
9284 KB |
Output is correct |
10 |
Correct |
144 ms |
18112 KB |
Output is correct |
11 |
Correct |
148 ms |
18120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
16 ms |
1616 KB |
Output is correct |
5 |
Correct |
34 ms |
2988 KB |
Output is correct |
6 |
Correct |
117 ms |
9116 KB |
Output is correct |
7 |
Correct |
106 ms |
8516 KB |
Output is correct |
8 |
Correct |
217 ms |
18424 KB |
Output is correct |
9 |
Correct |
119 ms |
9284 KB |
Output is correct |
10 |
Correct |
144 ms |
18112 KB |
Output is correct |
11 |
Correct |
148 ms |
18120 KB |
Output is correct |
12 |
Correct |
54 ms |
4488 KB |
Output is correct |
13 |
Correct |
130 ms |
9888 KB |
Output is correct |
14 |
Correct |
111 ms |
8428 KB |
Output is correct |
15 |
Correct |
213 ms |
18204 KB |
Output is correct |
16 |
Correct |
208 ms |
16832 KB |
Output is correct |
17 |
Correct |
148 ms |
18208 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
16 ms |
1616 KB |
Output is correct |
12 |
Correct |
34 ms |
2988 KB |
Output is correct |
13 |
Correct |
117 ms |
9116 KB |
Output is correct |
14 |
Correct |
106 ms |
8516 KB |
Output is correct |
15 |
Correct |
217 ms |
18424 KB |
Output is correct |
16 |
Correct |
119 ms |
9284 KB |
Output is correct |
17 |
Correct |
144 ms |
18112 KB |
Output is correct |
18 |
Correct |
148 ms |
18120 KB |
Output is correct |
19 |
Correct |
54 ms |
4488 KB |
Output is correct |
20 |
Correct |
130 ms |
9888 KB |
Output is correct |
21 |
Correct |
111 ms |
8428 KB |
Output is correct |
22 |
Correct |
213 ms |
18204 KB |
Output is correct |
23 |
Correct |
208 ms |
16832 KB |
Output is correct |
24 |
Correct |
148 ms |
18208 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
57 ms |
6432 KB |
Output is correct |
34 |
Correct |
152 ms |
17616 KB |
Output is correct |
35 |
Correct |
249 ms |
24788 KB |
Output is correct |
36 |
Correct |
243 ms |
22464 KB |
Output is correct |
37 |
Correct |
220 ms |
23020 KB |
Output is correct |
38 |
Correct |
247 ms |
24888 KB |
Output is correct |
39 |
Correct |
174 ms |
21124 KB |
Output is correct |
40 |
Correct |
189 ms |
19900 KB |
Output is correct |
41 |
Correct |
150 ms |
20076 KB |
Output is correct |
42 |
Correct |
149 ms |
20124 KB |
Output is correct |
43 |
Correct |
147 ms |
20068 KB |
Output is correct |
44 |
Correct |
145 ms |
20172 KB |
Output is correct |
45 |
Correct |
215 ms |
23264 KB |
Output is correct |
46 |
Correct |
188 ms |
20420 KB |
Output is correct |
47 |
Correct |
166 ms |
18200 KB |
Output is correct |