Pagini recente » Cod sursa (job #2681802) | Cod sursa (job #3228956) | Istoria paginii runda/bomba2 | Cod sursa (job #1374317) | Cod sursa (job #1657520)
#include <iostream>
#include <cstdio>
#include <cstring>
#define NMAX 5000
using namespace std;
int n, g;
int profitMax;
int poz[NMAX];
struct obiect
{
int greutate;
int profit;
}vec[NMAX];
bool bun(int k)
{
int aux = 0;
for (int i=1; i<=k; i++)
{
aux+=vec[poz[i]].greutate;
}
if (aux > g)
return false;
return true;
}
int profit(int k)
{
int aux = 0;
for (int i=1; i<=k; i++)
{
aux+=vec[poz[i]].profit;
}
return aux;
}
void bt(int k = 1)
{
for (int i = poz[k-1]+1; i<=n; i++)
{
poz[k] = i;
if(bun(k))
{
int aux = profit(k);
if (aux > profitMax)
profitMax = aux;
}
bt(k+1);
}
}
void read()
{
scanf("%d %d\n", &n, &g);
for (int i=1; i<=n; i++)
{
scanf("%d %d\n", &vec[i].greutate, &vec[i].profit);
}
}
int main()
{
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
read();
bt();
printf("%d", profitMax);
return 0;
}