Cod sursa(job #2927878)

Utilizator AlexandruBenescuAlexandru Benescu AlexandruBenescu Data 21 octombrie 2022 18:40:45
Problema Problema rucsacului Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.45 kb
#include <bits/stdc++.h>
#define L 5005
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
long long w[L], p[L], dp[L];
int main(){
  long long n, g, i, x, ans = 0;
  fin >> n >> g;
  for (i = 1; i <= n; i++)
    fin >> w[i] >> p[i];
  for (i = 1; i <= n; i++)
    for (x = g; x >= w[i]; x--){
      dp[x] = max(dp[x], dp[x - w[i]] + p[i]);
      ans = max(ans, dp[x]);
    }
  fout << ans << "\n";
  return 0;
}