#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int v[500005];
struct slopetrick
{
int a,b;
priority_queue<int> *s;
slopetrick()
{
a = b = 0;
s = new priority_queue<int>;
}
void operator += (slopetrick other)
{
a += other.a;
b += other.b;
if(s->size()<other.s->size())
{
swap(s,other.s);
}
while(other.s->size())
{
s->push(other.s->top());
other.s->pop();
}
}
void elim()
{
while(a>0)
{
b += s->top();
s->pop();
--a;
}
}
};
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
v[i] = v[i-1] + a - b;
}
slopetrick rez;
int val = 0;
for(int i=1; i<n; i++)
{
slopetrick aux;
if(v[i]<0)
{
val += abs(v[i]);
aux.a = 1, aux.b = 0;
aux.s->push(0);
aux.s->push(0);
}
else if(v[i]>v[n])
{
val += v[i] - v[n];
aux.a = 1, aux.b = -v[n];
aux.s->push(v[n]);
aux.s->push(v[n]);
}
else
{
aux.a = 1, aux.b = -v[i];
aux.s->push(v[i]);
aux.s->push(v[i]);
}
rez += aux;
rez.elim();
}
val += rez.b;
cout<<val<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
14 ms |
5032 KB |
Output is correct |
5 |
Correct |
28 ms |
9624 KB |
Output is correct |
6 |
Correct |
85 ms |
23724 KB |
Output is correct |
7 |
Correct |
175 ms |
47168 KB |
Output is correct |
8 |
Correct |
161 ms |
47248 KB |
Output is correct |
9 |
Correct |
140 ms |
47200 KB |
Output is correct |
10 |
Correct |
128 ms |
47256 KB |
Output is correct |
11 |
Correct |
126 ms |
47216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
14 ms |
5032 KB |
Output is correct |
5 |
Correct |
28 ms |
9624 KB |
Output is correct |
6 |
Correct |
85 ms |
23724 KB |
Output is correct |
7 |
Correct |
175 ms |
47168 KB |
Output is correct |
8 |
Correct |
161 ms |
47248 KB |
Output is correct |
9 |
Correct |
140 ms |
47200 KB |
Output is correct |
10 |
Correct |
128 ms |
47256 KB |
Output is correct |
11 |
Correct |
126 ms |
47216 KB |
Output is correct |
12 |
Correct |
50 ms |
12000 KB |
Output is correct |
13 |
Correct |
112 ms |
32464 KB |
Output is correct |
14 |
Correct |
168 ms |
54008 KB |
Output is correct |
15 |
Correct |
147 ms |
52064 KB |
Output is correct |
16 |
Correct |
147 ms |
51500 KB |
Output is correct |
17 |
Correct |
172 ms |
49240 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
624 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
14 ms |
5032 KB |
Output is correct |
12 |
Correct |
28 ms |
9624 KB |
Output is correct |
13 |
Correct |
85 ms |
23724 KB |
Output is correct |
14 |
Correct |
175 ms |
47168 KB |
Output is correct |
15 |
Correct |
161 ms |
47248 KB |
Output is correct |
16 |
Correct |
140 ms |
47200 KB |
Output is correct |
17 |
Correct |
128 ms |
47256 KB |
Output is correct |
18 |
Correct |
126 ms |
47216 KB |
Output is correct |
19 |
Correct |
50 ms |
12000 KB |
Output is correct |
20 |
Correct |
112 ms |
32464 KB |
Output is correct |
21 |
Correct |
168 ms |
54008 KB |
Output is correct |
22 |
Correct |
147 ms |
52064 KB |
Output is correct |
23 |
Correct |
147 ms |
51500 KB |
Output is correct |
24 |
Correct |
172 ms |
49240 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
624 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
57 ms |
13756 KB |
Output is correct |
34 |
Correct |
131 ms |
32608 KB |
Output is correct |
35 |
Correct |
211 ms |
53936 KB |
Output is correct |
36 |
Correct |
189 ms |
51524 KB |
Output is correct |
37 |
Correct |
174 ms |
52120 KB |
Output is correct |
38 |
Correct |
247 ms |
53992 KB |
Output is correct |
39 |
Correct |
152 ms |
50160 KB |
Output is correct |
40 |
Correct |
157 ms |
45696 KB |
Output is correct |
41 |
Correct |
156 ms |
49316 KB |
Output is correct |
42 |
Correct |
158 ms |
49136 KB |
Output is correct |
43 |
Correct |
170 ms |
49328 KB |
Output is correct |
44 |
Correct |
144 ms |
49188 KB |
Output is correct |
45 |
Correct |
179 ms |
53992 KB |
Output is correct |
46 |
Correct |
170 ms |
49524 KB |
Output is correct |
47 |
Correct |
136 ms |
39600 KB |
Output is correct |