Cod sursa(job #2496606)

Utilizator HumikoPostu Alexandru Humiko Data 21 noiembrie 2019 10:16:26
Problema Problema rucsacului Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#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';
}