Pagini recente » Cod sursa (job #275348) | Cod sursa (job #1546848) | Cod sursa (job #333032) | Cod sursa (job #895239) | Cod sursa (job #2496606)
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cstring>
using namespace std;
//#include <iostream>
#include <fstream>
//ifstream cin ("input.in");
//ofstream cout ("output.out");
ifstream cin ("rucsac.in");
ofstream cout ("rucsac.out");
static const int NMAX = 1e5+5;
int n, g;
int dp[1005];
vector <int> profit[NMAX];
vector <pair<int, int>> weightAndProfit;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>g;
for ( int i = 1; i <= n; ++i ) {
int w, p;
cin>>w>>p;
if ( !w ) {
dp[w] += p;
}
else {
profit[w].push_back(p);
}
}
for ( int i = 1; i <= g; ++i ) {
if ( profit[i].size() ) {
//sort(profit[i].begin(), profit[i].end());
for ( int j = 0; j < profit[i].size(); ++j ) {
weightAndProfit.push_back({i, profit[i][j]});
}
}
}
for (int i = 0; i < weightAndProfit.size(); ++i ) {
for ( int j = g-weightAndProfit[i].first; j >= 0; j-- ) {
dp[j+weightAndProfit[i].first] = max(dp[j+weightAndProfit[i].first], dp[j]+weightAndProfit[i].second);
}
}
int ans = 0;
for ( int i = 0; i <= g; ++i ) {
ans = max(ans, dp[i]);
}
cout<<ans<<'\n';
}