Pagini recente » Cod sursa (job #406217) | Cod sursa (job #3255321) | Cod sursa (job #2855218) | Cod sursa (job #792858) | Cod sursa (job #1809159)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
int nr,i,grmax,gr[5005],pr[5005],prmax;
bool srt=0;
int main()
{ f>>nr>>grmax;
for(i=1;i<=nr;i++)
f>>gr[i]>>pr[i];
while(srt==0)
{srt=1;
for(i=1;i<nr;i++)
if(gr[i]>gr[i+1]){swap(pr[i+1],pr[i]);
swap(gr[i+1],gr[i]);
srt=0;}
}
for(i=1;i<=nr;i++)
{prmax+=pr[i];
grmax-=gr[i];
if(grmax<=0){prmax-=pr[i];break;}
}
g<<prmax;
}