1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <bits/stdc++.h> using namespace std; template<typename T> inline bool ckmin(T &x,const T &y) { return (x > y) && (x = y,true);} template<typename T> inline bool ckmax(T &x,const T &y) { return (x < y) && (x = y,true);}
const int N = 3e2 + 5; typedef long long ll; int m,lim; ll L; ll au[N],ad[N],bu[N],bd[N]; int A[N * N * 2],B[N * N * 2]; inline void ins(int *A,int v,int w,ll num) { for(ll t = 1;t <= num;t <<= 1) { for(int i = lim;i >= t * v;i--) ckmax(A[i],A[i - t * v] + (int)t * w); num -= t; } if(num >= 1) for(int i = lim;i >= 1ll * num * v;i--) ckmax(A[i],A[i - num * v] + (int)num * w); } int main() { cin >> m >> L; for(int i = m;i >= 1;i--) cin >> ad[i]; ll num; cin >> num; for(int i = 1;i <= m;i++) cin >> au[i]; lim = m * m; for(int i = 1;i <= m;i++) num += ad[i],L += 1ll * i * ad[i]; if(L < 0) return puts("impossible"),0; for(int i = 1;i <= m;i++) { if(au[i] * i <= L) { num += (bu[i] = au[i]);L -= 1ll * i * bu[i];} else { num += (bu[i] = L / i);L -= 1ll * i * bu[i];break;} } for(int i = m;i >= 1;i--) { if(ad[i] * i <= L) { num -= (bd[i] = ad[i]);L -= 1ll * i * bd[i];} else { num -= (bd[i] = L / i);L -= 1ll * i * bd[i];break;} } memset(A,-0x3f,sizeof A);memset(B,-0x3f,sizeof B); A[0] = B[0] = 0; for(int i = 1;i <= m;i++) { if(au[i] - bu[i]) ins(A,i,1,au[i] - bu[i]); if(bu[i]) ins(B,i,-1,bu[i]); if(ad[i] - bd[i]) ins(A,i,-1,ad[i] - bd[i]); if(bd[i]) ins(B,i,1,bd[i]); } ll ans = -1; for(int j = 0;j + L <= lim;j++) if(B[j] > -1e9 && A[j + L] > -1e9) ans = max(ans,num + B[j] + A[j + L]); if(ans < 0) puts("impossible"); else cout << ans << endl; return 0; }
|