Pagini recente » Cod sursa (job #2652206) | Cod sursa (job #1047400) | Cod sursa (job #2628429) | Cod sursa (job #2628136) | Cod sursa (job #2205995)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct cel{
int w, p;
double coef;
} v[5010];
int d[10010], n, k;
bool cmp(cel x, cel y){
if (x.coef > y.coef)
return true;
if (x.coef == y.coef && x.w > y.w)
return true;
return false;
}
int main()
{
fin >> n >> k;
for(int i = 1; i <= n; ++i){
fin >> v[i].w >> v[i].p;
v[i].coef = ((double) v[i].p) / ((double) v[i].w);
}
sort(v + 1, v + n + 1, cmp);
int profit = 0;
for (int i = 1; i <= n; i++) {
if (v[i].w <= k) {
k -= v[i].w;
profit += v[i].p;
}
else break;
}
fout << profit;
return 0;
}