Cod sursa(job #3039320)

Utilizator VertimaXxFlorea Vlad VertimaXx Data 28 martie 2023 13:44:33
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
using namespace std;
long long int n,G;
struct ruc{
    float g;
    float p;
    float rap;
};
bool cmp(ruc x, ruc y)
{
    if(x.rap < y.rap)
    {
        return true;
    } else if(x.rap > y.rap)
    {
        return false;
    } else
    {
        if(x.g <= y.g)
        {
            return true;
        } else
        {
            return false;
        }
    }
}
ruc v[5005];
int main(){
    cin >> n >> G;
    for(int i =1;i<=n;i++)
    {
        cin >> v[i].g >> v[i].p;
        v[i].rap = v[i].g / v[i].p;
    }
    sort(v+1,v+n+1,cmp);
    int gcur = 0;
    int profit = 0;
    int last = 0;
    int i = 1;
    while(i <= n && gcur <= G)
    {
        gcur += v[i].g;
        profit += v[i].p;
        last = i;
        i++;
    }
    if(gcur > G)
    {
        profit -= v[last].p;
    }
    cout << profit;
}