Cod sursa(job #2720382)

Utilizator etohirseCristi Cretu etohirse Data 10 martie 2021 19:33:16
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

std::ifstream fin("rucsac.in");
std::ofstream fout("rucsac.out");

static const int mxn = 5e4;

int n, g, w[1 + mxn], p[1 + mxn], ans[1 + mxn];

int main(){
  fin >> n >> g;
  for (int i = 1; i <= n; ++i){
    fin >> w[i] >> p[i];
  }

  for (int i = 1; i <= n; ++i){
    for (int j = g - w[i]; j >= 0; --j){
      if (ans[j] != 0 || j == 0){
        ans[j + w[i]] = std::max(ans[j + w[i]], ans[j] + p[i]);
      }
    }
  }

  int rez = -1;
  for (int i = 1; i <= g; ++i){
    rez = std::max(rez, ans[i]);
  }

  fout << rez << '\n';
  return 0;
}