Pagini recente » Cod sursa (job #1233535) | Rating Nicolaescu Horia (Horica) | Cod sursa (job #84337) | Cod sursa (job #3168295) | Cod sursa (job #2953300)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int NMAX = 5000;
const int GMAX = 10000;
int n, g;
int dp[GMAX + 1];
vector<int> obj[GMAX + 1];
void maxSelf(int &a, int b) {
a = max(a, b);
}
int main() {
ios_base :: sync_with_stdio(false);
fin >> n >> g;
for(int i = 1; i <= n; i++) {
int w, p;
fin >> w >> p;
if(w == 0) {
dp[0] += p;
} else {
obj[w].push_back(p);
}
}
for(int i = 1; i <= g; i++) {
sort(obj[i].begin(), obj[i].end(), greater<int>());
}
for(int i = 1; i <= g; i++) {
for(int j = 1; j <= (int) obj[i].size() && i * j <= g; j++) {
for(int k = g; k >= i * j; k--) {
maxSelf(dp[k], dp[k - i] + obj[i][j - 1]);
}
}
}
int ans = 0;
for(int i = 0; i <= g; i++) {
maxSelf(ans, dp[i]);
}
fout << ans << '\n';
fin.close();
fout.close();
return 0;
}