//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1000005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
int dp[maxn];
int main() {
io
int m;
ll L;
cin >> m >> L;
vector<ll> a(2*m+1), ex(2*m+1), ex2(2*m+1);
ll mins = -maxn / 2, ans = 0;
ll sum = 0;
for (int i = 0;i < 2*m+1;i++) {
cin >> a[i];
if (i == m) ans += a[i], a[i] = 0;
sum += a[i] * (i - m);
}
if (sum < L) {
for (int i = 0;i < m;i++) swap(a[i], a[2*m - i]);
L = -L;
}
for (int i = 0;i < m;i++) {
L -= a[i] * (i - m);
ans += a[i];
ex2[2 * m - i] = min(a[i],(ll)(1LL<<60));
a[i] = 0;
}
for (int i = m+1;i < 2*m+1;i++) {
ll p = min(a[i], L / (i-m));
ans += p;
L -= p * (i-m);
a[i] -= p;
ex[i] = min(a[i], (ll)(1LL<<60));
ex[2 * m - i] = min(p,(ll)(1LL<<60));
}
for (int i = 0;i < maxn;i++) dp[i] = -inf;
dp[-mins] = 0;
for (int i = 0;i < 2*m+1;i++) {
ll cnt = 1;
vector<int> num;
while (ex[i] >= cnt) {
num.push_back(cnt);
ex[i] -= cnt;
cnt <<= 1;
}
if (ex[i] > 0) num.push_back(ex[i]);
for (int p:num) {
if (i - m > 0) {
for (int j = maxn - 1;j >= 0;j--) {
int val = j + p * (i-m);
if (val >= maxn) continue;
int del = i < m ? -p : p;
dp[val] = max(dp[val], dp[j] + del);
}
} else {
for (int j = 0;j < maxn;j++) {
int val = j + p * (i-m);
if (val < 0) continue;
int del = i < m ? -p : p;
dp[val] = max(dp[val], dp[j] + del);
}
}
}
}
ex = ex2;
for (int i = m+1;i < 2*m+1;i++) {
ll cnt = 1;
vector<int> num;
while (ex[i] >= cnt) {
num.push_back(cnt);
ex[i] -= cnt;
cnt <<= 1;
}
if (ex[i] > 0) num.push_back(ex[i]);
for (int p:num) {
for (int j = maxn - 1;j >= 0;j--) {
int val = j + p * (i-m);
if (val >= maxn) continue;
dp[val] = max(dp[val], dp[j] - p);
}
}
}
if (L - mins >= maxn || L - mins < 0 || dp[L - mins] < -maxn) cout << "impossible\n";
else cout << dp[L - mins]+ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4232 KB |
Output is correct |
3 |
Correct |
6 ms |
4180 KB |
Output is correct |
4 |
Correct |
28 ms |
4236 KB |
Output is correct |
5 |
Runtime error |
33 ms |
8332 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4232 KB |
Output is correct |
3 |
Correct |
6 ms |
4180 KB |
Output is correct |
4 |
Correct |
28 ms |
4236 KB |
Output is correct |
5 |
Runtime error |
33 ms |
8332 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4232 KB |
Output is correct |
2 |
Runtime error |
39 ms |
8320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4232 KB |
Output is correct |
2 |
Runtime error |
39 ms |
8320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4232 KB |
Output is correct |
2 |
Runtime error |
39 ms |
8320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4232 KB |
Output is correct |
3 |
Correct |
6 ms |
4180 KB |
Output is correct |
4 |
Correct |
28 ms |
4236 KB |
Output is correct |
5 |
Runtime error |
33 ms |
8332 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4232 KB |
Output is correct |
2 |
Runtime error |
39 ms |
8320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4232 KB |
Output is correct |
3 |
Correct |
6 ms |
4180 KB |
Output is correct |
4 |
Correct |
28 ms |
4236 KB |
Output is correct |
5 |
Runtime error |
33 ms |
8332 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4232 KB |
Output is correct |
2 |
Runtime error |
39 ms |
8320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4232 KB |
Output is correct |
3 |
Correct |
6 ms |
4180 KB |
Output is correct |
4 |
Correct |
28 ms |
4236 KB |
Output is correct |
5 |
Runtime error |
33 ms |
8332 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |