Cod sursa(job #1567256)

Utilizator tc_iuresiures tudor-cristian tc_iures Data 13 ianuarie 2016 00:17:04
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>

using namespace std;

const int Nmax = 5005;
const int Gmax = 10005;

int N, G;
int A[Gmax];
pair<int,int> ob[Nmax];

void read()
{
    ifstream f("rucsac.in");
    f >> N >>G;
    for(int i = 1; i <= N; i ++)
    {
        f >> ob[i].first;
        f >> ob[i].second;
    }
    f.close();
}

int max(int x, int y)
{
   if(x >= y)
   {
       return x;
   }
   return y;
}

void solve()
{
    for(int i = 1; i <= N; i ++)
    {
        for(int j = G-ob[i].first; j >= 0; j --)
        {
            if(A[j+ob[i].first] < A[j] + ob[i].second)
            {
                A[j+ob[i].first] = A[j] + ob[i].second;
            }
        }
    }
}

void print()
{
    ofstream g("rucsac.out");
    g << A[G];
    g.close();
}

int main()
{
    read();
    solve();
    print();
    return 0;
}