#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//#pragma GCC optmize("O3")
//#pragma GCC optmize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define int long long
const int nax=205;
vector<pair<int,int>> objects;
void add(int x , int i)
{
while(x)
{
int msb = 31 - __builtin_clz(x);
if(x == (1<<(msb + 1)) - 1)
{
for(int j = 0 ; j <= msb ; j++)
{
objects.pb({(1<<j) * i, (1<<j)});
}
break;
}
int cur=0;
for(int j = 0 ; j < msb ; j++)
{
cur+=(1<<j);
objects.pb({(1<<j) * i , (1<<j)});
}
x-=cur;
}
}
vector<int> dp;
const int INF = 1e18;
signed main(){
int m , L;
cin>>m>>L;
int ans = 0;
int MX=0;
int MN=0;
for(int i = -m ; i <= m ; i++)
{
int x;
cin>>x;
if(i>0) MX+=x*i;
else MN-=x*i;
if(i == 0){
ans=x;
}else add(x , i);
}
MX=max(abs(MN),abs(MX));
MX++;
dp.resize(2*MX);
for(int i = -MX + 1 ; i <= MX - 1; i++)
{
dp[i + MX] = -INF;
}
dp[MX] = ans;
sort(objects.begin(),objects.end());
for(auto c : objects){
if(c.fi > 0){
for(int j = MX - 1 ; j >= -MX + 1 ; j--)
{
if(j - c.fi >= -MX + 1)
dp[j + MX] = max(dp[j+MX] , dp[j - c.fi + MX] + c.se);
}
}else
{
for(int j = -MX + 1 ; j <= MX - 1 ; j++)
{
if(j - c.fi <= MX - 1)
{
dp[j+ MX] = max(dp[j+ MX] , dp[j - c.fi+ MX] + c.se);
}
}
}
}
if(L>=MX||L<=-MX) cout <<"impossible"<<endl;
else if(dp[L+MX]<=0) cout <<"impossible"<<endl;
else cout << dp[L+MX]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
1008 KB |
Output is correct |
6 |
Correct |
40 ms |
860 KB |
Output is correct |
7 |
Correct |
11 ms |
604 KB |
Output is correct |
8 |
Correct |
38 ms |
856 KB |
Output is correct |
9 |
Correct |
98 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
1008 KB |
Output is correct |
6 |
Correct |
40 ms |
860 KB |
Output is correct |
7 |
Correct |
11 ms |
604 KB |
Output is correct |
8 |
Correct |
38 ms |
856 KB |
Output is correct |
9 |
Correct |
98 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
41 ms |
856 KB |
Output is correct |
17 |
Correct |
39 ms |
860 KB |
Output is correct |
18 |
Correct |
11 ms |
604 KB |
Output is correct |
19 |
Correct |
37 ms |
860 KB |
Output is correct |
20 |
Correct |
105 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
819 ms |
4744 KB |
Output is correct |
24 |
Correct |
773 ms |
4572 KB |
Output is correct |
25 |
Correct |
126 ms |
2140 KB |
Output is correct |
26 |
Correct |
2012 ms |
8024 KB |
Output is correct |
27 |
Correct |
1976 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
1008 KB |
Output is correct |
6 |
Correct |
40 ms |
860 KB |
Output is correct |
7 |
Correct |
11 ms |
604 KB |
Output is correct |
8 |
Correct |
38 ms |
856 KB |
Output is correct |
9 |
Correct |
98 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
1008 KB |
Output is correct |
6 |
Correct |
40 ms |
860 KB |
Output is correct |
7 |
Correct |
11 ms |
604 KB |
Output is correct |
8 |
Correct |
38 ms |
856 KB |
Output is correct |
9 |
Correct |
98 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
41 ms |
856 KB |
Output is correct |
17 |
Correct |
39 ms |
860 KB |
Output is correct |
18 |
Correct |
11 ms |
604 KB |
Output is correct |
19 |
Correct |
37 ms |
860 KB |
Output is correct |
20 |
Correct |
105 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
819 ms |
4744 KB |
Output is correct |
24 |
Correct |
773 ms |
4572 KB |
Output is correct |
25 |
Correct |
126 ms |
2140 KB |
Output is correct |
26 |
Correct |
2012 ms |
8024 KB |
Output is correct |
27 |
Correct |
1976 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
40 ms |
1008 KB |
Output is correct |
6 |
Correct |
40 ms |
860 KB |
Output is correct |
7 |
Correct |
11 ms |
604 KB |
Output is correct |
8 |
Correct |
38 ms |
856 KB |
Output is correct |
9 |
Correct |
98 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
41 ms |
856 KB |
Output is correct |
17 |
Correct |
39 ms |
860 KB |
Output is correct |
18 |
Correct |
11 ms |
604 KB |
Output is correct |
19 |
Correct |
37 ms |
860 KB |
Output is correct |
20 |
Correct |
105 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
819 ms |
4744 KB |
Output is correct |
24 |
Correct |
773 ms |
4572 KB |
Output is correct |
25 |
Correct |
126 ms |
2140 KB |
Output is correct |
26 |
Correct |
2012 ms |
8024 KB |
Output is correct |
27 |
Correct |
1976 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Execution timed out |
5089 ms |
348 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |