Cod sursa(job #1811643)

Utilizator LazarAndreiLazar Andrei Teodor LazarAndrei Data 21 noiembrie 2016 14:19:40
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

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

int W[5001] , P[5001] , best[10001] , N , max_W ;

void read_input ()
{
    in >> N >> max_W;
    for(int i = 1 ; i <= N ; ++ i)
        in >> W[i] >> P[i];
}

void solve ()
{
    int Max = 0;
    for(int i = 1 ; i <= N ; ++i)
    {
        for(int j = max_W - W[i] ; j >= 0 ; -- j)
        {
            if(best[j + W[i]] < best[j] + P[i])
            {
                best[j + W[i]] = best[j] + P[i];
               if(best[j + W[i]] > Max)
                    Max = best[j + W[i]];
            }
        }
    }

    out << Max;
}

int main()
{
    read_input ();
    solve ();
    return 0;
}