Cod sursa(job #2819764)

Utilizator KarinaDKarina Dumitrescu KarinaD Data 18 decembrie 2021 22:37:40
Problema Problema rucsacului Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>

using namespace std;

const int N = 5001;
int g [ N ], p [ N ], profit [ N ][ 2 * N ];

int main ( ) {
    
    ifstream fin ( "rucsac.in" );
    ofstream fout ( "rucsac.out" );
    
    int n, G, i, j, s = 0;
    
    fin >> n >> G;
    
    for ( i = 1; i <= n; i++ )
        fin >> g [ i ] >> p [ i ];
    
    for ( i = 1; i <= n; i++ )
        for ( j = 1; j <= G; j++ )
            if ( j >= g [ i ] )
                profit [ i ][ j ] = max ( profit [ i - 1 ][ j ], p [ i ] + profit [ i - 1 ][ j - g [ i ] ] );
            else
                profit [ i ][ j ] = profit [ i - 1 ][ j ];
    
    for ( i = 1; i <= G; i++ )
        s = max ( s, profit [ n ][ i ] );
    
    fout << s;
    
    return 0;
}