Cod sursa(job #1551501)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 15 decembrie 2015 22:42:20
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
#define nmax 10004

using namespace std;

std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");

int g[nmax], c[nmax], n;
int d[2][nmax], L0, L1;
int MID, DIM, G;

void Citire()
{
    int i;
    fin >> n >> G;
    for (i=1; i<=n; i++)
        fin >> g[i] >> c[i];
}

void Rezolva()
{
    int i, j, maxim;
    L0 = 0; L1 = 1; DIM = -1;
    /// initializari
    for (i=1; i<=n; i++)
       {
          d[L0][g[i]] = c[i];
          DIM = max(DIM, c[i]);
       }

    for (i=2; i<=n; i++)
    {
        MID = DIM;
        for (j=1; j<=DIM; j++)
           {
               d[L1][j] = max(d[L1][j], d[L0][j]);

               /// de la j ma duc la g + g[i] adaugand si c[i]
               d[L1][j + g[i]] = max(d[L1][j + g[i]], d[L0][j] + c[i]);
               MID = max(MID, d[L1][j + g[i]]);
           }
        DIM = MID;
        L0 = 1 - L0;
        L1 = 1 - L1;
    }
}

void Afisare()
{
    int i, j;
    int maxim;
    maxim = -100;
    for (j=0; j<=DIM; j++)
        maxim = max(maxim, d[L0][j]);
    fout << maxim << "\n";
}

int main ()
{
    Citire();
    Rezolva();
    Afisare();
    fin.close();
    fout.close();
    return 0;
}