Cod sursa(job #1376017)

Utilizator radarobertRada Robert Gabriel radarobert Data 5 martie 2015 15:31:32
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>

using namespace std;

const int MAXG = 10002;
const int MAXN = 5003;

int smax[MAXG];
int w[MAXN], p[MAXN];

int main()
{
    ifstream in("rucsac.in");
    ofstream out("rucsac.out");

    int n, g;
    in >> n >> g;
    for (int i = 1; i <= n; i++)
        in >> w[i] >> p[i];

    for (int j = 1; j <= n; j++)
    {
        for (int i = g; i >= 0; i--)
        {
            if (w[j] + i <= g)
            {
                if (smax[w[j]+i] < smax[i] + p[j])
                    smax[w[j]+i] = smax[i] + p[j];
            }
        }
    }

    out << smax[g] << '\n';

    return 0;
}