#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()
{
ios_base::sync_with_stdio(false);
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 |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
13 ms |
1784 KB |
Output is correct |
5 |
Correct |
23 ms |
2944 KB |
Output is correct |
6 |
Correct |
68 ms |
7868 KB |
Output is correct |
7 |
Correct |
134 ms |
15340 KB |
Output is correct |
8 |
Correct |
113 ms |
13340 KB |
Output is correct |
9 |
Correct |
110 ms |
12748 KB |
Output is correct |
10 |
Correct |
85 ms |
10472 KB |
Output is correct |
11 |
Correct |
86 ms |
10404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
13 ms |
1784 KB |
Output is correct |
5 |
Correct |
23 ms |
2944 KB |
Output is correct |
6 |
Correct |
68 ms |
7868 KB |
Output is correct |
7 |
Correct |
134 ms |
15340 KB |
Output is correct |
8 |
Correct |
113 ms |
13340 KB |
Output is correct |
9 |
Correct |
110 ms |
12748 KB |
Output is correct |
10 |
Correct |
85 ms |
10472 KB |
Output is correct |
11 |
Correct |
86 ms |
10404 KB |
Output is correct |
12 |
Correct |
37 ms |
4140 KB |
Output is correct |
13 |
Correct |
91 ms |
10920 KB |
Output is correct |
14 |
Correct |
151 ms |
15312 KB |
Output is correct |
15 |
Correct |
123 ms |
13544 KB |
Output is correct |
16 |
Correct |
120 ms |
12784 KB |
Output is correct |
17 |
Correct |
110 ms |
10476 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
4 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
13 ms |
1784 KB |
Output is correct |
12 |
Correct |
23 ms |
2944 KB |
Output is correct |
13 |
Correct |
68 ms |
7868 KB |
Output is correct |
14 |
Correct |
134 ms |
15340 KB |
Output is correct |
15 |
Correct |
113 ms |
13340 KB |
Output is correct |
16 |
Correct |
110 ms |
12748 KB |
Output is correct |
17 |
Correct |
85 ms |
10472 KB |
Output is correct |
18 |
Correct |
86 ms |
10404 KB |
Output is correct |
19 |
Correct |
37 ms |
4140 KB |
Output is correct |
20 |
Correct |
91 ms |
10920 KB |
Output is correct |
21 |
Correct |
151 ms |
15312 KB |
Output is correct |
22 |
Correct |
123 ms |
13544 KB |
Output is correct |
23 |
Correct |
120 ms |
12784 KB |
Output is correct |
24 |
Correct |
110 ms |
10476 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
4 ms |
504 KB |
Output is correct |
28 |
Correct |
3 ms |
504 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
3 ms |
504 KB |
Output is correct |
32 |
Correct |
49 ms |
4340 KB |
Output is correct |
33 |
Correct |
121 ms |
10980 KB |
Output is correct |
34 |
Correct |
250 ms |
15284 KB |
Output is correct |
35 |
Correct |
190 ms |
12792 KB |
Output is correct |
36 |
Correct |
123 ms |
13320 KB |
Output is correct |
37 |
Correct |
190 ms |
15288 KB |
Output is correct |
38 |
Correct |
118 ms |
11368 KB |
Output is correct |
39 |
Correct |
124 ms |
11064 KB |
Output is correct |
40 |
Correct |
113 ms |
10472 KB |
Output is correct |
41 |
Correct |
116 ms |
10472 KB |
Output is correct |
42 |
Correct |
119 ms |
10476 KB |
Output is correct |
43 |
Correct |
119 ms |
10472 KB |
Output is correct |
44 |
Correct |
150 ms |
15208 KB |
Output is correct |
45 |
Correct |
107 ms |
10728 KB |
Output is correct |
46 |
Correct |
122 ms |
10344 KB |
Output is correct |
47 |
Correct |
3 ms |
376 KB |
Output is correct |