Cod sursa(job #2191753)

Utilizator UnseenMarksmanDavid Catalin UnseenMarksman Data 3 aprilie 2018 17:01:32
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
using namespace std;

const int maxn = 5002;
const int maxg = 10002;

int n, g, w[maxn], p[maxn], M[2][maxg];

int main()
{
    ifstream fin("rucsac.in");
    ofstream fout("rucsac.out");

    fin>>n>>g;
    for(int i=1; i<=n; i++)
    {
        fin>>w[i]>>p[i];
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<=g; j++)
        {
            if(w[i]>j)
            {
                M[1][j]=M[0][j];
            }
            else
            {
                M[1][j]=max(M[0][j],M[0][j-w[i]]+p[i]);
            }
        }
        for(int j=0; j<=g; j++)
        {
            M[0][j]=M[1][j];
        }
    }
    fout<<M[1][g]<<'\n';
    return 0;
}