#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e6 + 5;
int n, m, a[N], b[N], pre[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
while (m--) {
int s, t;
ll cap;
cin >> s >> t >> cap;
bool ok = true;
for (int i = s; i <= t - 1; i++)
if (a[i] > cap) {
ok = false;
break;
}
if (!ok) {
cout << "-1\n";
continue;
}
ll cur = 0, ans = 0;
for (int i = s; i <= t - 1; i++) {
int j = t;
for (int k = i + 1; k <= t - 1; k++)
if (b[k] < b[i]) {
j = k;
break;
}
int r = min(j - 1, t - 1);
ll nd = (r >= i ? pre[r] - pre[i - 1] : 0);
ll k = min(cap, nd);
if (k > cur) {
ll c = k - cur;
ans += c * b[i];
cur += c;
}
cur -= a[i];
}
cout << ans << "\n";
}
}
| # | 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... |