#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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
444 KB |
Output is correct |
2 |
Correct |
1154 ms |
560 KB |
Output is correct |
3 |
Correct |
38 ms |
412 KB |
Output is correct |
4 |
Correct |
78 ms |
448 KB |
Output is correct |
5 |
Correct |
1152 ms |
556 KB |
Output is correct |
6 |
Correct |
41 ms |
444 KB |
Output is correct |
7 |
Correct |
92 ms |
416 KB |
Output is correct |
8 |
Correct |
986 ms |
580 KB |
Output is correct |
9 |
Correct |
38 ms |
340 KB |
Output is correct |
10 |
Correct |
72 ms |
432 KB |
Output is correct |
11 |
Correct |
513 ms |
428 KB |
Output is correct |
12 |
Correct |
37 ms |
456 KB |
Output is correct |
13 |
Correct |
61 ms |
440 KB |
Output is correct |
14 |
Correct |
38 ms |
432 KB |
Output is correct |
15 |
Correct |
41 ms |
432 KB |
Output is correct |
16 |
Correct |
1018 ms |
460 KB |
Output is correct |
17 |
Correct |
49 ms |
444 KB |
Output is correct |
18 |
Correct |
337 ms |
444 KB |
Output is correct |
19 |
Correct |
27 ms |
428 KB |
Output is correct |
20 |
Execution timed out |
4059 ms |
416 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4045 ms |
2176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4067 ms |
4568 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
444 KB |
Output is correct |
2 |
Correct |
1154 ms |
560 KB |
Output is correct |
3 |
Correct |
38 ms |
412 KB |
Output is correct |
4 |
Correct |
78 ms |
448 KB |
Output is correct |
5 |
Correct |
1152 ms |
556 KB |
Output is correct |
6 |
Correct |
41 ms |
444 KB |
Output is correct |
7 |
Correct |
92 ms |
416 KB |
Output is correct |
8 |
Correct |
986 ms |
580 KB |
Output is correct |
9 |
Correct |
38 ms |
340 KB |
Output is correct |
10 |
Correct |
72 ms |
432 KB |
Output is correct |
11 |
Correct |
513 ms |
428 KB |
Output is correct |
12 |
Correct |
37 ms |
456 KB |
Output is correct |
13 |
Correct |
61 ms |
440 KB |
Output is correct |
14 |
Correct |
38 ms |
432 KB |
Output is correct |
15 |
Correct |
41 ms |
432 KB |
Output is correct |
16 |
Correct |
1018 ms |
460 KB |
Output is correct |
17 |
Correct |
49 ms |
444 KB |
Output is correct |
18 |
Correct |
337 ms |
444 KB |
Output is correct |
19 |
Correct |
27 ms |
428 KB |
Output is correct |
20 |
Execution timed out |
4059 ms |
416 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |