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>
#define all(i) (i).begin(), (i).end()
using namespace std;
#define debug(args...) cout << '[' << #args << "] is [", DEBUG(false, args), cout << ']' << endl
template<typename T>
void DEBUG(bool _sp, T i) {
    if (_sp)
        cout << ' ';
    cout << i;
}
template<typename T, typename ...U>
void DEBUG(bool _sp, T i, U ...j) {
    if (_sp)
        cout << ' ';
    cout << i;
    DEBUG(true, j...);
}
template<typename T, typename U>
ostream& operator << (ostream &i, pair<T, U> j) {
    i << '(' << j.first << ", " << j.second << ')';
}
template<typename T>
ostream& operator << (ostream &i, vector<T> j) {
    i << '{' << j.size() << " :";
    for (T _i : j)
        i << ' ' << _i;
    i << '}';
}
typedef long long ll;
typedef pair<int, int> pi;
typedef double dd;
const int inf = 0x3f3f3f3f, lg = 20;
const ll mod = 1e9 + 7, INF = 0x3f3f3f3f3f3f3f3f;
signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, q;
    cin >> n >> q;
    int a[n], b[n];
    for (int &i : a)
        cin >> i;
    for (int &i : b)
        cin >> i;
    ll pre[n + 1];
    pre[0] = 0;
    for (int i = 0; i < n; ++i)
        pre[i + 1] = pre[i] + a[i];
    if (n <= 3000 && q <= 3000) {
        while (q--) {
            int s, t, u;
            cin >> s >> t >> u;
            --s, --t;
            ll res = 0;
            deque<pi> q;
            for (int i = s; i < t; ++i) {
                if (a[i] > u) {
                    res = -1;
                    break;
                }
                while (!q.empty() && q.back().first >= b[i])
                    q.pop_back();
                q.emplace_back(b[i], i);
                ll tmp = 0;
                while (tmp < a[i]) {
                    while (pre[i] + tmp - pre[q.front().second] >= u)
                        q.pop_front();
                    ll x = min(a[i] - tmp, u - (pre[i] + tmp - pre[q.front().second]));
                    res += x * q.front().first;
                    tmp += x;
                }
            }
            cout << res << '\n';
        }
    }
}
Compilation message (stderr)
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)':
Main.cpp:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
   21 | }
      | ^
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<_Tp>)':
Main.cpp:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
   28 | }
      | ^| # | 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... |