Cod sursa(job #1863231)

Utilizator miha1000Dica Mihai miha1000 Data 30 ianuarie 2017 20:02:52
Problema Problema rucsacului Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#define nmax 5005

using namespace std;

int  v[nmax];

int n,w,pmax,i;
pair <int,int > per[nmax];
ifstream f("rucsac.in");
ofstream g("rucsac.out");

void sub(int k){
    int i,cost,profit;
    if (k>n){
        cost=0;
        profit=0;
        for(i=1;i<=n;i++){
            if (v[i]==1) {
                cost=cost+per[i].first;
                profit=profit+per[i].second;
            }
            if (cost<=w && profit > pmax) pmax=profit;
        }
        return;
    }
    v[k]=1;
    sub(k+1);
    v[k]=0;
    sub(k+1);
}

int main()
{
    int c,p;
    f >> n >> w;
    for(i=1;i<=n;i++){
        f >> c >> p;
        per[i]=make_pair(c,p);
    }
    sub(1);
    g << pmax;
}