Cod sursa(job #1766637)

Utilizator stefii_predaStefania Preda stefii_preda Data 28 septembrie 2016 11:04:49
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#define N 10005

using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");
int best[N];

int main()
{
    int n, G, i, j, g, p;
    in >> n >> G;
    int max1 = 0;
    for(i = 1; i <= n; i++)
    {
        in >> g >> p;
        for(j = max1; j >=0; j--)
        {
            if(best[j] != 0 && g + j <= G && best[g+j] < best[j] + p)
            {
                best[g+j] = best[j] + p;
                if(j + g > max1) max1 = j + g;
            }

        }
        if(best[g] < p)
                    best[g] = p;
            if(g > max1)
                    max1 = g;

    }
    int maxmax = 0;
    for(i = 1; i <= G; i++)
        if(best[i] > maxmax)
            maxmax = best[i];
    out << maxmax;

    return 0;
}