Cod sursa(job #760988)

Utilizator Theorytheo .c Theory Data 24 iunie 2012 02:42:09
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include<fstream>
#define nmax 5007
using namespace std;

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

int Gmax , p[nmax], w[nmax], N;
bool pm[nmax * 2];
int sum[nmax * 2];

void solve()
{
    pm[0] = true;
    int last = 0, nrmax = 0;
    for(int i = 1; i <= N ;i++)
    {
        last = nrmax ;nrmax = 0;
        for( int j = last; j >=0 ;j-- )
        {
            if(j + w[i] <= Gmax && pm[j] == true)
            {
                if(j + w[i] >= nrmax)
                    nrmax = j + w[i];
                pm[j + w[i]] =true;
                if(sum[j + w[i]] < sum[j] + p[i])
                    sum[j + w[i] ] = sum[j] + p[i] ;
            }
        }
    }
}
void read()
{
    fin >> N >>Gmax;
    for(int i = 1; i <= N ;i++)
    {
        fin >> w[i] >>p[i];
    }
    solve();

}
int main()
{

    read();
    int p_max = 0;
    for(int i = 0 ;i <= Gmax ; i++)
        if(p_max<sum[i] )
            p_max = sum[i];
    fout <<p_max;

    fin.close();
    return 0;
}