Cod sursa(job #2055715)

Utilizator DavidLDavid Lauran DavidL Data 3 noiembrie 2017 17:18:08
Problema Problema rucsacului Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>
#define MAXN 5005
#define MAXG 10005
using namespace std;
ifstream fi("rucsac.in");
ofstream fo("rucsac.out");


int dp[MAXN][MAXG];
int n,g;
int main()
{
    fi>>n>>g;
    for (int i=1; i<=n; i++)
    {
        int gr,profit;
        fi>>gr>>profit;
        for (int j=0; j<=g; j++)
        {
            dp[i][j]=dp[i-1][j];
            if (j>=gr)
                dp[i][j]=max(dp[i][j],dp[i-1][j-gr]+profit);
        }
    }
    fo<<dp[n][g];
    return 0;
}