Cod sursa(job #1767814)

Utilizator tudorcomanTudor Coman tudorcoman Data 29 septembrie 2016 19:33:38
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxN = 5000;
int weight[maxN + 5], profit[maxN + 5];
int dp[maxN + 5];

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

  int N, G, ans = 0;
  scanf("%d %d", &N, &G);
  for(int i = 1; i <= N; ++ i)
    scanf("%d %d", &weight[i], &profit[i]);

  for(int i = 1; i <= N; ++ i) {
    for(int j = G - weight[i]; j >= 0; -- j) {
      int k = j + weight[i];
      dp[k] = max(dp[k], dp[j] + profit[i]);
      ans = max(ans, dp[k]);
    }
  }

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