Cod sursa(job #1799086)

Utilizator ggaaggaabbiigoteciuc gabriel ggaaggaabbii Data 5 noiembrie 2016 19:00:56
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
using namespace std;
#define MAXN 5010
#define MAXG 10010
ifstream f("rucsac.in");
ofstream g("rucsac.out");
long long  n,a[2][MAXG],val[MAXN],w[MAXN],gr,maxim;
int main()
{
    f>>n>>gr;
    for(int i=1;i<=gr;i++)
    {
        a[0][i]=a[1][i]=-1;
    }
    for(int i=1;i<=n;i++)
    {
        f>>w[i]>>val[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=gr;j++)
        {
            a[i%2][j]=a[(i-1)%2][j];
            if(j-w[i]>=0&&a[i%2][j-w[i]]!=-1)
            {
                a[i%2][j]=max(a[(i-1)%2][j],a[(i-1)%2][j-w[i]]+val[i]);
                maxim=max(maxim,a[i%2][j]);
            }
        }
    }
    g<<maxim;
    return 0;
}