Cod sursa(job #2467276)

Utilizator GrimpowRadu Andrei Grimpow Data 3 octombrie 2019 22:13:47
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <cstdio>
 
using namespace std;
 
 
int n, maxWeight, l=0;
int weight[5010], profit[5010];
long long D[2][10010],sol=0;
 
int main()
{
    
    
    freopen("rucsac.in", "r", stdin);
    freopen("rucsac.out", "w", stdout);
    scanf("%d%d", &n, &maxWeight);

    for(int i = 0; i < n; ++i)
        scanf("%d%d", &weight[i], &profit[i]);

    for(int i = 0; i < n; ++i, l = 1 - l)// l reprezinta pozitia prezenta, 1-l reprezinta pozitia veche
        for(int currentWeight=0; currentWeight <= maxWeight ; ++currentWeight)
        {
            D[l][currentWeight] = D[1-l][currentWeight]; 
            if(weight[i] <= currentWeight )
                if( D[l][currentWeight] < D[1-l][currentWeight - weight[i]] + profit[i]   )
                    D[l][currentWeight] = D[1-l][currentWeight - weight[i]] + profit[i];
            if(D[l][currentWeight] > sol)
                sol=D[l][currentWeight];
        }
    printf("%ld\n", sol);
    return 0;
}