做法:01背包

思路



  • 所以设,把a[i]看作价值,c[i]看作容量
    又因为c[i]有正有负,所以我们正负分开求
    求的是当容量为0时,价值最大。我们分别把对应的正负容量相加为0时价值相加求最大即可

代码

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=110;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);

int n,k,a[N],b[N],c[N],ans;
int dp1[10005],dp2[10005];

void solve(){
    cin>>n>>k;
    rep(i,1,n) cin>>a[i];
    rep(i,1,n) cin>>b[i],c[i]=a[i]-k*b[i];
    rep(i,1,10000) dp1[i]=dp2[i]=-INF;
    rep(i,1,n){
        if(c[i]>=0){
            per(j,10000,c[i]){
                dp1[j]=max(dp1[j],dp1[j-c[i]]+a[i]);
            }
        }
        else{
            per(j,10000,-c[i]){
                dp2[j]=max(dp2[j],dp2[j+c[i]]+a[i]);
            }
        }
    }
    rep(i,0,10000){
        ans=max(ans,dp1[i]+dp2[i]);
    }
    if(ans) cout<<ans<<"\n";
    else cout<<"-1\n";
}


int main(){
    ios::sync_with_stdio(0);cin.tie(0);
//    int t;cin>>t;while(t--)
    solve();
    return 0;
}