Cod sursa(job #760989)

Utilizator Theorytheo .c Theory Data 24 iunie 2012 02:44:58
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<fstream>
#define nmax 5007
#define max(a,b) (a>b ? a : b)
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)
            {
                nrmax= max(nrmax, j +w[i]);

                pm[j + w[i]] =true;

                sum[j + w[i]] = max(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;
}