This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
using ll=long long;
int n;
int a[500005],b[500005];
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
//freopen(".inp","r",stdin);
//freopen(".out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
int ans=0;
for(int i=1;i<=n;i++) if(a[i]<b[i])
{
for(int l=i-1,r=i+1;a[i]<b[i]&&(l>0||r<=n);l--,r++)
{
if(l>0)
{
int cnt=min(a[l]-b[l],b[i]-a[i]);
ans+=cnt*(i-l);
a[l]-=cnt;
a[i]+=cnt;
}
if(a[i]>=b[i]) break;
if(r<=n)
{
int cnt=min(a[r],b[i]-a[i]);
ans+=cnt*(r-i);
a[r]-=cnt;
a[i]+=cnt;
}
}
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |