Cod sursa(job #711501)

Utilizator fhandreiAndrei Hareza fhandrei Data 12 martie 2012 11:40:45
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
//Include
//#include <iostream>
#include <fstream>
#include <set>
#include <utility>
using namespace std;

//Definitii
#define pereche pair<int, int>
#define greutate first
#define cost second

//Clase
class dupaEficienta
{
	public:
	bool operator() (pereche a, pereche b)
	{	return ((double)a.cost/a.greutate > (double)b.cost/b.greutate);	}
};

//Variabile
ifstream in("rucsac.in");
ofstream out("rucsac.out");

int n, g;
int profit;

pereche curent;
multiset<pereche, dupaEficienta> s;
multiset<pereche, dupaEficienta>::iterator it, end;

//Main
int main()
{
	in >> n >> g;
	
	for(int i=1 ; i<=n ; ++i)
	{
		in >> curent.greutate >> curent.cost;
		s.insert(curent);
	}
	
	end = s.end();
	for(it=s.begin() ; it!=end && g ; ++it)
	{
		//system("cls");
		//cout << it->greutate << ' ' << it->cost;
		if(it->greutate <=g)
		{
			g -= it->greutate;
			profit += it->cost;
		}
	}
	
	out << profit;
	
	
	
	in.close();
	out.close();
	return 0;
}