洛谷——动态规划的背包问题P1060

2019-11-30

10:08:41

#include<bits/stdc++.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int w[30],v[30],f[50000];
int n,m;
int main(){
    cin>>m>>n;
    for(int i=1;i<=n;++i){
        cin>>v[i]>>w[i];
        w[i] *= v[i];
    }
    //01背包
    for(int i=1;i<=n;++i){
        for(int j=m;j>=v[i];j--){
            if(j>=v[i]){
                f[j] = max(f[j],f[j-v[i]]+w[i]);
            }
        }
    } 
    cout<<f[m]<<endl;
    system("pause");
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/JasonPeng1/p/11961475.html