Cod sursa(job #2295834)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 3 decembrie 2018 23:04:42
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>
#define NMAX 5001
#define GMAX 10001
using namespace std;

ifstream in("tren.in");
ofstream out("tren.out");

int n, gmax, c[NMAX], g[NMAX], cmax[GMAX], uz[GMAX][NMAX];

int main()
{
    int i, k, S;
    in >> n >> gmax;

    for(i = 1; i <= n; i++) in >> g[i] >> c[i];

    for(S = 1; S <= gmax; S++) cmax[S] = -1;
    for(S = 1; S <= gmax; S++)
        for(i = 1; i <= n; i++)
            if(g[i] <= S && cmax[S-g[i]] != -1 && !uz[S-g[i]][i])
                if(cmax[S] < c[i] + cmax[S-g[i]])
                {
                    cmax[S] = c[i] + cmax[S-g[i]];

                    for(k = 1; k <= n; k++) uz[S][k] = uz[S-g[i]][k];
                    uz[S][i] = 1;
                }

    if(cmax[gmax] == -1) out << "imposibil";
    else out << cmax[gmax];

	return 0;
}