Cod sursa(job #2491019)

Utilizator ViAlexVisan Alexandru ViAlex Data 11 noiembrie 2019 17:24:56
Problema Problema rucsacului Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include<bits/stdc++.h>
using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

int weight[5000],profit[5000];
int n,max_weight;


int dp[5001];


void read()
{
    in>>n>>max_weight;
    for(int i=0; i<n; i++)
    {
        in>>weight[i]>>profit[i];
    }
}

int knapsack()
{
    int sol=0;
    for( int i = 1; i <= n; i++)
        for(int j = max_weight-weight[i]; j >= 0; j--)
        {
            if(dp[j+weight[i]] < dp[j] + profit[i] )
            {
                dp[j+weight[i]] = dp[j] + profit[i];
                if( dp[j+weight[i]] > sol)
                    sol = dp[j+weight[i]];
            }
        }
    return sol;
}

int main()
{
    read();
    out<<knapsack();

}