///leeminhduc2
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define ii pair<int,int>
#define sz(x) (int) (x).size()
#define pb push_back
using namespace std;
template<class T1,class T2> bool maximize(T1 &a,T2 b) {return(a<b ? a=b,1:0);};
template<class T1,class T2> bool minimize(T1 &a,T2 b) {return(a>b ? a=b,1:0);};
int n;
ll a,b,lst,d[500010];
void leeminhduc2()
{
cin >> n;
a=0,b=0;
priority_queue<ll> pq;
for( int i=1;i<=n;i++)
{
ll u,v;
cin >> u >> v;
d[i]=u-v;
d[i]+=d[i-1];
//cout <<d[i] << " ";
}
for (int i=1;i<=n;i++)
{
ll x;
x=d[i];
if (x>=0){
++a;
pq.push(x);
pq.push(x);
b-=x;
}
else
{
++a;
pq.push(0ll);
b-=x;
}
if (i<n){
while (sz(pq)&&a>=0)
{
ll cur=pq.top(),cnt=0;
while (sz(pq)&&cur==pq.top())
{
++cnt;
pq.pop();
}
a-=cnt;
b+=cur*cnt;
lst=cur;
}
for (int j=1;j<=-a;j++) pq.push(lst);
b+=lst*a;
a=0;
}
else
{
while (sz(pq)&&pq.top()>=d[n])
{
ll cur=pq.top(),cnt=0;
while (sz(pq)&&cur==pq.top())
{
++cnt;
pq.pop();
}
a-=cnt;
b+=cur*cnt;
lst=cur;
}
}
}
cout << a*d[n]+b;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
leeminhduc2();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
299 ms |
1412 KB |
Output is correct |
5 |
Correct |
52 ms |
2808 KB |
Output is correct |
6 |
Correct |
162 ms |
7872 KB |
Output is correct |
7 |
Execution timed out |
1075 ms |
11064 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
299 ms |
1412 KB |
Output is correct |
5 |
Correct |
52 ms |
2808 KB |
Output is correct |
6 |
Correct |
162 ms |
7872 KB |
Output is correct |
7 |
Execution timed out |
1075 ms |
11064 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
7 ms |
424 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
65 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
424 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
65 ms |
384 KB |
Output is correct |
11 |
Correct |
9 ms |
428 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
77 ms |
412 KB |
Output is correct |
18 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
59 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
424 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
65 ms |
384 KB |
Output is correct |
11 |
Correct |
299 ms |
1412 KB |
Output is correct |
12 |
Correct |
52 ms |
2808 KB |
Output is correct |
13 |
Correct |
162 ms |
7872 KB |
Output is correct |
14 |
Execution timed out |
1075 ms |
11064 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |