Cod sursa(job #44736)

Utilizator Bluedrop_demonPandia Gheorghe Bluedrop_demon Data 31 martie 2007 17:55:36
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
// Problema energii
// Particularizare a problemei rucsac

#include <stdio.h>
#define MAX        1001
#define MAXIM      100000

int G, W;
int E[MAX], C[MAX];
long A[MAXIM];

int main()
{
    long suma = 0;
    long cost = 0;
    freopen( "energii.in", "rt", stdin );
             scanf( "%d", &G );
             scanf( "%d", &W );
             long i;
             for( i=1; i<=G; i++ )
                  {
                       scanf( "%d %d", &E[i], &C[i] );                       
                       suma += E[i];
                       cost += C[i];
                  }
    fclose( stdin );
        
    freopen( "energii.out", "wt", stdout );
             if( suma < W )
                 {
                      printf( "-1\n" );
                      fclose( stdout );
                      return 0;
                 }
             long max;
             int j;
             A[0] = 0;
             for( i =1; i<=suma-W; i++ )
                  {
                        j = 1;
                        max = 0;
                        while( E[j] <= i )
                               {
                                    if( C[j] + A[i-E[j]] > max ) max = C[j] + A[i-E[j]];
                                    j++;
                               }
                        A[i] = max;
                  }
             printf( "%ld\n", cost-A[suma-W]+1 );
    fclose( stdout );
    return 0;
}