[DP] [01背包] [洛谷] P1060 开心的金明

[DP] [01背包] [洛谷] P1060 开心的金明_第1张图片

基础01背包练习

value of j[i] equal to vj[i] * wj[i]

 

#pragma GCC optimize(2)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

int dp[MAXN] = {0}, volume[MAXN] = {0}, value[MAXN] = {0};

int main()
{
    ios::sync_with_stdio(false);

    cin.tie(0);     cout.tie(0);

    int len, sumvolume;
    
    cin>>sumvolume>>len;
    
    for(int i = 1; i <= len; i++)
    {
        int tmp;
        cin>>volume[i]>>tmp;
        value[i] = volume[i] * tmp;
    }
    
    for(int i = 1; i <= len; i++)
    {
        for(int j = sumvolume; j >= volume[i]; j--)
        {
            dp[j] = max(dp[j], dp[j - volume[i]] + value[i] );
        }
    }
        
    cout<

 

你可能感兴趣的:([Dynamic)