Cod sursa(job #2205995)

Utilizator andytosaAndrei Tosa andytosa Data 20 mai 2018 20:15:23
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#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;
}