Pagini recente » Cod sursa (job #1945092) | Cod sursa (job #265487) | Cod sursa (job #2431402) | Cod sursa (job #1412996) | Cod sursa (job #2452686)
#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] < profit[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;
}