计蒜客 蒜头君的购物袋2(DP)

蒜头君的购物袋2

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

#define maxn 105
#define maxv 1005
using namespace std;

int dp[maxn][maxv];
int weight[maxn];
int value[maxn];

int main()
{
    //freopen("data.in","r",stdin);
    ios::sync_with_stdio(false);

    memset(weight,0,sizeof(weight));
    memset(dp, 0, sizeof(dp));

    int w;
    int n;
    cin >> w >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> weight[i] >> value[i];
    }

    dp[0][0] = 0;
    for(int i = 1; i <= n; ++i) {
        for(int j = 0; j <= w; ++j) {
            dp[i][j] = dp[i-1][j];
            if(j >= weight[i-1]) {
                dp[i][j] = max(dp[i][j], dp[i-1][j - weight[i-1]] + value[i-1]);
            }
        }
    }

    cout << dp[n][w] << endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/ccshijtgc/article/details/80893785