Cod sursa(job #2173664)

Utilizator Tataru_AdelinTataru Adelin Tataru_Adelin Data 15 martie 2018 23:25:09
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

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

class Object
{
private:
    int val=0;
    int weight=0;
public:
    void SetValWei(int Val, int Wei)
    {
        val=Val;
        weight=Wei;
    }

    int GetVal(void)
    {
        return val;
    }

    int GetWei(void)
    {
        return weight;
    }
};

bool comp(Object a, Object b)
{
    if(a.GetWei()!=b.GetWei())
        return a.GetWei()<b.GetWei();
    else
        return a.GetVal()<b.GetVal();
}


int main()
{
    int n,weight;
    fin>>n>>weight;
    vector<Object>things(n+1);
    int x,y;

    for(int i=1;i<=n;i++)
    {
        fin>>x>>y;
        things[i].SetValWei(y,x);
    }

    sort(things.begin()+1,things.end(),comp);

    vector<int>past(weight+1,0);
    vector<int>now(weight+1,0);

    for(int i=1;i<=n;i++)
    {

        for(int j=1;j<things[i].GetWei();j++)
        {
            now[j]=past[j];
        }

        for(int j=things[i].GetWei(); j<=weight;j++)
        {
            now[j]=max(past[j],past[j-things[i].GetWei()]+things[i].GetVal());
        }

        swap(now,past);

    }

    fout<<past[weight]<<'\n';



    return 0;
}