Cod sursa(job #2010944)

Utilizator DianaPopDiana Pop DianaPop Data 14 august 2017 19:28:18
Problema Energii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <stdio.h>
#include <stdlib.h>

int N,W,DP[1005][50005],S;
int inf = 1000000001;
void energii(){

    scanf( "%d%d" , &N , &W );

    for( int i = 0 ; i <= N ; i++ ){
        for( int j = 1 ; j <= W ; j++ ){
            DP[i][j] = inf;
        }
    }
    for( int i = 1 ; i <= N ; i++ ){

       int e , c ;
       scanf( "%d%d" , &e , &c );
       S = S + e ;
       for( int j = 1 ; j <= W ; j++ ){
          if( j > e ){
            if( DP[i-1][j] < DP[i-1][j-e]+c ){
                DP[i][j] = DP[i-1][j];
            }else{
                DP[i][j] = DP[i-1][j-e]+c;
            }
          }else{
              if( DP[i-1][j] < c ){
                DP[i][j] = DP[i-1][j];
              }else{
                DP[i][j] = c;
              }
          }
       }
    }
}

int main(){

    freopen( "energii.in" , "r" , stdin );
    freopen( "energii.out" , "w" , stdout );

    energii();

    if( S < W )
        printf("-1");
    else
      printf( "%d" , DP[N][W] );
    return 0;
}