Pagini recente » Cod sursa (job #211035) | Cod sursa (job #1138090) | Cod sursa (job #3227550) | Cod sursa (job #2190064) | Cod sursa (job #2224705)
#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';
}