Cod sursa(job #2683533)

Utilizator PopescuAndreiAlexandruPopescu Andrei Alexandru PopescuAndreiAlexandru Data 11 decembrie 2020 16:31:04
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 10005;

int n,g,G[N],S[N],A[N],B[N];

void Read()
{
    fin>>n>>g;
    for(int i=1;i<=n;i++)
            fin>>G[i]>>S[i];
}

void FindSol()
{
    for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=g;j++)
                {
                    B[j]=A[j];
                    if(G[i]<=j)
                        B[j]=max(B[j],A[j-G[i]]+S[i]);
                }
            for(int j=0;j<=g;j++)
                A[j]=B[j];
        }
}

void Print()
{
    int ans=0;
    for(int i=0;i<=g;i++)
            ans=max(ans,A[i]);
    fout<<ans<<'\n';
}

int main()
{
    Read();
    FindSol();
    Print();
}