Pagini recente » Cod sursa (job #1655808) | Cod sursa (job #1307100) | Cod sursa (job #567178) | Cod sursa (job #931065) | Cod sursa (job #2491134)
#include <fstream>
using namespace std;
struct obiect{
int greutate;
int profit;
};
ifstream f("rucsac.in");
ofstream out("rucsac.out");
void sortare(obiect x[],int n)
{
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(x[i].profit<x[j].profit){
obiect aux=x[i];
x[i]=x[j];
x[j]=aux;
}else{
if(x[i].profit==x[j].profit){
if(x[i].greutate>x[j].greutate){
obiect aux=x[i];
x[i]=x[j];
x[j]=aux;
}
}
}
}
}
}
int main()
{
int n,g;
f>>n>>g;
obiect x[5010];
for(int i=1;i<=n;i++){
f>>x[i].greutate;
f>>x[i].profit;
}
sortare(x,n);
int rez=0;
for(int i=1;i<=n && g>0;i++){
if(g>=x[i].greutate){
rez=rez+x[i].profit;
g=g-x[i].greutate;
}
}
out<<rez;
return 0;
}