#include <bits/stdc++.h>
using namespace std;
const int MAX_M = 100;
const int MAX_S = MAX_M * MAX_M;
const int MIN_S = -MAX_S;
const long long INF = 1e18;
int m;
unordered_map<int, long long> taken, untaken;
unordered_map<int, long long> dpCrt, dpPrv;
void add( int x, int i ) {
swap( dpCrt, dpPrv );
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
for ( int s = MIN_S; s - i * x <= MAX_S; s++ ) {
if ( MIN_S <= s - i * x && s - i * x <= MAX_S )
dpCrt[s] = max( dpCrt[s], dpPrv[s - i * x] + x );
}
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = (dpCrt[s] <= -INF + m ? -INF : dpCrt[s]);
}
void substract( int x, int i ) {
swap( dpCrt, dpPrv );
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
for ( int s = MIN_S; s <= MAX_S; s++ ) {
if ( MIN_S <= s + i * x && s + i * x <= MAX_S )
dpCrt[s] = max( dpCrt[s], dpPrv[s + i * x] - x );
}
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = (dpCrt[s] <= -INF + m ? -INF : dpCrt[s]);
}
void afis() {
for ( int s = MIN_S; s <= MAX_S; s++ )
cout << dpCrt[s] << " ";
cout << "\n";
}
int main() {
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;
}
}
for ( int s = MIN_S; s <= MAX_S; s++ )
dpCrt[s] = -INF;
dpCrt[0] = 0;
for ( int i = -m; i <= m; i++ ) {
int k;
k = min( (long long)m, untaken[i] );
for ( int b = 1; (1 << b) - 1 <= k; b++ )
add( (1 << (b - 1)), i );
add( k - (1 << ((int)log2( m ))), i );
k = min( (long long)m, untaken[i] );
for ( int b = 1; (1 << b) - 1 <= k; b++ )
substract( (1 << (b - 1)), i );
substract( k - (1 << ((int)log2( m ))), i );
}
if ( l - s > MAX_S || dpCrt[l - s] == -INF )
cout << "impossible\n";
else
cout << t + dpCrt[l - s] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2020 KB |
Output is correct |
2 |
Correct |
22 ms |
1884 KB |
Output is correct |
3 |
Correct |
12 ms |
1884 KB |
Output is correct |
4 |
Correct |
89 ms |
1884 KB |
Output is correct |
5 |
Correct |
563 ms |
2492 KB |
Output is correct |
6 |
Correct |
204 ms |
2488 KB |
Output is correct |
7 |
Correct |
371 ms |
2504 KB |
Output is correct |
8 |
Correct |
361 ms |
2500 KB |
Output is correct |
9 |
Correct |
576 ms |
2136 KB |
Output is correct |
10 |
Incorrect |
234 ms |
2488 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2020 KB |
Output is correct |
2 |
Correct |
22 ms |
1884 KB |
Output is correct |
3 |
Correct |
12 ms |
1884 KB |
Output is correct |
4 |
Correct |
89 ms |
1884 KB |
Output is correct |
5 |
Correct |
563 ms |
2492 KB |
Output is correct |
6 |
Correct |
204 ms |
2488 KB |
Output is correct |
7 |
Correct |
371 ms |
2504 KB |
Output is correct |
8 |
Correct |
361 ms |
2500 KB |
Output is correct |
9 |
Correct |
576 ms |
2136 KB |
Output is correct |
10 |
Incorrect |
234 ms |
2488 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
159 ms |
2020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
159 ms |
2020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
159 ms |
2020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2020 KB |
Output is correct |
2 |
Correct |
22 ms |
1884 KB |
Output is correct |
3 |
Correct |
12 ms |
1884 KB |
Output is correct |
4 |
Correct |
89 ms |
1884 KB |
Output is correct |
5 |
Correct |
563 ms |
2492 KB |
Output is correct |
6 |
Correct |
204 ms |
2488 KB |
Output is correct |
7 |
Correct |
371 ms |
2504 KB |
Output is correct |
8 |
Correct |
361 ms |
2500 KB |
Output is correct |
9 |
Correct |
576 ms |
2136 KB |
Output is correct |
10 |
Incorrect |
234 ms |
2488 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
159 ms |
2020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2020 KB |
Output is correct |
2 |
Correct |
22 ms |
1884 KB |
Output is correct |
3 |
Correct |
12 ms |
1884 KB |
Output is correct |
4 |
Correct |
89 ms |
1884 KB |
Output is correct |
5 |
Correct |
563 ms |
2492 KB |
Output is correct |
6 |
Correct |
204 ms |
2488 KB |
Output is correct |
7 |
Correct |
371 ms |
2504 KB |
Output is correct |
8 |
Correct |
361 ms |
2500 KB |
Output is correct |
9 |
Correct |
576 ms |
2136 KB |
Output is correct |
10 |
Incorrect |
234 ms |
2488 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
2004 KB |
Output is correct |
2 |
Incorrect |
159 ms |
2020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2020 KB |
Output is correct |
2 |
Correct |
22 ms |
1884 KB |
Output is correct |
3 |
Correct |
12 ms |
1884 KB |
Output is correct |
4 |
Correct |
89 ms |
1884 KB |
Output is correct |
5 |
Correct |
563 ms |
2492 KB |
Output is correct |
6 |
Correct |
204 ms |
2488 KB |
Output is correct |
7 |
Correct |
371 ms |
2504 KB |
Output is correct |
8 |
Correct |
361 ms |
2500 KB |
Output is correct |
9 |
Correct |
576 ms |
2136 KB |
Output is correct |
10 |
Incorrect |
234 ms |
2488 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |