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;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e2+10, MOD = 998244353;
void solve() {
int n, m; cin >> n >> m;
vector<ll> a(n); for (auto& x : a) cin >> x;
vector<ll> b(n); for (auto& x : b) cin >> x;
while (m--) {
int l, r, cap; cin >> l >> r >> cap, --l, --r;
deque<pair<int, ll>> st;
ll ans = 0;
ll sub = 0;
for (int i = l; i < r; i++) {
ll need = a[i], cost = b[i];
while (sz(st) && b[st.back().first] >= cost) st.pop_back();
st.emplace_back(i, cap + sub);
while (sz(st) && need) {
ll cur = min(st.front().second - sub, need);
ans += b[st.front().first] * cur;
need -= cur, sub += cur;
while (sz(st) && st.front().second == sub) st.pop_front();
}
if (need) {
ans = -1;
break;
}
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# | 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... |