#include<bits/stdc++.h>
using namespace std;
long long int m,dp[10800005];
long long int L;
int num[200005];
int val[200005];
int num2[500005];
int val2[500005];
int all = 0;
int main()
{
cin>>m>>L;
for(int i=1;i<=m*2+1;i++)
{
cin>>num[i];
val[i] = i-m-1;
}
for(int i=0;i<=520000*2;i++)dp[i] = -1;
dp[520000] = 0; //遍移量為520000
int last = 0;
for (int i=m+1;i<=m*2+1;i++)
{
for(int j=1;j<=num[i];j*=2)
{
if(num[i] >= j)
{
num[i] -= j;
all++;
num2[all] = j;
val2[all] = j*val[i];
}
}
if(num[i] > 0)
{
all++;
num2[all] = num[i];
val2[all] = num[i] * val[i];
}
for(int j=last+1;j<=all;j++)
{
for(int k=520000*2;k>=1;k--)
{
if(k >= val2[j])
{
if(dp[k-val2[j]] == -1)continue;
if(dp[k] == -1)
{
dp[k] = dp[k-val2[j]] + num2[j];
}
else dp[k] = max(dp[k-val2[j]] + num2[j],dp[k]);
}
}
}
last = all;
}
for (int i=1;i<=m;i++)
{
for(int j=1;j<=num[i];j*=2)
{
if(num[i] >= j)
{
num[i] -= j;
all++;
num2[all] = j;
val2[all] = j*val[i];
}
}
if(num[i] > 0)
{
all++;
num2[all] = num[i];
val2[all] = num[i] * val[i];
}
for(int j=last+1;j<=all;j++)
{
for(int k=1;k<=520000*2;k++)
{
if(k -val2[j] <= 520000*2)
{
if(dp[k-val2[j]] == -1)continue;
if(dp[k] == -1)
{
dp[k] = dp[k-val2[j]] + num2[j];
}
else dp[k] = max(dp[k-val2[j]] +num2[j],dp[k]);
}
}
}
last = all;
}
// for(int i=1;i<=all;i++)
// {
// cout<<num2[i]<<" "<<val2[i]<<endl;
// }
if(L >= 520000 || L <= 0-520000)
{
cout<<"impossible"<<endl;
return 0;
}
else
{
if(dp[L+520000] >= 0)cout<<dp[L+520000]<<endl;
else cout<<"impossible"<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
24 ms |
8608 KB |
Output is correct |
5 |
Correct |
342 ms |
8504 KB |
Output is correct |
6 |
Correct |
370 ms |
8540 KB |
Output is correct |
7 |
Correct |
148 ms |
8592 KB |
Output is correct |
8 |
Correct |
368 ms |
8592 KB |
Output is correct |
9 |
Correct |
434 ms |
8536 KB |
Output is correct |
10 |
Correct |
15 ms |
8536 KB |
Output is correct |
11 |
Correct |
15 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
24 ms |
8608 KB |
Output is correct |
5 |
Correct |
342 ms |
8504 KB |
Output is correct |
6 |
Correct |
370 ms |
8540 KB |
Output is correct |
7 |
Correct |
148 ms |
8592 KB |
Output is correct |
8 |
Correct |
368 ms |
8592 KB |
Output is correct |
9 |
Correct |
434 ms |
8536 KB |
Output is correct |
10 |
Correct |
15 ms |
8536 KB |
Output is correct |
11 |
Correct |
15 ms |
8536 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
11 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
25 ms |
8540 KB |
Output is correct |
16 |
Correct |
353 ms |
8592 KB |
Output is correct |
17 |
Correct |
351 ms |
8536 KB |
Output is correct |
18 |
Correct |
144 ms |
8536 KB |
Output is correct |
19 |
Correct |
346 ms |
8604 KB |
Output is correct |
20 |
Correct |
438 ms |
8536 KB |
Output is correct |
21 |
Correct |
16 ms |
8540 KB |
Output is correct |
22 |
Correct |
15 ms |
8540 KB |
Output is correct |
23 |
Correct |
885 ms |
8784 KB |
Output is correct |
24 |
Correct |
896 ms |
8612 KB |
Output is correct |
25 |
Correct |
322 ms |
8536 KB |
Output is correct |
26 |
Correct |
1133 ms |
8600 KB |
Output is correct |
27 |
Correct |
1149 ms |
8784 KB |
Output is correct |
28 |
Correct |
33 ms |
8536 KB |
Output is correct |
29 |
Correct |
22 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
24 ms |
8608 KB |
Output is correct |
5 |
Correct |
342 ms |
8504 KB |
Output is correct |
6 |
Correct |
370 ms |
8540 KB |
Output is correct |
7 |
Correct |
148 ms |
8592 KB |
Output is correct |
8 |
Correct |
368 ms |
8592 KB |
Output is correct |
9 |
Correct |
434 ms |
8536 KB |
Output is correct |
10 |
Correct |
15 ms |
8536 KB |
Output is correct |
11 |
Correct |
15 ms |
8536 KB |
Output is correct |
12 |
Correct |
25 ms |
8540 KB |
Output is correct |
13 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
24 ms |
8608 KB |
Output is correct |
5 |
Correct |
342 ms |
8504 KB |
Output is correct |
6 |
Correct |
370 ms |
8540 KB |
Output is correct |
7 |
Correct |
148 ms |
8592 KB |
Output is correct |
8 |
Correct |
368 ms |
8592 KB |
Output is correct |
9 |
Correct |
434 ms |
8536 KB |
Output is correct |
10 |
Correct |
15 ms |
8536 KB |
Output is correct |
11 |
Correct |
15 ms |
8536 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
11 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
25 ms |
8540 KB |
Output is correct |
16 |
Correct |
353 ms |
8592 KB |
Output is correct |
17 |
Correct |
351 ms |
8536 KB |
Output is correct |
18 |
Correct |
144 ms |
8536 KB |
Output is correct |
19 |
Correct |
346 ms |
8604 KB |
Output is correct |
20 |
Correct |
438 ms |
8536 KB |
Output is correct |
21 |
Correct |
16 ms |
8540 KB |
Output is correct |
22 |
Correct |
15 ms |
8540 KB |
Output is correct |
23 |
Correct |
885 ms |
8784 KB |
Output is correct |
24 |
Correct |
896 ms |
8612 KB |
Output is correct |
25 |
Correct |
322 ms |
8536 KB |
Output is correct |
26 |
Correct |
1133 ms |
8600 KB |
Output is correct |
27 |
Correct |
1149 ms |
8784 KB |
Output is correct |
28 |
Correct |
33 ms |
8536 KB |
Output is correct |
29 |
Correct |
22 ms |
8540 KB |
Output is correct |
30 |
Correct |
25 ms |
8540 KB |
Output is correct |
31 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8536 KB |
Output is correct |
2 |
Correct |
9 ms |
8536 KB |
Output is correct |
3 |
Correct |
6 ms |
8540 KB |
Output is correct |
4 |
Correct |
24 ms |
8608 KB |
Output is correct |
5 |
Correct |
342 ms |
8504 KB |
Output is correct |
6 |
Correct |
370 ms |
8540 KB |
Output is correct |
7 |
Correct |
148 ms |
8592 KB |
Output is correct |
8 |
Correct |
368 ms |
8592 KB |
Output is correct |
9 |
Correct |
434 ms |
8536 KB |
Output is correct |
10 |
Correct |
15 ms |
8536 KB |
Output is correct |
11 |
Correct |
15 ms |
8536 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
11 ms |
8540 KB |
Output is correct |
14 |
Correct |
8 ms |
8540 KB |
Output is correct |
15 |
Correct |
25 ms |
8540 KB |
Output is correct |
16 |
Correct |
353 ms |
8592 KB |
Output is correct |
17 |
Correct |
351 ms |
8536 KB |
Output is correct |
18 |
Correct |
144 ms |
8536 KB |
Output is correct |
19 |
Correct |
346 ms |
8604 KB |
Output is correct |
20 |
Correct |
438 ms |
8536 KB |
Output is correct |
21 |
Correct |
16 ms |
8540 KB |
Output is correct |
22 |
Correct |
15 ms |
8540 KB |
Output is correct |
23 |
Correct |
885 ms |
8784 KB |
Output is correct |
24 |
Correct |
896 ms |
8612 KB |
Output is correct |
25 |
Correct |
322 ms |
8536 KB |
Output is correct |
26 |
Correct |
1133 ms |
8600 KB |
Output is correct |
27 |
Correct |
1149 ms |
8784 KB |
Output is correct |
28 |
Correct |
33 ms |
8536 KB |
Output is correct |
29 |
Correct |
22 ms |
8540 KB |
Output is correct |
30 |
Correct |
25 ms |
8540 KB |
Output is correct |
31 |
Runtime error |
50 ms |
17232 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |