Cod sursa(job #1862881)

Utilizator alxi.2001Alex Ionescu alxi.2001 Data 30 ianuarie 2017 13:22:52
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <fstream>
#include <algorithm>

using namespace std;
ifstream in("rucsac.in"); ofstream out("rucsac.out");

struct object
{
int w, p;
};

object a[5001];
int n, wmax, pmax, wcrt;

bool cmp(object a, object b)
{
return (a.w<=b.w && a.p>=b.p);
}

int main()
{
in>>n>>wmax;

for (int i=1; i<=n; ++i)
  in>>a[i].w>>a[i].p;

sort(a+1, a+1+n, cmp);

for (int i=1; i<=n && wcrt<g; ++i)
  if (wcrt+a[i].w<=wmax)
    pmax+=a[i].p; wcrt+=a[i].w;

out<<pmax;
}