Cod sursa(job #3184537)

Utilizator DARIUSQSDarius Nicoara DARIUSQS Data 16 decembrie 2023 10:41:54
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <string.h>
using namespace std;

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

int n, g, greutate, val;

const int WMAX = 1e4 + 1;
const int inf = -0x3f3f3f3f;

int dp[WMAX];

int main()
{
    in >> n >> g;
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    int max = 0;
    for (int i = 0; i < n; ++i)
    {
        in >> greutate >> val;
        for (int j = g; j >= 0; --j)
        {
            int prev = j - greutate;

            if (prev >= 0 && dp[prev] + val > dp[j])
                dp[j] = dp[prev] + val;

            if (dp[j] != inf && dp[j] > max)
                max = dp[j];
        }
    }
    fout << max << '\n';
    return 0;
}