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>
using namespace std;
long long a,b,c,t,n,x,y,m,q,store,st2;
vector<long long>v1,v2;
stack<pair<long long,long long>>s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
long long res=0;
for(long long i=0;i<n;i++){
cin>>a;
v1.push_back(a);
}
for(long long i=0;i<n;i++){
cin>>a;
v2.push_back(a);
}
long long pt1=n-1,pt2=n-1;
if(a)
s.push({a,n-1});
while(pt1>=0){
//cout<<pt1<<" "<<pt2<<" ";
if(pt1>=pt2 || v1[pt1]==0){
pt1--;
continue;
}
while(pt2-1>pt1){
pt2--;
if(v2[pt2]>0)
s.push({v2[pt2],pt2});
}
if(s.empty()){
pt2--;
if(v2[pt2]>0)
s.push({v2[pt2],pt2});
continue;}
if(s.top().first>v1[pt1]){
s.top().first-=v1[pt1];
res+=v1[pt1];
v1[pt1]=0;
}
else{
v1[pt1]-=s.top().first;
res+=s.top().first;
s.pop();
}
}
pt2--;
while(pt2>=0){
s.push({v2[pt2],pt2});
pt2--;}
//cout<<res;
while(s.size()>0){
//cout<<s.top().first<<" "<<s.top().second<<" ";
res-=max(0LL,s.top().first-v1[s.top().second]);
s.pop();
}
cout<<res;
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... |