Cod sursa(job #2889777)

Utilizator VictorB11Badulescu Victor VictorB11 Data 13 aprilie 2022 11:29:53
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace  std;
ifstream fin ("rucsac.in");
ofstream fout ("rucsac.out");
struct ob{
    int g;
    int val;
};
int main()
{
    int n, greutate;
    vector<ob> obiecte;
    fin>>n>>greutate;
    ob temp;
    for(int i=0;i<n;i++)
    {
        fin>>temp.g>>temp.val;
        obiecte.push_back(temp);
    }
    sort(obiecte.begin(), obiecte.end(), [](ob a, ob b) {return a.val>b.val;});
    int gTotal=0, profit=0;
    for(auto x:obiecte){
        if(gTotal+x.g <= greutate){
            profit+=x.val;
            gTotal+=x.g;
        }
        else break;
    }
    cout<<profit;
    fout<<profit;
    return 0;
}