#include<bits/stdc++.h>
using namespace std;
int n,a,b[500005],c[500005],id,val;
vector<pair<int,int> > va,slv;
set<int> vb;
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>b[i]>>a;
a -= min(a,b[i]);
b[i] -= min(a,b[i]);
if(a>0){
va.push_back(make_pair(i,a));
}
if(b[i]>0){
vb.insert(i);
}
}
long long aa=0;
memset(c,0,sizeof c);
for(auto x : va){
id = x.first;
val = x.second;
aa += val;
//cout<<id<<"-id val-"<<val<<endl;;
while(val>0){
auto kanan = vb.lower_bound(id);
//cout<<*kanan<<endl;
if(kanan == vb.begin()){
//cout<<"depan"<<endl;
if(b[*kanan] <= val){
val -= b[*kanan];
vb.erase(kanan);
//slv.push_back(make_pair(*kanan,b[*kanan]));
c[*kanan] += b[*kanan];
}
else{
b[*kanan] -= val;
//slv.push_back(make_pair(*kanan,val));
c[*kanan] += val;
val = 0;
}
continue;
}
if(kanan == vb.end()){
//cout<<"belakang"<<endl;
kanan--;
if(b[*kanan] <= val){
val -= b[*kanan];
vb.erase(kanan);
//slv.push_back(make_pair(*kanan,b[*kanan]));
c[*kanan] += b[*kanan];
}
else{
b[*kanan] -= val;
//slv.push_back(make_pair(*kanan,val));
c[*kanan] += val;
val = 0;
}
continue;
}
auto kiri = kanan;
kiri--;
//cout<<*kiri<<' '<<*kanan<<endl;
if(id - *kiri > *kanan - id){
if(b[*kanan] <= val){
val -= b[*kanan];
vb.erase(kanan);
c[*kanan] += b[*kanan];
//slv.push_back(make_pair(*kanan,b[*kanan]));
}
else{
b[*kanan] -= val;
//slv.push_back(make_pair(*kanan,val));
c[*kanan] += val;
val = 0;
}
}
else{
if(b[*kiri] <= val){
val -= b[*kiri];
vb.erase(kiri);
//slv.push_back(make_pair(*kiri,b[*kiri]));
c[*kiri] += b[*kiri];
}
else{
b[*kiri] -= val;
//slv.push_back(make_pair(*kiri,val));
c[*kiri] += val;
val = 0;
}
}
}
}
long long cc=0;;
for(int i=1;i<=n;i++){
cc += c[i];
}
assert(cc==aa);
int v;
long long ans=0;
v = 1;
for(auto x: va){
while(c[v]<=x.second && v<n){
ans += c[v] * 1ll * abs(v-x.first);
x.second -= c[v];
v++;
}
ans += x.second * 1ll * abs(v-x.first);
c[v] -= x.second;
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2304 KB |
Output is correct |
2 |
Correct |
1 ms |
2304 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2304 KB |
Output is correct |
2 |
Correct |
1 ms |
2304 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2304 KB |
Output is correct |
2 |
Correct |
1 ms |
2304 KB |
Output is correct |
3 |
Correct |
1 ms |
2304 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2304 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2304 KB |
Output is correct |
2 |
Correct |
1 ms |
2304 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2304 KB |
Output is correct |
2 |
Correct |
1 ms |
2304 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2432 KB |
Output isn't correct |