#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = 1e9;
void solve() {
int n, zero = 0;
ll l;
cin >> n >> l;
vector<ll> a(n), b(n);
for (ll &x: b) cin >> x;
cin >> zero;
for (ll &x: a) cin >> x;
reverse(b.begin(), b.end());
if (l < 0) {
swap(a, b);
l = -l;
}
if (*max_element(a.begin(), a.end()) <= 100) {
int N = 63751, ans = 0;
vector<int> dp_plus(N, -INF), dp_minus(N, -INF);
dp_plus[0] = dp_minus[0] = 0;
if (l > N) {
cout << "impossible";
return;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < a[i]; j++) {
for (int k = N - 1; k > i; k--) {
dp_plus[k] = max(dp_plus[k], dp_plus[k - i - 1] + 1);
}
}
for (int j = 0; j < b[i]; j++) {
for (int k = N - 1; k > i; k--) {
dp_minus[k] = max(dp_minus[k], dp_minus[k - i - 1] + 1);
}
}
// for (int j = 0; j < 100; j++) cout << dp_plus[j] << ' ';
// cout << '\n';
}
for (int i = l; i < N; i++) {
ans = max(ans, dp_plus[i] + dp_minus[i - l]);
}
if (ans == 0) cout << "impossible";
else cout << ans + zero;
}
}
int main() {
ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
solve();
// cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
97 ms |
828 KB |
Output is correct |
7 |
Correct |
41 ms |
820 KB |
Output is correct |
8 |
Correct |
101 ms |
820 KB |
Output is correct |
9 |
Correct |
179 ms |
820 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
97 ms |
828 KB |
Output is correct |
7 |
Correct |
41 ms |
820 KB |
Output is correct |
8 |
Correct |
101 ms |
820 KB |
Output is correct |
9 |
Correct |
179 ms |
820 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
103 ms |
724 KB |
Output is correct |
18 |
Correct |
41 ms |
828 KB |
Output is correct |
19 |
Correct |
88 ms |
832 KB |
Output is correct |
20 |
Correct |
170 ms |
832 KB |
Output is correct |
21 |
Correct |
5 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
832 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Incorrect |
353 ms |
820 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
832 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
832 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
832 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
97 ms |
828 KB |
Output is correct |
7 |
Correct |
41 ms |
820 KB |
Output is correct |
8 |
Correct |
101 ms |
820 KB |
Output is correct |
9 |
Correct |
179 ms |
820 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
832 KB |
Output is correct |
13 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
832 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
97 ms |
828 KB |
Output is correct |
7 |
Correct |
41 ms |
820 KB |
Output is correct |
8 |
Correct |
101 ms |
820 KB |
Output is correct |
9 |
Correct |
179 ms |
820 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
103 ms |
724 KB |
Output is correct |
18 |
Correct |
41 ms |
828 KB |
Output is correct |
19 |
Correct |
88 ms |
832 KB |
Output is correct |
20 |
Correct |
170 ms |
832 KB |
Output is correct |
21 |
Correct |
5 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
832 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Incorrect |
353 ms |
820 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
832 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
97 ms |
828 KB |
Output is correct |
7 |
Correct |
41 ms |
820 KB |
Output is correct |
8 |
Correct |
101 ms |
820 KB |
Output is correct |
9 |
Correct |
179 ms |
820 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
103 ms |
724 KB |
Output is correct |
18 |
Correct |
41 ms |
828 KB |
Output is correct |
19 |
Correct |
88 ms |
832 KB |
Output is correct |
20 |
Correct |
170 ms |
832 KB |
Output is correct |
21 |
Correct |
5 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
832 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Incorrect |
353 ms |
820 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |