Cod sursa(job #2691455)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 28 decembrie 2020 18:45:00
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
using namespace std;

ifstream in("rucsac.in");
ofstream out("rucsac.out");

struct ex{
    int profit, greutate;
}v[5005];

int n, g, sum, gr;

struct cmp{
    bool operator()(ex a, ex b){
        return a.profit < b.profit;
    }
};

int main(){
    in>>n>>g;
    for(int i = 1; i <= n; i++)
        in>>v[i].greutate>>v[i].profit;
    sort(v + 1, v + n + 1, cmp());
    int it = n;
    while(gr < g && it >= 1){
        sum += v[it].profit;
        gr += v[it--].greutate;
    }
    out<<sum;

}