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 int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
int n,m;
cin>>n>>m;
vector <int> a(n+2),pr(n+1);
vector <int> b(n+1),r(n);
for(int i=1;i<=n;i++){
cin>>a[i];
pr[i]=pr[i-1]+a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
stack <int> st;
for(int i=1;i<=n+1;i++){
while(!st.empty() && b[st.top()]>=b[i]){
r[st.top()]=i-1;
st.pop();
}
st.push(i);
}
while(m--){
int s,t,u;
cin>>s>>t>>u;
bool ok=1;
int en=0,ans=0,ls=-1;
for(int i=s;i<t;i++){
int sum=pr[min(t-1,r[i])]-pr[i-1];
sum=max(0ll,sum-en);
ans+=min(sum,u-en)*b[i];
if(min(sum,u-en)>0)ls=b[i];
en+=min(sum,u-en);
en-=a[i];
if(en<0){
ok=0;
}
}
if(ok)cout<<ans<<"\n";
else cout<<-1<<"\n";
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:33:24: warning: variable 'ls' set but not used [-Wunused-but-set-variable]
33 | int en=0,ans=0,ls=-1;
| ^~
# | 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... |