Cod sursa(job #1657520)

Utilizator gabor.vlad13lil pump gabor.vlad13 Data 20 martie 2016 15:49:54
Problema Problema rucsacului Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#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;
}