Cod sursa(job #3247154)

Utilizator silvialutschSilvia Lutsch silvialutsch Data 5 octombrie 2024 21:29:16
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <algorithm>
using namespace std;

pair<pair<double, int>, int> obiecte[1000];

int n, gm;
double V;

int main() {
     cin >> n >> gm;
     for(int i = 0; i < n; i++) {
        cin >> obiecte[i].first.second >> obiecte[i].second;
        obiecte[i].first.first = 1.00 * obiecte[i].second / obiecte[i].first.second;
     }
     sort(obiecte, obiecte + n);
     for(int i = n - 1; i >= 0; i--) {
        if(gm - obiecte[i].first.second < 0) {
            V += (double)gm / obiecte[i].first.second * obiecte[i].second;
            break;
        } else {
            V += obiecte[i].second;
            gm -= obiecte[i].first.second;
        }
     }
     cout << V;

}