#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvl = vector<vll>;
using vb = vector<bool>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
using vi = vector<int>;
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define pb push_back
int main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);
ll n, l, z, q;
ll s1 = 0, s2=0, a1=0, a2 = 0;
cin >> n >> l;
vll ng(n),ps(n);
for(ll i = n; i > 0; --i){cin >> ng[i-1];a2+=ng[i-1];s2+=ng[i-1]*i;}
cin >> z;
for(ll i = 1; i <= n; ++i){cin >> ps[i-1];a1+=ps[i-1];s1+=ps[i-1]*i;}
if(l>s1 || -s2>l){cout << "impossible";return 0;}
deque<vll> kn(1, vll(s1+s2+1, -1e18));
kn[0][s2]=0;
queue<pair<ll,pll>> pq;
for(ll i = 0; i < n; ++i)pq.push({(i+1), {ps[i], 1}});
while(!pq.empty()){
auto [i, c] = pq.front();pq.pop();
//if(c.f<=0)continue;
//if(c.f%2==1){pq.push({i*2, {c.f/2, c.s*2}});c.f=1;}
//else {pq.push({i*2, {c.f/2-1, c.s*2}});c.f=2;}
kn.pb(vll(s1+s2+1));
for(ll j = 0; j <= s1+s2; ++j){
for(ll h = 0; h <= c.f && j+h*i<=s1+s2; ++h){
kn[1][j+i*h] = max(kn[1][j+i*h], kn[0][j]+h*c.s);
}
}
kn.pop_front();
}
for(ll i = 0; i < n; ++i)pq.push({(i+1), {ng[i], 1}});
while(!pq.empty()){
auto [i, c] = pq.front();pq.pop();
//if(c.f<=0)continue;
//if(c.f%2==1){pq.push({i*2, {c.f/2, c.s*2}});c.f=1;}
//else {pq.push({i*2, {c.f/2-1, c.s*2}});c.f=2;}
kn.pb(vll(s1+s2+1));
for(ll j = 0; j <= s1+s2; ++j){
for(ll h = 0; h <= c.f && j >= h*i; ++h){
kn[1][j-i*h] = max(kn[1][j-i*h], kn[0][j]+h*c.s);
}
}
kn.pop_front();
}
if(kn[0][l+s2]<=0)cout << "impossible";
else cout << kn[0][l+s2]+z;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |