Cod sursa(job #1956674)

Utilizator thinkphpAdrian Statescu thinkphp Data 6 aprilie 2017 22:41:22
Problema Problema rucsacului Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 0.96 kb
#include <stdio.h>
#define FIN "rucsac.in"
#define FOUT "rucsac.out"
#define MAXW 10001
#define MAXC 5001
#define MAXP 700

int main() {

  int i,j,
      n,//number of objects
      g,//capacity of the knapsack
      w[ MAXW ], //weight
      c[ MAXC ], //costs 
      Cost[ MAXP ][ MAXP ]; //profits 

  freopen(FIN, "r", stdin);
  freopen(FOUT, "w", stdout);

  scanf("%d %d", &n, &g);

  for(i = 1; i <= n; i++) {
      scanf("%d", &w[ i ]);
      scanf("%d", &c[ i ]);
  }


  for(i = 1; i <= n; ++i) {

      for(j = 1; j <= g; ++j) {

          if(w[i] <= j) {

                  if( Cost[i - 1 ][ j ] < Cost[i - 1][j - w[i]] + c[ i ]) 
       
                      Cost[i][j] = Cost[i - 1][j - w[i]] + c[ i ];     

                  else 

                      Cost[i][j] = Cost[ i - 1][ j ];                  

           } else     Cost[i][j] = Cost[ i - 1][ j ];
      }
  } 

 printf("%d", Cost[n][g]);

 return(0);
}