#include <bits/stdc++.h>
using namespace std;
const int MAX_M = 50;
const int MAX_S = MAX_M * MAX_M;
const int MIN_S = -MAX_S;
const long long INF = 1e18;
unordered_map<int, long long> taken, untaken;
unordered_map<int, long long> dpCrt, dpPrv;
int main() {
int m;
long long l, u = 0, t = 0;
cin >> m >> l;
for ( int i = -m; i <= m; i++ ) {
cin >> untaken[i];
u += untaken[i];
}
if ( l < 0 ) {
for ( int i = 1; i <= m; i++ )
swap( untaken[i], untaken[-i] );
l = -l;
}
long long s = 0;
for ( int i = -m; i <= 0; i++ ) {
long long x = untaken[i];
s += i * x;
taken[i] += x;
untaken[i] -= x;
t += x;
u -= x;
}
for ( int i = 1; i <= m; i++ ) {
long long x = min( untaken[i], (l - s) / i );
s += i * x;
taken[i] += x;
untaken[i] -= x;
t += x;
u -= x;
}
if ( s + m < l ) {
for ( int i = -m; i < 0; i++ ) {
long long x = min( taken[i], (l - s) / (-i) );
s -= i * x;
taken[i] -= x;
untaken[i] += x;
t -= x;
u += x;
}
}
/*cout << s << "\n";
for ( int i = -m; i <= m; i++ )
cout << taken[i] << " " << untaken[i] << "\n";*/
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
dpCrt[0] = 0;
for ( int i = -m; i <= -1; i++ ) {
swap( dpCrt, dpPrv );
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
for ( int x = 0; x <= m && x <= untaken[i]; x++ ) {
for ( int s = MIN_S; s - i * x <= MAX_S; s++ )
dpCrt[s] = max( dpCrt[s], dpPrv[s - i * x] + x );
}
for ( int x = 0; x <= m && x <= taken[i]; x++ ) {
for ( int s = MIN_S - i * x; s <= MAX_S; s++ )
dpCrt[s] = max( dpCrt[s], dpPrv[s + i * x] - x );
}
/*for ( int s = MIN_S; s <= MAX_S; s++ )
cout << dpCrt[s] << " ";
cout << "\n";*/
}
for ( int i = 0; i <= m; i++ ) {
swap( dpCrt, dpPrv );
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
for ( int x = 0; x <= m && x <= untaken[i]; x++ ) {
for ( int s = MIN_S + i * x; s <= MAX_S; s++ )
dpCrt[s] = max( dpCrt[s], dpPrv[s - i * x] + x );
}
for ( int x = 0; x <= m && x <= taken[i]; x++ ) {
for ( int s = MIN_S; s + i * x <= MAX_S; s++ )
dpCrt[s] = max( dpCrt[s], dpPrv[s + i * x] - x );
}
/*for ( int s = MIN_S; s <= MAX_S; s++ )
cout << dpCrt[s] << " ";
cout << "\n";*/
}
if ( l - s > MAX_S || dpCrt[l - s] < 0 )
cout << "impossible\n";
else
cout << t + dpCrt[l - s] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
451 ms |
812 KB |
Output is correct |
6 |
Correct |
476 ms |
832 KB |
Output is correct |
7 |
Correct |
209 ms |
816 KB |
Output is correct |
8 |
Correct |
454 ms |
820 KB |
Output is correct |
9 |
Correct |
788 ms |
820 KB |
Output is correct |
10 |
Correct |
57 ms |
600 KB |
Output is correct |
11 |
Correct |
54 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
451 ms |
812 KB |
Output is correct |
6 |
Correct |
476 ms |
832 KB |
Output is correct |
7 |
Correct |
209 ms |
816 KB |
Output is correct |
8 |
Correct |
454 ms |
820 KB |
Output is correct |
9 |
Correct |
788 ms |
820 KB |
Output is correct |
10 |
Correct |
57 ms |
600 KB |
Output is correct |
11 |
Correct |
54 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
856 KB |
Output is correct |
13 |
Correct |
5 ms |
856 KB |
Output is correct |
14 |
Correct |
3 ms |
832 KB |
Output is correct |
15 |
Correct |
20 ms |
604 KB |
Output is correct |
16 |
Correct |
445 ms |
816 KB |
Output is correct |
17 |
Correct |
476 ms |
816 KB |
Output is correct |
18 |
Correct |
215 ms |
812 KB |
Output is correct |
19 |
Correct |
452 ms |
604 KB |
Output is correct |
20 |
Correct |
787 ms |
812 KB |
Output is correct |
21 |
Correct |
57 ms |
604 KB |
Output is correct |
22 |
Correct |
54 ms |
604 KB |
Output is correct |
23 |
Correct |
1330 ms |
840 KB |
Output is correct |
24 |
Correct |
1281 ms |
820 KB |
Output is correct |
25 |
Correct |
510 ms |
828 KB |
Output is correct |
26 |
Correct |
2001 ms |
1112 KB |
Output is correct |
27 |
Correct |
2008 ms |
820 KB |
Output is correct |
28 |
Incorrect |
100 ms |
600 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
600 KB |
Output is correct |
2 |
Correct |
126 ms |
808 KB |
Output is correct |
3 |
Correct |
56 ms |
820 KB |
Output is correct |
4 |
Correct |
147 ms |
804 KB |
Output is correct |
5 |
Correct |
175 ms |
604 KB |
Output is correct |
6 |
Correct |
122 ms |
812 KB |
Output is correct |
7 |
Correct |
47 ms |
808 KB |
Output is correct |
8 |
Correct |
39 ms |
604 KB |
Output is correct |
9 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
600 KB |
Output is correct |
2 |
Correct |
126 ms |
808 KB |
Output is correct |
3 |
Correct |
56 ms |
820 KB |
Output is correct |
4 |
Correct |
147 ms |
804 KB |
Output is correct |
5 |
Correct |
175 ms |
604 KB |
Output is correct |
6 |
Correct |
122 ms |
812 KB |
Output is correct |
7 |
Correct |
47 ms |
808 KB |
Output is correct |
8 |
Correct |
39 ms |
604 KB |
Output is correct |
9 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
600 KB |
Output is correct |
2 |
Correct |
126 ms |
808 KB |
Output is correct |
3 |
Correct |
56 ms |
820 KB |
Output is correct |
4 |
Correct |
147 ms |
804 KB |
Output is correct |
5 |
Correct |
175 ms |
604 KB |
Output is correct |
6 |
Correct |
122 ms |
812 KB |
Output is correct |
7 |
Correct |
47 ms |
808 KB |
Output is correct |
8 |
Correct |
39 ms |
604 KB |
Output is correct |
9 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
451 ms |
812 KB |
Output is correct |
6 |
Correct |
476 ms |
832 KB |
Output is correct |
7 |
Correct |
209 ms |
816 KB |
Output is correct |
8 |
Correct |
454 ms |
820 KB |
Output is correct |
9 |
Correct |
788 ms |
820 KB |
Output is correct |
10 |
Correct |
57 ms |
600 KB |
Output is correct |
11 |
Correct |
54 ms |
604 KB |
Output is correct |
12 |
Correct |
20 ms |
600 KB |
Output is correct |
13 |
Correct |
126 ms |
808 KB |
Output is correct |
14 |
Correct |
56 ms |
820 KB |
Output is correct |
15 |
Correct |
147 ms |
804 KB |
Output is correct |
16 |
Correct |
175 ms |
604 KB |
Output is correct |
17 |
Correct |
122 ms |
812 KB |
Output is correct |
18 |
Correct |
47 ms |
808 KB |
Output is correct |
19 |
Correct |
39 ms |
604 KB |
Output is correct |
20 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
600 KB |
Output is correct |
2 |
Correct |
126 ms |
808 KB |
Output is correct |
3 |
Correct |
56 ms |
820 KB |
Output is correct |
4 |
Correct |
147 ms |
804 KB |
Output is correct |
5 |
Correct |
175 ms |
604 KB |
Output is correct |
6 |
Correct |
122 ms |
812 KB |
Output is correct |
7 |
Correct |
47 ms |
808 KB |
Output is correct |
8 |
Correct |
39 ms |
604 KB |
Output is correct |
9 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
451 ms |
812 KB |
Output is correct |
6 |
Correct |
476 ms |
832 KB |
Output is correct |
7 |
Correct |
209 ms |
816 KB |
Output is correct |
8 |
Correct |
454 ms |
820 KB |
Output is correct |
9 |
Correct |
788 ms |
820 KB |
Output is correct |
10 |
Correct |
57 ms |
600 KB |
Output is correct |
11 |
Correct |
54 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
856 KB |
Output is correct |
13 |
Correct |
5 ms |
856 KB |
Output is correct |
14 |
Correct |
3 ms |
832 KB |
Output is correct |
15 |
Correct |
20 ms |
604 KB |
Output is correct |
16 |
Correct |
445 ms |
816 KB |
Output is correct |
17 |
Correct |
476 ms |
816 KB |
Output is correct |
18 |
Correct |
215 ms |
812 KB |
Output is correct |
19 |
Correct |
452 ms |
604 KB |
Output is correct |
20 |
Correct |
787 ms |
812 KB |
Output is correct |
21 |
Correct |
57 ms |
604 KB |
Output is correct |
22 |
Correct |
54 ms |
604 KB |
Output is correct |
23 |
Correct |
1330 ms |
840 KB |
Output is correct |
24 |
Correct |
1281 ms |
820 KB |
Output is correct |
25 |
Correct |
510 ms |
828 KB |
Output is correct |
26 |
Correct |
2001 ms |
1112 KB |
Output is correct |
27 |
Correct |
2008 ms |
820 KB |
Output is correct |
28 |
Incorrect |
100 ms |
600 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
600 KB |
Output is correct |
2 |
Correct |
126 ms |
808 KB |
Output is correct |
3 |
Correct |
56 ms |
820 KB |
Output is correct |
4 |
Correct |
147 ms |
804 KB |
Output is correct |
5 |
Correct |
175 ms |
604 KB |
Output is correct |
6 |
Correct |
122 ms |
812 KB |
Output is correct |
7 |
Correct |
47 ms |
808 KB |
Output is correct |
8 |
Correct |
39 ms |
604 KB |
Output is correct |
9 |
Incorrect |
75 ms |
600 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
19 ms |
604 KB |
Output is correct |
5 |
Correct |
451 ms |
812 KB |
Output is correct |
6 |
Correct |
476 ms |
832 KB |
Output is correct |
7 |
Correct |
209 ms |
816 KB |
Output is correct |
8 |
Correct |
454 ms |
820 KB |
Output is correct |
9 |
Correct |
788 ms |
820 KB |
Output is correct |
10 |
Correct |
57 ms |
600 KB |
Output is correct |
11 |
Correct |
54 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
856 KB |
Output is correct |
13 |
Correct |
5 ms |
856 KB |
Output is correct |
14 |
Correct |
3 ms |
832 KB |
Output is correct |
15 |
Correct |
20 ms |
604 KB |
Output is correct |
16 |
Correct |
445 ms |
816 KB |
Output is correct |
17 |
Correct |
476 ms |
816 KB |
Output is correct |
18 |
Correct |
215 ms |
812 KB |
Output is correct |
19 |
Correct |
452 ms |
604 KB |
Output is correct |
20 |
Correct |
787 ms |
812 KB |
Output is correct |
21 |
Correct |
57 ms |
604 KB |
Output is correct |
22 |
Correct |
54 ms |
604 KB |
Output is correct |
23 |
Correct |
1330 ms |
840 KB |
Output is correct |
24 |
Correct |
1281 ms |
820 KB |
Output is correct |
25 |
Correct |
510 ms |
828 KB |
Output is correct |
26 |
Correct |
2001 ms |
1112 KB |
Output is correct |
27 |
Correct |
2008 ms |
820 KB |
Output is correct |
28 |
Incorrect |
100 ms |
600 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |