Cod sursa(job #2806585)

Utilizator victorzarzuZarzu Victor victorzarzu Data 22 noiembrie 2021 20:13:18
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
#define oo 0x3f3f3f3f 
#define greutate first
#define profit second

using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int ruc[10001];
pair<int, int> vals[5001];
int n, gre;

void read()
{
   f>>n>>gre;
   int x, y;
   for(int i = 1;i <= n;++i)
    f>>x>>y, vals[i] = make_pair(x, y); 
}

void solve()
{
  for(int i = 1;i <= n;++i)
    for(int j = gre - vals[i].greutate;j >= 0;--j)
      ruc[j + vals[i].greutate] = max(ruc[j] + vals[i].profit, ruc[j + vals[i].greutate]);
  int maximum = 0;
  for(int i = gre;i >= 0;--i)
    maximum = max(maximum, ruc[i]);
  g<<maximum<<'\n';
}

int main()
{
  read();
  solve();
  return 0;
}