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<int> a(n); for (auto& x : a) cin >> x;
vector<int> b(n); for (auto& x : b) cin >> x;
while (m--) {
int l, r, cap; cin >> l >> r >> cap, --l, --r;
deque<pair<int, int>> st;
ll ans = 0;
for (int i = l; i < r; i++) {
int need = a[i], cost = b[i];
while (sz(st) && b[st.back().first] >= cost) st.pop_back();
st.emplace_back(i, cap);
while (sz(st) && need) {
int cur = min(st.front().second, need); need -= cur;
ans += (long long) b[st.front().first] * cur;
for (auto& [x, y] : st) y -= cur;
while (sz(st) && st.front().second == 0) 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... |