Submission #896373

#TimeUsernameProblemLanguageResultExecution timeMemory
896373DQCPotatoes and fertilizers (LMIO19_bulves)C++14
0 / 100
1 ms2396 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...