Cod sursa(job #3254975)

Utilizator andre_bogdanBogdan Andrei andre_bogdan Data 9 noiembrie 2024 10:55:59
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define limN 5002
#define limG 10002

int W[limN], P[limN];
int dp[limN][limG];

int main()
{
    int n, gmax;
    fin >> n >> gmax;
    for(int i = 1; i <= n; i++)
        fin >> W[i] >> P[i];
    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j <= gmax; j++)
        {
            dp[i][j] = max(dp[i-1][j], dp[i-1][j]);
            if(W[i] <= j)
                dp[i][j] = max(dp[i][j], dp[i-1][j-W[i]]+P[i]);
        }
    }
    fout << dp[n][gmax];
    return 0;
}