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 <iostream>
#include <vector>
#include <deque>
using namespace std;
using ll = long long;
const int MAXN = 2e5 + 123;
ll eng[MAXN];
ll cost[MAXN];
ll get_ans(int l, int r, int mxeng) {
deque<pair<ll, ll>> dq;
ll eng_total = 0;
ll ans = 0;
for (int i = l; i < r; ++i) {
while (dq.size() && dq.back().first >= cost[i]) {
eng_total -= dq.back().second;
dq.pop_back();
}
if (eng_total < mxeng)
dq.push_back({cost[i], mxeng - eng_total});
eng_total = mxeng;
if (eng[i] > eng_total) {
return -1;
}
ll eng_need = eng[i];
while (eng_need > 0) {
if (eng_need >= dq.front().second) {
eng_need -= dq.front().second;
ans += dq.front().first * dq.front().second;
eng_total -= dq.front().second;
dq.pop_front();
} else {
dq.front().second -= eng_need;
ans += dq.front().first * eng_need;
eng_total -= eng_need;
eng_need = 0;
}
}
}
return ans;
}
signed main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> eng[i];
}
for (int i = 0; i < n; ++i) {
cin >> cost[i];
}
while (m--) {
int l, r, mxeng;
cin >> l >> r >> mxeng;
--l, --r;
cout << get_ans(l, r, mxeng) << '\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... |