Cod sursa(job #1956827)

Utilizator thinkphpAdrian Statescu thinkphp Data 7 aprilie 2017 08:26:59
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <iostream>
#include <fstream>
#define FIN "rucsac.in"
#define FOUT "rucsac.out"
#define MAXW 10001
#define MAXC 10001
#define MAXP 10001

using namespace std;

int max(int a, int b) {if(a>b) return a; else return b;}

int main() {
  int i, j, n, g;
  int w[MAXW], c[MAXC], Cost[MAXP];

  ifstream fin(FIN);
  ofstream fout(FOUT);

  fin>>n>>g;

  for(i = 1; i <= n; i++) {
      fin>>w[i]>>c[i];
  }

  for(i = 0; i <= n; ++i) 

      for(j = g; j >= w[i]; j--) 

          Cost[ j ] = max(Cost[j], Cost[j - w[i]] + c[i]);



  fout<<Cost[g];


 return(0);
}