Cod sursa(job #2988702)

Utilizator smunteanuMunteanu Stefan Catalin smunteanu Data 5 martie 2023 13:02:47
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;

const int nmax = 5007;
const int cmax = 10007;

int n, c;
int w[nmax];
int p[nmax];
int dp[cmax];

void solve() {

  cin >> n >> c;
  for (int i = 1; i <= n; i++) cin >> w[i] >> p[i];

  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = c - w[i]; j >= 0; j--) {
      if (dp[j] == -1) continue;
      dp[j + w[i]] = max(dp[j + w[i]], dp[j] + p[i]);
    }
  }

  cout << *max_element(dp, dp + c + 1) << endl;
  
}

int main() {

  #ifdef LOCAL
  freopen("file.in", "r", stdin);
  #else
  freopen("rucsac.in", "r", stdin);
  freopen("rucsac.out", "w", stdout);
  #endif

  ios_base::sync_with_stdio(false), cin.tie(NULL);

  solve();
}