Cod sursa(job #2486486)

Utilizator C3YOTodoran Diana Andreea C3YO Data 2 noiembrie 2019 22:30:33
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");
int n, gr[5010], co[5010], gmax;
int x[10010];

int main()
{
    int i, j=0;
    fin>>n>>gmax;
    for(i=1; i<=n; i++)
        fin>>gr[i]>>co[i];

    for(i=1; i<=gmax; i++)
        if(i-gr[1]>=0)
            x[i]=co[1];
   ///cout<<x[gmax]<<" ";
    for(j=2; j<=n; j++)
    {
       for(i=gmax; i>=1; i--)
        {
            if(i-gr[j]>=0)
            {
                if(x[i-gr[j]]+co[j]>x[i])
                    x[i]=x[i-gr[j]]+co[j];
            }
        }
    }

    fout<<x[gmax];

    fin.close();
    fout.close();
    return 0;
}