Cod sursa(job #2224705)

Utilizator HumikoPostu Alexandru Humiko Data 24 iulie 2018 20:41:59
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, limit_of_Weigth, current_Weigth, current_Profit, answer;
int dp[10005];

vector <int> profit[10005];
vector <pair<int, int>> weight_and_Profit;


int main()
{
    fin>>n>>limit_of_Weigth;

    for ( int i = 1; i <= n; ++i )
    {
        fin>>current_Weigth>>current_Profit;

        if ( current_Weigth == 0 )
            dp[0] += current_Profit;
        else
            profit[current_Weigth].push_back(current_Profit);
    }

    for ( int i = 1; i <= limit_of_Weigth; ++i )
    {
        if ( profit[i].size() )
        {
            sort (profit[i].begin(), profit[i].end());

            for ( int j = 0; j < (int)profit[i].size(); ++j )
                weight_and_Profit.push_back( {i, profit[i][j]} );
        }
    }

    for ( int i = 0; i < weight_and_Profit.size(); ++i )
        for ( int j = limit_of_Weigth-weight_and_Profit[i].first; j >= 0; j-- )
            dp[j+weight_and_Profit[i].first] = max( dp[j+weight_and_Profit[i].first], dp[j]+weight_and_Profit[i].second );

    for ( int i = 0; i <= limit_of_Weigth; ++i )
        answer = max(answer, dp[i]);

    fout<<answer<<'\n';
}