Pagini recente » Cod sursa (job #2122565) | Cod sursa (job #1123518) | Cod sursa (job #813808) | Cod sursa (job #855287) | Cod sursa (job #2973928)
#include <bits/stdc++.h>
using namespace std;
bool ckmax(int &a, int b) { return (b > a ? a = b, 1 : 0); }
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int G = 10001;
vector<int> profit(G, -1);
int main() {
int n, g;
in >> n >> g;
vector<int> w(n + 1), p(n + 1);
for(int i = 1; i <= n; i++) {
in >> w[i] >> p[i];
}
profit[0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = g - w[i]; j >= 0; j--) {
if(profit[j] != -1)
ckmax(profit[j + w[i]], profit[j] + p[i]);
}
}
int ans = *max_element(profit.begin(), profit.begin() + g + 1);
out << ans << '\n';
}