#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
const int mxN=303;
const int MOD=1e9+7;
ll M, L;
ll A[2*mxN];
vector <pll> minu, plu;
int dp[mxN*mxN];
int main()
{
gibon
cin >> M >> L;
for(int i=-M;i<=M;i++) cin >> A[i+mxN];
ll psum=0, msum=0;
for(int i=-M;i<0;i++) msum+=i*A[i+mxN];
for(int i=1;i<=M;i++) psum+=i*A[i+mxN];
if(L>psum || L<msum) {cout << "impossible"; return 0;}
if(L==msum+psum)
{
ll tmp=0;
for(int i=-M;i<=M;i++) tmp+=A[i];
cout << tmp; return 0;
}
if(L>msum+psum)
{
L*=-1;
for(int i=1;i<=M;i++) swap(A[i+mxN], A[-i+mxN]);
}
ll nval=0, ncnt=0;
for(int i=-M;i<0;i++) minu.emplace_back(i, A[i+mxN]);
for(ll i=-M;i<0;i++) nval+=A[i+mxN]*i, ncnt+=A[i+mxN];
for(int i=1;i<=M;i++)
{
if(nval>L)
{
plu.emplace_back(i, A[i+mxN]);
continue;
}
if(L-nval>i*A[i+mxN]) nval+=i*A[i+mxN], ncnt+=A[i+mxN], minu.emplace_back(i, A[i+mxN]);
else if((L-nval)%i==0)
{
ncnt+=(L-nval)/i;
cout << ncnt;
return 0;
}
else
{
minu.emplace_back(i, (L-nval)/i+1);
plu.emplace_back(i, A[i+mxN]-(L-nval)/i-1);
ncnt+=(L-nval)/i+1;
nval+=i*((L-nval)/i+1);
}
}
for(int i=L-nval;i<=M*M;i++) dp[i+mxN]=-MOD;
dp[mxN]=0;
for(pll ele : plu)
{
for(int i=M*M;i>=L-nval;i--)
{
for(int j=1;j<=min(ele.sec, (i-(L-nval))/ele.fir);j++) dp[i+mxN]=max(dp[i+mxN], dp[i+mxN-j*ele.fir]+j);
}
}
for(pll ele : minu)
{
if(ele.fir>0)
{
for(int i=M*M;i>=L-nval;i--)
{
for(int j=1;j<=min(ele.sec, (i-(L-nval))/ele.fir);j++) dp[i+mxN]=max(dp[i+mxN], dp[i+mxN-j*ele.fir]-j);
}
}
else
{
for(int i=L-nval;i<=M*M;i++)
{
for(int j=1;j<=min(ele.sec, (M*M-i)/(-ele.fir));j++) dp[i+mxN]=max(dp[i+mxN], dp[i+mxN-j*ele.fir]-j);
}
}
}
if(dp[L-nval+mxN]==-MOD) cout << "impossible";
else cout << ncnt+dp[L-nval+mxN]+A[mxN];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |