Pagini recente » Cod sursa (job #2382271) | Cod sursa (job #584739) | Cod sursa (job #2945235) | Cod sursa (job #340912) | Cod sursa (job #3254906)
#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, W;
pair<int, int> a[nmax + 5];
pair<int, int> pref[nmax + 5];
bool chosen[nmax + 5];
int weight = 0;
int profit = 0;
int max_profit = -1;
inline void take(int i) {
weight += a[i].first;
profit += a[i].second;
}
inline void untake(int i) {
weight -= a[i].first;
profit -= a[i].second;
}
void backtrack(int i) {
if (i > n) {
if (profit > max_profit) {
max_profit = profit;
}
return;
}
// nu il luam
backtrack(i + 1);
// il luam
take(i);
if (weight <= W) { // 1. greutatea curenta nu trebuie sa depaseasca greutatea maxima
backtrack(i + 1);
}
untake(i);
}
int main() {
fin >> n >> W;
for (int i = 1; i <= n; ++i) {
fin >> a[i].first >> a[i].second;
}
backtrack(1);
fout << max_profit;
}