#include <iostream>
#include <queue>
using namespace std;
const int nmax=500005;
priority_queue<long long> pq;
long long c[nmax];
long long ans;
int n,i,j,a,b;
int main()
{
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a>>b;
long long dif=a-b;
c[i]=(c[i-1]+dif);
}
for(i=1;i<=n;i++)
{
if(c[i]<0) ans-=c[i],c[i]=0;
if(c[i]>c[n]) ans+=(c[i]-c[n]),c[i]=c[n];
}
for(i=1;i<=n;i++)
{
pq.push(c[i]);
if(pq.top()>c[i])
{
pq.push(c[i]);
ans+=1LL*pq.top()-c[i];
pq.pop();
}
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
37 ms |
1624 KB |
Output is correct |
5 |
Correct |
72 ms |
2896 KB |
Output is correct |
6 |
Correct |
285 ms |
7916 KB |
Output is correct |
7 |
Correct |
563 ms |
15204 KB |
Output is correct |
8 |
Correct |
464 ms |
13484 KB |
Output is correct |
9 |
Correct |
426 ms |
12872 KB |
Output is correct |
10 |
Correct |
270 ms |
10724 KB |
Output is correct |
11 |
Correct |
290 ms |
10468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
37 ms |
1624 KB |
Output is correct |
5 |
Correct |
72 ms |
2896 KB |
Output is correct |
6 |
Correct |
285 ms |
7916 KB |
Output is correct |
7 |
Correct |
563 ms |
15204 KB |
Output is correct |
8 |
Correct |
464 ms |
13484 KB |
Output is correct |
9 |
Correct |
426 ms |
12872 KB |
Output is correct |
10 |
Correct |
270 ms |
10724 KB |
Output is correct |
11 |
Correct |
290 ms |
10468 KB |
Output is correct |
12 |
Correct |
143 ms |
4080 KB |
Output is correct |
13 |
Correct |
345 ms |
10980 KB |
Output is correct |
14 |
Correct |
573 ms |
15324 KB |
Output is correct |
15 |
Correct |
464 ms |
13452 KB |
Output is correct |
16 |
Correct |
421 ms |
12764 KB |
Output is correct |
17 |
Correct |
282 ms |
10560 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
4 ms |
436 KB |
Output is correct |
7 |
Correct |
3 ms |
424 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
4 ms |
436 KB |
Output is correct |
8 |
Correct |
3 ms |
424 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
4 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
5 ms |
504 KB |
Output is correct |
15 |
Correct |
5 ms |
504 KB |
Output is correct |
16 |
Correct |
4 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
4 ms |
436 KB |
Output is correct |
8 |
Correct |
3 ms |
424 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
37 ms |
1624 KB |
Output is correct |
12 |
Correct |
72 ms |
2896 KB |
Output is correct |
13 |
Correct |
285 ms |
7916 KB |
Output is correct |
14 |
Correct |
563 ms |
15204 KB |
Output is correct |
15 |
Correct |
464 ms |
13484 KB |
Output is correct |
16 |
Correct |
426 ms |
12872 KB |
Output is correct |
17 |
Correct |
270 ms |
10724 KB |
Output is correct |
18 |
Correct |
290 ms |
10468 KB |
Output is correct |
19 |
Correct |
143 ms |
4080 KB |
Output is correct |
20 |
Correct |
345 ms |
10980 KB |
Output is correct |
21 |
Correct |
573 ms |
15324 KB |
Output is correct |
22 |
Correct |
464 ms |
13452 KB |
Output is correct |
23 |
Correct |
421 ms |
12764 KB |
Output is correct |
24 |
Correct |
282 ms |
10560 KB |
Output is correct |
25 |
Correct |
4 ms |
376 KB |
Output is correct |
26 |
Correct |
6 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
504 KB |
Output is correct |
30 |
Correct |
4 ms |
376 KB |
Output is correct |
31 |
Correct |
6 ms |
376 KB |
Output is correct |
32 |
Correct |
157 ms |
4188 KB |
Output is correct |
33 |
Correct |
381 ms |
11040 KB |
Output is correct |
34 |
Correct |
629 ms |
15332 KB |
Output is correct |
35 |
Correct |
489 ms |
12900 KB |
Output is correct |
36 |
Correct |
462 ms |
13412 KB |
Output is correct |
37 |
Correct |
639 ms |
15200 KB |
Output is correct |
38 |
Correct |
357 ms |
11556 KB |
Output is correct |
39 |
Correct |
373 ms |
10920 KB |
Output is correct |
40 |
Correct |
307 ms |
10468 KB |
Output is correct |
41 |
Correct |
306 ms |
10544 KB |
Output is correct |
42 |
Correct |
309 ms |
10468 KB |
Output is correct |
43 |
Correct |
305 ms |
10472 KB |
Output is correct |
44 |
Correct |
601 ms |
15196 KB |
Output is correct |
45 |
Correct |
298 ms |
10948 KB |
Output is correct |
46 |
Correct |
330 ms |
10440 KB |
Output is correct |
47 |
Correct |
4 ms |
376 KB |
Output is correct |