#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(),a.end()
const int LIM=2e5+7;
ll A[LIM], pos[LIM], B[LIM], mi[LIM];
ll solve(int l, int r, ll u) {
ll ans=0, akt=0;
while(l<r) {
if(min(pos[mi[l]], pos[r])-pos[l]>akt) {
ans+=B[l]*min(min(pos[mi[l]], pos[r])-pos[l]-akt, u-akt);
akt=min(min(pos[mi[l]], pos[r])-pos[l], u);
}
akt-=A[l];
++l;
if(akt<0) return -1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
rep(i, n) {
cin >> A[i];
pos[i+1]=pos[i]+A[i];
}
rep(i, n) cin >> B[i];
A[n]=-1;
stack<pair<ll,ll>>S;
S.push({-1, n});
for(int i=n-1; i>=0; --i) {
while(S.top().st>B[i]) S.pop();
mi[i]=S.top().nd;
S.push({B[i], i});
}
while(m--) {
ll l, r, u;
cin >> l >> r >> u; --l; --r;
cout << solve(l, r, u) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
8 ms |
548 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
10 ms |
472 KB |
Output is correct |
5 |
Correct |
9 ms |
576 KB |
Output is correct |
6 |
Correct |
10 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
444 KB |
Output is correct |
8 |
Correct |
8 ms |
560 KB |
Output is correct |
9 |
Correct |
9 ms |
596 KB |
Output is correct |
10 |
Correct |
9 ms |
452 KB |
Output is correct |
11 |
Correct |
9 ms |
580 KB |
Output is correct |
12 |
Correct |
9 ms |
592 KB |
Output is correct |
13 |
Correct |
9 ms |
472 KB |
Output is correct |
14 |
Correct |
9 ms |
596 KB |
Output is correct |
15 |
Correct |
9 ms |
584 KB |
Output is correct |
16 |
Correct |
8 ms |
540 KB |
Output is correct |
17 |
Correct |
7 ms |
472 KB |
Output is correct |
18 |
Correct |
6 ms |
468 KB |
Output is correct |
19 |
Correct |
7 ms |
616 KB |
Output is correct |
20 |
Correct |
17 ms |
536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2247 ms |
4400 KB |
Output is correct |
2 |
Correct |
2414 ms |
4136 KB |
Output is correct |
3 |
Correct |
2229 ms |
4292 KB |
Output is correct |
4 |
Correct |
2222 ms |
5144 KB |
Output is correct |
5 |
Correct |
2009 ms |
4204 KB |
Output is correct |
6 |
Execution timed out |
4081 ms |
13060 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4067 ms |
8292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
8 ms |
548 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
10 ms |
472 KB |
Output is correct |
5 |
Correct |
9 ms |
576 KB |
Output is correct |
6 |
Correct |
10 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
444 KB |
Output is correct |
8 |
Correct |
8 ms |
560 KB |
Output is correct |
9 |
Correct |
9 ms |
596 KB |
Output is correct |
10 |
Correct |
9 ms |
452 KB |
Output is correct |
11 |
Correct |
9 ms |
580 KB |
Output is correct |
12 |
Correct |
9 ms |
592 KB |
Output is correct |
13 |
Correct |
9 ms |
472 KB |
Output is correct |
14 |
Correct |
9 ms |
596 KB |
Output is correct |
15 |
Correct |
9 ms |
584 KB |
Output is correct |
16 |
Correct |
8 ms |
540 KB |
Output is correct |
17 |
Correct |
7 ms |
472 KB |
Output is correct |
18 |
Correct |
6 ms |
468 KB |
Output is correct |
19 |
Correct |
7 ms |
616 KB |
Output is correct |
20 |
Correct |
17 ms |
536 KB |
Output is correct |
21 |
Correct |
2247 ms |
4400 KB |
Output is correct |
22 |
Correct |
2414 ms |
4136 KB |
Output is correct |
23 |
Correct |
2229 ms |
4292 KB |
Output is correct |
24 |
Correct |
2222 ms |
5144 KB |
Output is correct |
25 |
Correct |
2009 ms |
4204 KB |
Output is correct |
26 |
Execution timed out |
4081 ms |
13060 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |