Cod sursa(job #1349732)

Utilizator western100Sutu Eusebiu western100 Data 20 februarie 2015 14:08:49
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>

using namespace std;

ifstream f("rucsac.in");
ofstream g("rucsac.out");

int a[10001],b[10001];
int main()
{
    int n,g1,g2,p,i,j;
    bool k=false;
    f>>n>>g1;
    for(i=1;i<=n;i++)
    {
        f>>g2>>p;
        if(k)
        {
            for(j=1;j<g2;j++)
                b[j]=a[j];
            for(j=g2;j<=g1;j++)
                b[j]=max(a[j],a[j-g2]+p);
            k=false;
        }
        else
        {
            for(j=1;j<g2;j++)
                a[j]=b[j];
            for(j=g2;j<=g1;j++)
                a[j]=max(b[j],b[j-g2]+p);
            k=true;
        }
    }
    if(k)
        g<<a[g1];
    else
        g<<b[g1];
    return 0;
}