Cod sursa(job #2491134)

Utilizator TzigCurta Tudor Tzig Data 11 noiembrie 2019 20:56:13
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>

using namespace std;

struct obiect{
    int greutate;
    int profit;
};

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

void sortare(obiect x[],int n)
{
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            if(x[i].profit<x[j].profit){
                obiect aux=x[i];
                x[i]=x[j];
                x[j]=aux;
            }else{
                if(x[i].profit==x[j].profit){
                    if(x[i].greutate>x[j].greutate){
                        obiect aux=x[i];
                        x[i]=x[j];
                        x[j]=aux;
                    }
                }
            }
        }
    }
}

int main()
{
    int n,g;
    f>>n>>g;
    obiect x[5010];
    for(int i=1;i<=n;i++){
        f>>x[i].greutate;
        f>>x[i].profit;
    }
    sortare(x,n);
    int rez=0;
    for(int i=1;i<=n && g>0;i++){
        if(g>=x[i].greutate){
            rez=rez+x[i].profit;
            g=g-x[i].greutate;
        }
    }
    out<<rez;
    return 0;
}