Cod sursa(job #1416507)

Utilizator vazanIonescu Victor Razvan vazan Data 8 aprilie 2015 11:05:44
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include<cstdio>
using namespace std;
int d[10001];
int main()
{
    FILE *in, *out;
    in=fopen("rucsac.in", "r");
    out=fopen("rucsac.out", "w");
    int n, gr, i, j, grlm, prf, smax;
    fscanf(in, "%d%d", &n, &gr);
    smax=0;
    d[0]=0;
    for(i=1; i<=n; i++)
    {
        fscanf(in, "%d%d", &grlm, &prf);
        for(j=smax; j>0; j--)
            if(d[j])
                if(j+grlm<=gr && d[j]+prf>d[j+grlm])
                {
                    d[j+grlm]=d[j]+prf;
                    if(j+grlm>smax)
                        smax=j+grlm;
                }
        if(grlm<=gr&&prf>d[grlm])
        {
            d[grlm]=prf;
            if(grlm>smax)
                smax=grlm;
        }
    }
    int rasp=0;
    for(i=1; i<=smax; i++)
        if(d[i]>rasp)
            rasp=d[i];
    fprintf(out, "%d", rasp);
    fclose(in);
    fclose(out);
    return 0;

}