Cod sursa(job #2293860)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 1 decembrie 2018 17:32:22
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
using namespace std;

const int NR = 10005 ;

ifstream f ("rucsac.in") ;
ofstream g ("rucsac.out") ;

int t [ 2 ][ NR ] ;
int maxim ;

int main() {
    
    int n , G ; f >> n >> G ;
    
    while ( n -- )
    {
        int x , y ; f >> x >> y ;
        for ( int j = 1 ; j <= G ; j ++ )
        {
            if ( j < x )    t [ n % 2 ][ j ] = t [ ( n + 1 ) % 2  ][ j ] ;
            else
            {
               t [ n % 2 ][ j ] = max ( t [ ( n + 1 ) % 2  ][ j ] , t [ ( n + 1 ) % 2  ][ j - x ] + y  ) ;        
                
            }
        }
        
    }
    
    g << t [ 0 ][ G ] ;
	return 0;
}