Cod sursa(job #2452687)

Utilizator baTTLe4u_15Nita Iulian baTTLe4u_15 Data 31 august 2019 20:37:50
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ifstream in("rucsac.in");
	ofstream out("rucsac.out");

	int nrObiecte;
	int G;
	int greutate[5001], profit[5001];
	int profitTotal = 0;

	in >> nrObiecte >> G;
	for(int i=0; i<nrObiecte; i++)
		in >> greutate[i] >> profit[i];

	for(int i=0; i<nrObiecte-1; i++)
		for(int j=i+1; j<nrObiecte; j++)
			if(profit[i]/greutate[i] < profit[j]/greutate[j])
			{
				int aux1, aux2;
				aux1 = greutate[i];
				aux2 = profit[i];

				greutate[i] = greutate[j];
				profit[i] = profit[j];

				greutate[j] = aux1;
				profit[j] = aux2;
			}

    int greutateActuala = 0;
	for(int i=0; i<nrObiecte; i++)
	{
		if(greutateActuala + greutate[i] <= G)
		{
			profitTotal += profit[i];
			greutateActuala += greutate[i];
		}
		else
			break;
	}

	out << profitTotal;
}