Cod sursa(job #760990)

Utilizator Theorytheo .c Theory Data 24 iunie 2012 02:51:43
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 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");

const int inf = 1<<30;
int Gmax , p[nmax], w[nmax], N;
bool pm[nmax * 2];
int sum[nmax * 2];

void solve()
{
    for(int i = 1; i <= Gmax ;i++)
        sum[i] = -inf;
    sum[0] = 0;
    int last = 0, nrmax = 0;
    for(int i = 1; i <= N ;i++)
    {

        for( int j = Gmax - w[i] ; j >=0 ;j-- )

       if(sum[j] != -inf)
            {

                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;
}