Cod sursa(job #988523)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 23 august 2013 04:21:17
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>

#define MAXN 5002
#define MAXG 10002

using namespace std;

struct Object
{
    int w = 0;
    int v = 0;
};

bool operator < (const Object lhs, const Object rhs)
{
    return lhs.w > rhs.w;
}

Object vObjs[MAXN];

int knapsack[MAXG];

int main()
{
    int n, g;
    fstream fin("rucsac.in", fstream::in);
    fstream fout("rucsac.out", fstream::out);
    
    fin >> n >> g;
    //cout << n << " " << g << endl;
    
    for (int i=0; i<n; ++i)
    {
        fin >> vObjs[i].w >> vObjs[i].v;
        //cout << vObjs[i].w << " " << vObjs[i].v << endl;
    }
    
    sort(begin(vObjs), begin(vObjs) + n);
    
    for (int i=0; i<n; ++i)
    {
        for (int j=g; j>=vObjs[i].w; --j)
        {
            knapsack[j] = max(knapsack[j], vObjs[i].v + knapsack[j - vObjs[i].w]);
        }
    }
    
    fout << knapsack[g] << "\n";
    
    return 0; 
}