Pagini recente » Cod sursa (job #385668) | Cod sursa (job #1878426) | Cod sursa (job #1632197) | Cod sursa (job #2064103) | Cod sursa (job #3205112)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
#define VP vector<pair<int,int>>
int N, G;
int maxi = 0;
VP obiect;
void maxProfit(int indice, int sum, int weight) {
sum += obiect[indice].second, weight += obiect[indice].first;
if (weight >= G) {
if (weight > G) {
sum -= obiect[indice].second;
}
maxi = max(maxi, sum);
return;
}
for (int i = indice + 1; i < N; ++i) {
maxProfit(i, sum, weight);
}
}
int main() {
fin >> N >> G;
obiect = VP(N+1);
for (int i = 0; i < N; ++i) {
fin >> obiect[i].first >> obiect[i].second;
}
for (int i = 0; i < N; ++i) {
maxProfit(i, 0, 0);
}
fout << maxi;
}