Cod sursa(job #1846913)

Utilizator stefan_gheorgheGheorghe Stefan stefan_gheorghe Data 14 ianuarie 2017 09:56:21
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.34 kb
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int p[10001],n,G,gr,val,j,i;
int main()
{
    f>>n>>G;
    for(i=1;i<=n;i++)
    {
        f>>gr>>val;
        for(j=G;j>=gr;j--)
            if(p[j]<p[j-gr]+val)p[j]=p[j-gr]+val;
    }
    while (!p[G]) G--;
    g<<p[G];
    return 0;
}