#include <iostream>
#include <vector>
#include <set>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define sz(x) int(x.size())
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vll x(1+N);
ll basicCost = 0;
x[0] = 0;
for(int i = 1; i <= N; i++)
{
ll a, b;
cin >> a >> b;
x[i] = x[i-1] + a - b;
}
for(int i = 1; i < N; i++)
{
if(x[i] < 0)
{
basicCost += 0 - x[i];
x[i] = 0;
}
if(x[i] > x[N])
{
basicCost += x[i] - x[N];
x[i] = x[N];
}
}
ll ans0 = 0;
multiset<ll> points;
for(int i = 1; i <= N; i++)
{
ans0 += x[i];
points.insert(x[i]);
points.insert(x[i]);
points.erase(points.find(*points.rbegin()));
}
ll curr_sl = -N;
ll answer = basicCost + ans0;
points.insert(0);
while(sz(points) > 1)
{
ll u = *points.begin();
points.erase(points.begin());
ll v = *points.begin();
answer += (v - u) * curr_sl;
curr_sl++;
}
cout << answer << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
21 ms |
3268 KB |
Output is correct |
5 |
Correct |
46 ms |
6332 KB |
Output is correct |
6 |
Correct |
141 ms |
17340 KB |
Output is correct |
7 |
Correct |
298 ms |
34364 KB |
Output is correct |
8 |
Correct |
281 ms |
32708 KB |
Output is correct |
9 |
Correct |
276 ms |
31852 KB |
Output is correct |
10 |
Correct |
272 ms |
29720 KB |
Output is correct |
11 |
Correct |
259 ms |
29556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
21 ms |
3268 KB |
Output is correct |
5 |
Correct |
46 ms |
6332 KB |
Output is correct |
6 |
Correct |
141 ms |
17340 KB |
Output is correct |
7 |
Correct |
298 ms |
34364 KB |
Output is correct |
8 |
Correct |
281 ms |
32708 KB |
Output is correct |
9 |
Correct |
276 ms |
31852 KB |
Output is correct |
10 |
Correct |
272 ms |
29720 KB |
Output is correct |
11 |
Correct |
259 ms |
29556 KB |
Output is correct |
12 |
Correct |
60 ms |
8828 KB |
Output is correct |
13 |
Correct |
172 ms |
20756 KB |
Output is correct |
14 |
Correct |
247 ms |
34360 KB |
Output is correct |
15 |
Correct |
289 ms |
32880 KB |
Output is correct |
16 |
Correct |
243 ms |
31916 KB |
Output is correct |
17 |
Correct |
236 ms |
29552 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
2 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
2 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
448 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
452 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
2 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
21 ms |
3268 KB |
Output is correct |
12 |
Correct |
46 ms |
6332 KB |
Output is correct |
13 |
Correct |
141 ms |
17340 KB |
Output is correct |
14 |
Correct |
298 ms |
34364 KB |
Output is correct |
15 |
Correct |
281 ms |
32708 KB |
Output is correct |
16 |
Correct |
276 ms |
31852 KB |
Output is correct |
17 |
Correct |
272 ms |
29720 KB |
Output is correct |
18 |
Correct |
259 ms |
29556 KB |
Output is correct |
19 |
Correct |
60 ms |
8828 KB |
Output is correct |
20 |
Correct |
172 ms |
20756 KB |
Output is correct |
21 |
Correct |
247 ms |
34360 KB |
Output is correct |
22 |
Correct |
289 ms |
32880 KB |
Output is correct |
23 |
Correct |
243 ms |
31916 KB |
Output is correct |
24 |
Correct |
236 ms |
29552 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
448 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
452 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
504 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
57 ms |
8820 KB |
Output is correct |
34 |
Correct |
143 ms |
20688 KB |
Output is correct |
35 |
Correct |
276 ms |
34428 KB |
Output is correct |
36 |
Correct |
289 ms |
31940 KB |
Output is correct |
37 |
Correct |
302 ms |
32796 KB |
Output is correct |
38 |
Correct |
245 ms |
34468 KB |
Output is correct |
39 |
Correct |
300 ms |
30612 KB |
Output is correct |
40 |
Correct |
200 ms |
27880 KB |
Output is correct |
41 |
Correct |
316 ms |
29716 KB |
Output is correct |
42 |
Correct |
291 ms |
29672 KB |
Output is correct |
43 |
Correct |
274 ms |
29632 KB |
Output is correct |
44 |
Correct |
258 ms |
29624 KB |
Output is correct |
45 |
Correct |
290 ms |
34424 KB |
Output is correct |
46 |
Correct |
220 ms |
29940 KB |
Output is correct |
47 |
Correct |
172 ms |
24260 KB |
Output is correct |