CSES – Book Shop

背包問題

#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
const int maxn = 1005;
const int maxx = 1e5+5;
int n,x,dp[maxx],w[maxn],v[maxn];
//
signed main(){
    fastio;
    cin>>n>>x;
    for(int i=0;i<n;i++) cin>>w[i];
    for(int i=0;i<n;i++) cin>>v[i];
    for(int i=0;i<n;i++){
        for(int j=x;j>=0;j--){
            if(j-w[i] >= 0) dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
        }
    }
    cout<<*max_element(dp,dp+x+1)<<"\n";
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *