Cod sursa(job #1490743)

Utilizator salam1Florin Salam salam1 Data 24 septembrie 2015 03:28:06
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int GMAX = 10505;
int n, gMax;
int best[GMAX];

int main() {
  freopen("rucsac.in", "r", stdin);
  freopen("rucsac.out", "w", stdout);

  scanf("%d%d", &n, &gMax);
  int weight, profit;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &weight, &profit);
    for (int j = gMax; j >= 0; j--)
      if (j - weight >= 0) {
        best[j] = max(best[j], best[j - weight] + profit);
      }
  }

  int res = 0;
  for (int w = 0; w <= gMax; w++)
    res = max(res, best[w]);

  printf("%d\n", res);
  return 0;
}