#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M=310, inf=3e18;
int m, L, a[M*2];
void impossible(){
cout << "impossible\n";
exit(0);
}
pair<pair<int, int>, vector<int>> dp(int power2, pair<pair<int, int>, vector<int>> start, vector<pair<int, int>> v){
int tl=start.first.first, tr=start.first.second;
vector<int> f=start.second;
int szl=max(0ll, min(m*m, (tl-(-inf))/power2));
int szr=max(0ll, min(m*m, (inf-tr)/power2));
tl-=szl*power2, tr+=szr*power2;
reverse(f.begin(), f.end());
f.resize((int)f.size()+szl, -1);
reverse(f.begin(), f.end());
f.resize((int)f.size()+szr, -1);
for (auto &i:v){
int val=i.first, cnt=i.second;
if (val>0){
for (int j=(int)f.size()-1; j>=val; --j){
if (f[j-val]!=-1) f[j]=max(f[j], f[j-val]+cnt);
}
}else{
for (int j=0; j-val<(int)f.size(); ++j){
if (f[j-val]!=-1) f[j]=max(f[j], f[j-val]+cnt);
}
}
}
reverse(f.begin(), f.end());
while (f.size() && f.back()==-1){
tl+=power2;
f.pop_back();
}
reverse(f.begin(), f.end());
while (f.size() && f.back()==-1){
tr-=power2;
f.pop_back();
}
if (f.empty()) impossible();
return {{tl, tr}, f};
}
vector<pair<int, int>> v[60];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> m >> L;
for (int i=-m; i<=m; ++i) cin >> a[m+i];
for (int i=-m; i<=m; ++i) if (i){
int val=i, cnt=0;
while (val%2==0) val/=2, ++cnt;
for (int j=0; j<60; ++j) if (a[m+i]>=(1ll<<j)) v[j+cnt].emplace_back(val, 1ll<<j), a[m+i]-=1ll<<j;
for (int j=0; j<60; ++j) if (a[m+i]>>j&1) v[j+cnt].emplace_back(val, 1ll<<j);
}
pair<pair<int, int>, vector<int>> f={{0, 0}, {a[m]}};
for (int i=0; i<60; ++i){
f=dp(1ll<<i, f, v[i]);
if (abs(f.first.first-L)>>i&1){
f.first.first+=1ll<<i;
if (f.second.empty()) impossible();
f.second.erase(f.second.begin());
}
if (abs(f.first.second-L)>>i&1){
f.first.second-=1ll<<i;
if (f.second.empty()) impossible();
f.second.pop_back();
}
vector<int> tmp;
for (int j=0; j<(int)f.second.size(); j+=2) tmp.push_back(f.second[j]);
tmp.swap(f.second);
}
if (f.first.first>L || f.first.second<L) impossible();
cout << f.second[(L-f.first.first)>>60] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
648 KB |
Output is correct |
27 |
Correct |
5 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
648 KB |
Output is correct |
27 |
Correct |
5 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
5 ms |
604 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
5 ms |
616 KB |
Output is correct |
44 |
Correct |
6 ms |
604 KB |
Output is correct |
45 |
Correct |
5 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
604 KB |
Output is correct |
47 |
Correct |
4 ms |
700 KB |
Output is correct |
48 |
Correct |
4 ms |
864 KB |
Output is correct |
49 |
Correct |
35 ms |
996 KB |
Output is correct |
50 |
Correct |
17 ms |
876 KB |
Output is correct |
51 |
Correct |
41 ms |
976 KB |
Output is correct |
52 |
Correct |
41 ms |
1012 KB |
Output is correct |
53 |
Correct |
38 ms |
988 KB |
Output is correct |
54 |
Correct |
30 ms |
944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
648 KB |
Output is correct |
27 |
Correct |
5 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
5 ms |
604 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
5 ms |
616 KB |
Output is correct |
44 |
Correct |
6 ms |
604 KB |
Output is correct |
45 |
Correct |
5 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
604 KB |
Output is correct |
47 |
Correct |
4 ms |
700 KB |
Output is correct |
48 |
Correct |
4 ms |
864 KB |
Output is correct |
49 |
Correct |
35 ms |
996 KB |
Output is correct |
50 |
Correct |
17 ms |
876 KB |
Output is correct |
51 |
Correct |
41 ms |
976 KB |
Output is correct |
52 |
Correct |
41 ms |
1012 KB |
Output is correct |
53 |
Correct |
38 ms |
988 KB |
Output is correct |
54 |
Correct |
30 ms |
944 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
2 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
2 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
5 ms |
628 KB |
Output is correct |
68 |
Correct |
3 ms |
580 KB |
Output is correct |
69 |
Correct |
5 ms |
604 KB |
Output is correct |
70 |
Correct |
6 ms |
604 KB |
Output is correct |
71 |
Correct |
7 ms |
604 KB |
Output is correct |
72 |
Correct |
6 ms |
600 KB |
Output is correct |
73 |
Correct |
4 ms |
712 KB |
Output is correct |
74 |
Correct |
4 ms |
716 KB |
Output is correct |
75 |
Correct |
35 ms |
996 KB |
Output is correct |
76 |
Correct |
17 ms |
876 KB |
Output is correct |
77 |
Correct |
40 ms |
1092 KB |
Output is correct |
78 |
Correct |
47 ms |
1012 KB |
Output is correct |
79 |
Correct |
37 ms |
988 KB |
Output is correct |
80 |
Correct |
30 ms |
944 KB |
Output is correct |
81 |
Correct |
1111 ms |
4980 KB |
Output is correct |
82 |
Correct |
424 ms |
3664 KB |
Output is correct |
83 |
Correct |
1042 ms |
5040 KB |
Output is correct |
84 |
Correct |
1047 ms |
6936 KB |
Output is correct |
85 |
Correct |
37 ms |
2588 KB |
Output is correct |
86 |
Correct |
26 ms |
2588 KB |
Output is correct |
87 |
Correct |
1002 ms |
4488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |