/*input
2 5
2 3 1 1 4
*/
#pragma GCC optimize ("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int M;
ll L;
cin >> M >> L;
ll A_[2 * M + 1];
ll* A = A_ + M;
for (int i = -M; i <= M; i++)
cin >> A[i];
ll pl = 0;
for (int v = M; v >= 1; v--)
{
while (A[v] > 0 && A[-v] > 0)
{
if (L > 0)
{
A[v]--;
L -= v;
pl++;
}
else
{
A[-v]--;
L += v;
pl++;
}
}
}
L = -L;
ll cnt = 0;
for (int l = -M; l <= M; l++)
{
L += l * A[l];
cnt += A[l];
}
map<ll, ll>D;
D[0] = 0;
ll del = 250;
for (int v = M; v >= 1; v--)
for (int l : {v, -v})
{
map<ll, ll>D_;
for (pair<ll, ll>v : D)
{
ll k = (L - v.first) / l;
k = max(k, 0ll);
k = min(k, A[l]);
ll lo = max(0ll, k - del);
ll hi = min(k + del, A[l]);
for (ll c = lo; c <= hi; c++)
{
ll v_ = v.first + l * c;
ll c_ = v.second + c;
if (D_.count(v_) == 0)
D_[v_] = c_;
else
D_[v_] = min(D_[v_], c_);
}
}
D = D_;
}
if (D.count(L))
cout << pl + cnt - D[L] << "\n";
else
cout << "impossible\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4182 ms |
4240 KB |
Output is correct |
6 |
Correct |
2402 ms |
3452 KB |
Output is correct |
7 |
Correct |
1866 ms |
2956 KB |
Output is correct |
8 |
Correct |
3342 ms |
4356 KB |
Output is correct |
9 |
Execution timed out |
5052 ms |
5772 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4182 ms |
4240 KB |
Output is correct |
6 |
Correct |
2402 ms |
3452 KB |
Output is correct |
7 |
Correct |
1866 ms |
2956 KB |
Output is correct |
8 |
Correct |
3342 ms |
4356 KB |
Output is correct |
9 |
Execution timed out |
5052 ms |
5772 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
4204 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
4204 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
4204 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4182 ms |
4240 KB |
Output is correct |
6 |
Correct |
2402 ms |
3452 KB |
Output is correct |
7 |
Correct |
1866 ms |
2956 KB |
Output is correct |
8 |
Correct |
3342 ms |
4356 KB |
Output is correct |
9 |
Execution timed out |
5052 ms |
5772 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
4204 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4182 ms |
4240 KB |
Output is correct |
6 |
Correct |
2402 ms |
3452 KB |
Output is correct |
7 |
Correct |
1866 ms |
2956 KB |
Output is correct |
8 |
Correct |
3342 ms |
4356 KB |
Output is correct |
9 |
Execution timed out |
5052 ms |
5772 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
4204 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4182 ms |
4240 KB |
Output is correct |
6 |
Correct |
2402 ms |
3452 KB |
Output is correct |
7 |
Correct |
1866 ms |
2956 KB |
Output is correct |
8 |
Correct |
3342 ms |
4356 KB |
Output is correct |
9 |
Execution timed out |
5052 ms |
5772 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |