Cod sursa(job #1379884)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 6 martie 2015 20:01:33
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

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

int S, n, x, y, nr;
vector<int> a;

int main()
{
    is >> n >> S;
    a = vector<int>( S + 1, -1 );
    a[0] = 0;
    for ( int i = 1; i <= n; i++ )
    {
        is >> x >> y;
        for ( int j = S - x; j >= 0; j-- )
            if ( a[j] != -1 && a[j+x] < a[j] + y )
            {
                a[j+x] = a[j] + y;
                nr = max( nr, a[j+x] );
            }
    }
    os << nr;
    is.close();
    os.close();
    return 0;
}