Cod sursa(job #1490742)

Utilizator salam1Florin Salam salam1 Data 24 septembrie 2015 03:26:28
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int GMAX = 10505;
int n, gMax;
int best[2][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 = 0; j <= gMax; j++) {
      best[i & 1][j] = best[(i - 1) & 1][j];
      if (j - weight >= 0) {
        best[i & 1][j] = max(best[i & 1][j], best[(i - 1) & 1][j - weight] + profit);
      }
    }
  }

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

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