Cod sursa(job #2576396)

Utilizator flee123Flee Bringa flee123 Data 6 martie 2020 19:05:13
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
#define gmaxxx 10002
#define nmax 5005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

int n, gmax, dp[3][gmaxxx], greutati[nmax], profit[nmax];

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