Cod sursa(job #2672976)

Utilizator szaszdavidSzasz David szaszdavid Data 15 noiembrie 2020 16:21:45
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>

using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int nmax=5000;

int a[nmax+5][10006],n,g,profit=0;

int main()
{
    int i,x,y,j,l;
    fin>>n>>g;
    l=1;
    for(i=1;i<=n;i++,l=1-l)
    {
        fin>>y>>x;
        for(j=1;j<=g;j++)
        {
            if(j>=y) a[1-l][j]=max(a[l][j],a[l][j-y]+x);
            else a[1-l][j]=a[l][j];
            if(a[1-l][j]>=profit)
            {
               profit=a[1-l][j];
            }
        }
    }
    fout<<profit;
    return 0;
}