#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+1),pr(n+1);
vector <int> b(n+2),r(n+1);
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
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;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
604 KB |
Output is correct |
2 |
Correct |
12 ms |
600 KB |
Output is correct |
3 |
Correct |
13 ms |
616 KB |
Output is correct |
4 |
Correct |
14 ms |
600 KB |
Output is correct |
5 |
Correct |
12 ms |
604 KB |
Output is correct |
6 |
Correct |
13 ms |
660 KB |
Output is correct |
7 |
Correct |
18 ms |
604 KB |
Output is correct |
8 |
Correct |
13 ms |
604 KB |
Output is correct |
9 |
Correct |
12 ms |
640 KB |
Output is correct |
10 |
Correct |
16 ms |
604 KB |
Output is correct |
11 |
Correct |
12 ms |
620 KB |
Output is correct |
12 |
Correct |
13 ms |
860 KB |
Output is correct |
13 |
Correct |
15 ms |
600 KB |
Output is correct |
14 |
Correct |
13 ms |
604 KB |
Output is correct |
15 |
Correct |
12 ms |
628 KB |
Output is correct |
16 |
Correct |
12 ms |
604 KB |
Output is correct |
17 |
Correct |
13 ms |
608 KB |
Output is correct |
18 |
Correct |
11 ms |
604 KB |
Output is correct |
19 |
Correct |
13 ms |
604 KB |
Output is correct |
20 |
Correct |
20 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1851 ms |
4108 KB |
Output is correct |
2 |
Correct |
2903 ms |
4276 KB |
Output is correct |
3 |
Correct |
1634 ms |
4620 KB |
Output is correct |
4 |
Correct |
1852 ms |
4176 KB |
Output is correct |
5 |
Correct |
2277 ms |
4172 KB |
Output is correct |
6 |
Execution timed out |
4022 ms |
10064 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4045 ms |
9760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
604 KB |
Output is correct |
2 |
Correct |
12 ms |
600 KB |
Output is correct |
3 |
Correct |
13 ms |
616 KB |
Output is correct |
4 |
Correct |
14 ms |
600 KB |
Output is correct |
5 |
Correct |
12 ms |
604 KB |
Output is correct |
6 |
Correct |
13 ms |
660 KB |
Output is correct |
7 |
Correct |
18 ms |
604 KB |
Output is correct |
8 |
Correct |
13 ms |
604 KB |
Output is correct |
9 |
Correct |
12 ms |
640 KB |
Output is correct |
10 |
Correct |
16 ms |
604 KB |
Output is correct |
11 |
Correct |
12 ms |
620 KB |
Output is correct |
12 |
Correct |
13 ms |
860 KB |
Output is correct |
13 |
Correct |
15 ms |
600 KB |
Output is correct |
14 |
Correct |
13 ms |
604 KB |
Output is correct |
15 |
Correct |
12 ms |
628 KB |
Output is correct |
16 |
Correct |
12 ms |
604 KB |
Output is correct |
17 |
Correct |
13 ms |
608 KB |
Output is correct |
18 |
Correct |
11 ms |
604 KB |
Output is correct |
19 |
Correct |
13 ms |
604 KB |
Output is correct |
20 |
Correct |
20 ms |
604 KB |
Output is correct |
21 |
Correct |
1851 ms |
4108 KB |
Output is correct |
22 |
Correct |
2903 ms |
4276 KB |
Output is correct |
23 |
Correct |
1634 ms |
4620 KB |
Output is correct |
24 |
Correct |
1852 ms |
4176 KB |
Output is correct |
25 |
Correct |
2277 ms |
4172 KB |
Output is correct |
26 |
Execution timed out |
4022 ms |
10064 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |