#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=5e5+5;
ll n, a[nx], b[nx], d[nx], res;
priority_queue<ll> pq;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i]>>b[i], d[i]=d[i-1]+a[i]-b[i];
for (int i=1; i<n; i++)
{
//cout<<"val "<<i<<' '<<d[i]<<' '<<d[n]<<'\n';
if (pq.empty())
{
if (d[i]<=0) res-=d[i];
else if (d[i]>=d[n]) res+=d[i]-d[n], pq.push(d[n]);
else pq.push(d[i]);
//cout<<i<<' '<<res<<'\n';
}
else
{
if (d[i]>=d[n]) res+=(d[i]-d[n]), pq.push(d[n]);
else if (d[i]>=pq.top()) pq.push(d[i]);
else if (d[i]<=0) res+=pq.top()-d[i], pq.pop();
else res+=pq.top()-d[i], pq.push(max(0ll, d[i])), pq.push(max(0ll, d[i])), pq.pop();
}
//priority_queue<ll> tmp=pq;
//cout<<"pq "<<res<<':';
//while (!tmp.empty()) cout<<tmp.top()<<' ', tmp.pop();
//cout<<'\n';
//cout<<i<<' '<<res<<'\n';
}
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
6 ms |
9052 KB |
Output is correct |
5 |
Correct |
12 ms |
11488 KB |
Output is correct |
6 |
Correct |
33 ms |
13004 KB |
Output is correct |
7 |
Correct |
64 ms |
12512 KB |
Output is correct |
8 |
Correct |
61 ms |
18376 KB |
Output is correct |
9 |
Correct |
57 ms |
12768 KB |
Output is correct |
10 |
Correct |
40 ms |
18124 KB |
Output is correct |
11 |
Correct |
41 ms |
17160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
6 ms |
9052 KB |
Output is correct |
5 |
Correct |
12 ms |
11488 KB |
Output is correct |
6 |
Correct |
33 ms |
13004 KB |
Output is correct |
7 |
Correct |
64 ms |
12512 KB |
Output is correct |
8 |
Correct |
61 ms |
18376 KB |
Output is correct |
9 |
Correct |
57 ms |
12768 KB |
Output is correct |
10 |
Correct |
40 ms |
18124 KB |
Output is correct |
11 |
Correct |
41 ms |
17160 KB |
Output is correct |
12 |
Correct |
23 ms |
11992 KB |
Output is correct |
13 |
Correct |
54 ms |
16900 KB |
Output is correct |
14 |
Correct |
67 ms |
19280 KB |
Output is correct |
15 |
Correct |
60 ms |
22812 KB |
Output is correct |
16 |
Correct |
63 ms |
22216 KB |
Output is correct |
17 |
Correct |
42 ms |
14816 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
2 ms |
4644 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
6 ms |
9052 KB |
Output is correct |
12 |
Correct |
12 ms |
11488 KB |
Output is correct |
13 |
Correct |
33 ms |
13004 KB |
Output is correct |
14 |
Correct |
64 ms |
12512 KB |
Output is correct |
15 |
Correct |
61 ms |
18376 KB |
Output is correct |
16 |
Correct |
57 ms |
12768 KB |
Output is correct |
17 |
Correct |
40 ms |
18124 KB |
Output is correct |
18 |
Correct |
41 ms |
17160 KB |
Output is correct |
19 |
Correct |
23 ms |
11992 KB |
Output is correct |
20 |
Correct |
54 ms |
16900 KB |
Output is correct |
21 |
Correct |
67 ms |
19280 KB |
Output is correct |
22 |
Correct |
60 ms |
22812 KB |
Output is correct |
23 |
Correct |
63 ms |
22216 KB |
Output is correct |
24 |
Correct |
42 ms |
14816 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
2 ms |
4644 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
28 ms |
13524 KB |
Output is correct |
34 |
Correct |
69 ms |
21196 KB |
Output is correct |
35 |
Correct |
121 ms |
23600 KB |
Output is correct |
36 |
Correct |
108 ms |
21196 KB |
Output is correct |
37 |
Correct |
63 ms |
22768 KB |
Output is correct |
38 |
Correct |
106 ms |
24008 KB |
Output is correct |
39 |
Correct |
71 ms |
20428 KB |
Output is correct |
40 |
Correct |
69 ms |
20172 KB |
Output is correct |
41 |
Correct |
71 ms |
19180 KB |
Output is correct |
42 |
Correct |
65 ms |
20084 KB |
Output is correct |
43 |
Correct |
60 ms |
16336 KB |
Output is correct |
44 |
Correct |
61 ms |
16124 KB |
Output is correct |
45 |
Correct |
79 ms |
25024 KB |
Output is correct |
46 |
Correct |
53 ms |
19452 KB |
Output is correct |
47 |
Correct |
72 ms |
19692 KB |
Output is correct |