#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;
vector<int> prv(n, -1), nxt(n, n);
{
vector<int> st;
for (int i = 0; i < n; i++) {
while (sz(st) && b[st.back()] >= b[i]) st.pop_back();
if (sz(st)) prv[i] = st.back();
st.push_back(i);
}
}
{
vector<int> st;
for (int i = n-1; i >= 0; i--) {
while (sz(st) && b[st.back()] > b[i]) st.pop_back();
if (sz(st)) nxt[i] = st.back();
st.push_back(i);
}
}
auto sum = [&](int l, int r) {
assert(l <= r);
ll ans = 0;
for (int i = l; i <= r; i++) ans += a[i];
return ans;
};
while (m--) {
int l, r, cap; cin >> l >> r >> cap, --l, --r;
bool bad = 0;
for (int i = l; i < r; i++) {
if (a[i] > cap) {
bad = 1;
}
}
if (bad) {
cout << -1 << '\n';
continue;
}
ll ans = 0;
for (int i = l; i < r; i++) {
ll lose = 0;
if (prv[i] >= l)
lose = max(0LL, cap - sum(prv[i], i-1));
ll cur = min((long long) cap, sum(i, min(r-1, nxt[i]-1))) - lose;
cur = max(cur, 0LL);
ans += cur * b[i];
}
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 |
1 |
Correct |
35 ms |
496 KB |
Output is correct |
2 |
Correct |
1367 ms |
488 KB |
Output is correct |
3 |
Correct |
12 ms |
516 KB |
Output is correct |
4 |
Correct |
36 ms |
456 KB |
Output is correct |
5 |
Correct |
1326 ms |
500 KB |
Output is correct |
6 |
Correct |
11 ms |
468 KB |
Output is correct |
7 |
Correct |
39 ms |
488 KB |
Output is correct |
8 |
Correct |
1371 ms |
520 KB |
Output is correct |
9 |
Correct |
11 ms |
432 KB |
Output is correct |
10 |
Correct |
34 ms |
476 KB |
Output is correct |
11 |
Correct |
1282 ms |
528 KB |
Output is correct |
12 |
Correct |
11 ms |
468 KB |
Output is correct |
13 |
Correct |
43 ms |
508 KB |
Output is correct |
14 |
Correct |
57 ms |
516 KB |
Output is correct |
15 |
Correct |
127 ms |
488 KB |
Output is correct |
16 |
Correct |
1346 ms |
532 KB |
Output is correct |
17 |
Correct |
24 ms |
468 KB |
Output is correct |
18 |
Correct |
721 ms |
516 KB |
Output is correct |
19 |
Correct |
8 ms |
468 KB |
Output is correct |
20 |
Execution timed out |
4061 ms |
504 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4050 ms |
2136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
5428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
496 KB |
Output is correct |
2 |
Correct |
1367 ms |
488 KB |
Output is correct |
3 |
Correct |
12 ms |
516 KB |
Output is correct |
4 |
Correct |
36 ms |
456 KB |
Output is correct |
5 |
Correct |
1326 ms |
500 KB |
Output is correct |
6 |
Correct |
11 ms |
468 KB |
Output is correct |
7 |
Correct |
39 ms |
488 KB |
Output is correct |
8 |
Correct |
1371 ms |
520 KB |
Output is correct |
9 |
Correct |
11 ms |
432 KB |
Output is correct |
10 |
Correct |
34 ms |
476 KB |
Output is correct |
11 |
Correct |
1282 ms |
528 KB |
Output is correct |
12 |
Correct |
11 ms |
468 KB |
Output is correct |
13 |
Correct |
43 ms |
508 KB |
Output is correct |
14 |
Correct |
57 ms |
516 KB |
Output is correct |
15 |
Correct |
127 ms |
488 KB |
Output is correct |
16 |
Correct |
1346 ms |
532 KB |
Output is correct |
17 |
Correct |
24 ms |
468 KB |
Output is correct |
18 |
Correct |
721 ms |
516 KB |
Output is correct |
19 |
Correct |
8 ms |
468 KB |
Output is correct |
20 |
Execution timed out |
4061 ms |
504 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |