Cod sursa(job #1784118)

Utilizator AndreiStAndrei Popa AndreiSt Data 19 octombrie 2016 19:52:33
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");

int n;
int Values[5001];
int Weights[5001];
int maxWeight;
int dp[5001];
int mass=0;
int value=0;

void citire()
{
    int i;
    f>>n;
    f>>maxWeight;
    for(i=1;i<=n;i++)
    {
        f>>Weights[i];
        f>>Values[i];
    }
}
void knapsack()
{
    int i;
    int j;

    for(i=1;i<=n;i++)
    {
        for(j=maxWeight;j>=0;j--)

            if(j+Weights[i]<=maxWeight)
            {
                 mass=j+Weights[i];
                 value=dp[j]+Values[i];

                if(dp[mass]<value)
                dp[mass]=value;
            }

    }

    int max1=0;
    for(i=1;i<=maxWeight;i++)
    if(dp[i]>max1)
    max1=dp[i];

    g<<max1;

}
int main()
{
    citire();
    knapsack();

    f.close();
    g.close();
    return 0;
}