Pagini recente » Cod sursa (job #2450162) | Cod sursa (job #980013) | Cod sursa (job #456644) | Cod sursa (job #976409) | Cod sursa (job #2272359)
#include <bits/stdc++.h>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int DP[2][10010];
struct obiect {
int w, p;
};
int main() {
ios::sync_with_stdio(false); in.tie(0); out.tie(0);
int n, g; in >> n >> g;
vector< obiect > obiecte(n + 1);
for(int i = 1; i <= n; ++i) {
in >> obiecte[i].w >> obiecte[i].p;
}
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < obiecte[i].w; ++j) {
DP[i % 2][j] = DP[abs(i % 2 - 1)][j];
}
for(int j = obiecte[i].w; j <= g; ++j) {
DP[i % 2][j] = max(DP[abs(i % 2 - 1)][j], obiecte[i].p + DP[abs(i % 2 - 1)][j - obiecte[i].w]);
}
}
out << DP[n % 2][g] << "\n";
in.close(); out.close();
return 0;
}