#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;
int n;
long long a[N],ans,s;
priority_queue<long long> q;
vector<long long> v;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)
{
int x,y;
cin >> x >> y;
a[i] = x-y+a[i-1];
}
for(int i = 1;i <= n;i++)
{
if(a[i]<0) ans+=-a[i],a[i] = 0;
if(a[i]>a[n]) ans+=a[i]-a[n],a[i] = a[n];
q.push(a[i]),q.push(a[i]),q.pop();
s+=a[i];
}
while(!q.empty()) v.push_back(q.top()),q.pop();
reverse(v.begin(),v.end());
long long prev = 0;
for(auto x : v) s-=(n--)*(x-prev),prev = x;
cout << s+ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
16 ms |
2300 KB |
Output is correct |
5 |
Correct |
27 ms |
4084 KB |
Output is correct |
6 |
Correct |
90 ms |
10732 KB |
Output is correct |
7 |
Correct |
183 ms |
18444 KB |
Output is correct |
8 |
Correct |
162 ms |
18400 KB |
Output is correct |
9 |
Correct |
157 ms |
15796 KB |
Output is correct |
10 |
Correct |
123 ms |
15716 KB |
Output is correct |
11 |
Correct |
128 ms |
15712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
16 ms |
2300 KB |
Output is correct |
5 |
Correct |
27 ms |
4084 KB |
Output is correct |
6 |
Correct |
90 ms |
10732 KB |
Output is correct |
7 |
Correct |
183 ms |
18444 KB |
Output is correct |
8 |
Correct |
162 ms |
18400 KB |
Output is correct |
9 |
Correct |
157 ms |
15796 KB |
Output is correct |
10 |
Correct |
123 ms |
15716 KB |
Output is correct |
11 |
Correct |
128 ms |
15712 KB |
Output is correct |
12 |
Correct |
50 ms |
5232 KB |
Output is correct |
13 |
Correct |
113 ms |
12644 KB |
Output is correct |
14 |
Correct |
175 ms |
15716 KB |
Output is correct |
15 |
Correct |
149 ms |
15712 KB |
Output is correct |
16 |
Correct |
150 ms |
15836 KB |
Output is correct |
17 |
Correct |
138 ms |
15840 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
16 ms |
2300 KB |
Output is correct |
12 |
Correct |
27 ms |
4084 KB |
Output is correct |
13 |
Correct |
90 ms |
10732 KB |
Output is correct |
14 |
Correct |
183 ms |
18444 KB |
Output is correct |
15 |
Correct |
162 ms |
18400 KB |
Output is correct |
16 |
Correct |
157 ms |
15796 KB |
Output is correct |
17 |
Correct |
123 ms |
15716 KB |
Output is correct |
18 |
Correct |
128 ms |
15712 KB |
Output is correct |
19 |
Correct |
50 ms |
5232 KB |
Output is correct |
20 |
Correct |
113 ms |
12644 KB |
Output is correct |
21 |
Correct |
175 ms |
15716 KB |
Output is correct |
22 |
Correct |
149 ms |
15712 KB |
Output is correct |
23 |
Correct |
150 ms |
15836 KB |
Output is correct |
24 |
Correct |
138 ms |
15840 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
5 ms |
512 KB |
Output is correct |
31 |
Correct |
6 ms |
512 KB |
Output is correct |
32 |
Correct |
67 ms |
5232 KB |
Output is correct |
33 |
Correct |
152 ms |
12640 KB |
Output is correct |
34 |
Correct |
272 ms |
15712 KB |
Output is correct |
35 |
Correct |
239 ms |
15712 KB |
Output is correct |
36 |
Correct |
184 ms |
15716 KB |
Output is correct |
37 |
Correct |
258 ms |
15844 KB |
Output is correct |
38 |
Correct |
155 ms |
15836 KB |
Output is correct |
39 |
Correct |
178 ms |
15200 KB |
Output is correct |
40 |
Correct |
153 ms |
15840 KB |
Output is correct |
41 |
Correct |
147 ms |
15840 KB |
Output is correct |
42 |
Correct |
151 ms |
15840 KB |
Output is correct |
43 |
Correct |
155 ms |
16000 KB |
Output is correct |
44 |
Correct |
201 ms |
15968 KB |
Output is correct |
45 |
Correct |
169 ms |
15840 KB |
Output is correct |
46 |
Correct |
156 ms |
14176 KB |
Output is correct |
47 |
Correct |
5 ms |
512 KB |
Output is correct |