#include <bits/stdc++.h>
using namespace std;
const int N = 505000 + 5;
long long m, l, pos[N], old[N], neg[N], ans = -1;
vector<int> negw, posw;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> m >> l;
negw = posw = vector(m + 1, 0);
for(int i = m; i >= 1; i--) cin >> negw[i];
for(int i = 0; i <= m; i++) cin >> posw[i];
if(abs(l) >= N) cout << "impossible";
else{
pos[0] = 1;
for(int x = 1; x <= m; x++){
if(posw[x]){
memset(old, 0, sizeof(old));
for(int md = 0; md < x; md++){
deque<int> dq;
for(int i = md; i < N; i += x){
old[i] = pos[i];
pos[i] = max(pos[i], dq.empty() ? 0ll : dq.front() + i / x);
if(i - x * posw[x] >= 0 && old[i - x * posw[x]] > 0 && dq.front() == old[i - x * posw[x]] - (i - x * posw[x]) / x){
dq.pop_front();
}
if(old[i] > 0){
while(!dq.empty() && dq.back() < old[i] - i / x) dq.pop_back();
dq.push_back(old[i] - i / x);
}
}
}
}
}
for(int i = 0; i < N; i++) pos[i] -= 1;
neg[0] = 1;
for(int x = 1; x <= m; x++){
if(negw[x]){
memset(old, 0, sizeof(old));
for(int md = 0; md < x; md++){
deque<int> dq;
for(int i = md; i < N; i += x){
old[i] = neg[i];
neg[i] = max(neg[i], dq.empty() ? 0ll : dq.front() + i / x);
if(i - x * negw[x] >= 0 && old[i - x * negw[x]] > 0 && dq.front() == old[i - x * negw[x]] - (i - x * negw[x]) / x){
dq.pop_front();
}
if(old[i] > 0){
while(!dq.empty() && dq.back() < old[i] - i / x) dq.pop_back();
dq.push_back(old[i] - i / x);
}
}
}
}
}
for(int i = 0; i < N; i++) neg[i] -= 1;
if(l < 0 && neg[-l] > 0) ans = neg[-l];
else if(l == 0) ans = 0;
else if(l > 0 && pos[l] > 0) ans = pos[l];
for(int i = 1; i < N; i++){
if(pos[i] > 0 && i - l >= 1 && i - l < N && neg[i - l] > 0){
ans = max(ans, pos[i] + neg[i - l]);
}
}
if(ans == -1) cout << "impossible";
else cout << ans + posw[0];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12116 KB |
Output is correct |
2 |
Correct |
14 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
12124 KB |
Output is correct |
4 |
Correct |
40 ms |
12068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
392 ms |
12052 KB |
Output is correct |
7 |
Correct |
276 ms |
12064 KB |
Output is correct |
8 |
Correct |
636 ms |
12048 KB |
Output is correct |
9 |
Correct |
514 ms |
12052 KB |
Output is correct |
10 |
Correct |
16 ms |
12116 KB |
Output is correct |
11 |
Correct |
28 ms |
12068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12116 KB |
Output is correct |
2 |
Correct |
14 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
12124 KB |
Output is correct |
4 |
Correct |
40 ms |
12068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
392 ms |
12052 KB |
Output is correct |
7 |
Correct |
276 ms |
12064 KB |
Output is correct |
8 |
Correct |
636 ms |
12048 KB |
Output is correct |
9 |
Correct |
514 ms |
12052 KB |
Output is correct |
10 |
Correct |
16 ms |
12116 KB |
Output is correct |
11 |
Correct |
28 ms |
12068 KB |
Output is correct |
12 |
Correct |
15 ms |
12116 KB |
Output is correct |
13 |
Correct |
15 ms |
12172 KB |
Output is correct |
14 |
Correct |
9 ms |
12128 KB |
Output is correct |
15 |
Correct |
32 ms |
12064 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
551 ms |
12096 KB |
Output is correct |
18 |
Correct |
231 ms |
12052 KB |
Output is correct |
19 |
Correct |
434 ms |
12048 KB |
Output is correct |
20 |
Correct |
514 ms |
12108 KB |
Output is correct |
21 |
Correct |
15 ms |
12116 KB |
Output is correct |
22 |
Correct |
29 ms |
12108 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
937 ms |
12048 KB |
Output is correct |
25 |
Correct |
431 ms |
12200 KB |
Output is correct |
26 |
Correct |
1351 ms |
12164 KB |
Output is correct |
27 |
Correct |
1291 ms |
12188 KB |
Output is correct |
28 |
Correct |
16 ms |
12116 KB |
Output is correct |
29 |
Correct |
27 ms |
12068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12064 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12064 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12064 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12116 KB |
Output is correct |
2 |
Correct |
14 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
12124 KB |
Output is correct |
4 |
Correct |
40 ms |
12068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
392 ms |
12052 KB |
Output is correct |
7 |
Correct |
276 ms |
12064 KB |
Output is correct |
8 |
Correct |
636 ms |
12048 KB |
Output is correct |
9 |
Correct |
514 ms |
12052 KB |
Output is correct |
10 |
Correct |
16 ms |
12116 KB |
Output is correct |
11 |
Correct |
28 ms |
12068 KB |
Output is correct |
12 |
Correct |
33 ms |
12064 KB |
Output is correct |
13 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12064 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12116 KB |
Output is correct |
2 |
Correct |
14 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
12124 KB |
Output is correct |
4 |
Correct |
40 ms |
12068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
392 ms |
12052 KB |
Output is correct |
7 |
Correct |
276 ms |
12064 KB |
Output is correct |
8 |
Correct |
636 ms |
12048 KB |
Output is correct |
9 |
Correct |
514 ms |
12052 KB |
Output is correct |
10 |
Correct |
16 ms |
12116 KB |
Output is correct |
11 |
Correct |
28 ms |
12068 KB |
Output is correct |
12 |
Correct |
15 ms |
12116 KB |
Output is correct |
13 |
Correct |
15 ms |
12172 KB |
Output is correct |
14 |
Correct |
9 ms |
12128 KB |
Output is correct |
15 |
Correct |
32 ms |
12064 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
551 ms |
12096 KB |
Output is correct |
18 |
Correct |
231 ms |
12052 KB |
Output is correct |
19 |
Correct |
434 ms |
12048 KB |
Output is correct |
20 |
Correct |
514 ms |
12108 KB |
Output is correct |
21 |
Correct |
15 ms |
12116 KB |
Output is correct |
22 |
Correct |
29 ms |
12108 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
937 ms |
12048 KB |
Output is correct |
25 |
Correct |
431 ms |
12200 KB |
Output is correct |
26 |
Correct |
1351 ms |
12164 KB |
Output is correct |
27 |
Correct |
1291 ms |
12188 KB |
Output is correct |
28 |
Correct |
16 ms |
12116 KB |
Output is correct |
29 |
Correct |
27 ms |
12068 KB |
Output is correct |
30 |
Correct |
33 ms |
12064 KB |
Output is correct |
31 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
12064 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12116 KB |
Output is correct |
2 |
Correct |
14 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
12124 KB |
Output is correct |
4 |
Correct |
40 ms |
12068 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
392 ms |
12052 KB |
Output is correct |
7 |
Correct |
276 ms |
12064 KB |
Output is correct |
8 |
Correct |
636 ms |
12048 KB |
Output is correct |
9 |
Correct |
514 ms |
12052 KB |
Output is correct |
10 |
Correct |
16 ms |
12116 KB |
Output is correct |
11 |
Correct |
28 ms |
12068 KB |
Output is correct |
12 |
Correct |
15 ms |
12116 KB |
Output is correct |
13 |
Correct |
15 ms |
12172 KB |
Output is correct |
14 |
Correct |
9 ms |
12128 KB |
Output is correct |
15 |
Correct |
32 ms |
12064 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
551 ms |
12096 KB |
Output is correct |
18 |
Correct |
231 ms |
12052 KB |
Output is correct |
19 |
Correct |
434 ms |
12048 KB |
Output is correct |
20 |
Correct |
514 ms |
12108 KB |
Output is correct |
21 |
Correct |
15 ms |
12116 KB |
Output is correct |
22 |
Correct |
29 ms |
12108 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
937 ms |
12048 KB |
Output is correct |
25 |
Correct |
431 ms |
12200 KB |
Output is correct |
26 |
Correct |
1351 ms |
12164 KB |
Output is correct |
27 |
Correct |
1291 ms |
12188 KB |
Output is correct |
28 |
Correct |
16 ms |
12116 KB |
Output is correct |
29 |
Correct |
27 ms |
12068 KB |
Output is correct |
30 |
Correct |
33 ms |
12064 KB |
Output is correct |
31 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |