Cod sursa(job #2587542)

Utilizator As932Stanciu Andreea As932 Data 22 martie 2020 22:04:32
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");

const int nmax=5005;
const int gmax=10005;

int n,g;
int w[nmax],p[nmax],dp[2][gmax];

int main()
{
    fin>>n>>g;
    for(int i=1;i<=n;i++)
        fin>>w[i]>>p[i];

    int l=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=g;j++)
        {
            dp[1-l][j]=dp[l][j];
            if(w[i]<=j)
                dp[1-l][j]=max(dp[1-l][j],dp[l][j-w[i]]+p[i]);
        }
        l=1-l;
    }

    fout<<dp[l][g];

    return 0;
}