Cod sursa(job #2926028)

Utilizator Ser.MariusSerban Marius Aurelian Ser.Marius Data 16 octombrie 2022 18:56:12
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
using namespace std;

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

const int maxn=10001;
int n,G;


struct detalii{
    int greutate,profit;
};

void sortare(detalii V[])
{
    for(int i=0;i<n-1;i++)
        for(int j=i;j<n;j++)
            if(V[i].profit<V[j].profit)
            {
                swap(V[i].profit,V[j].profit);
                swap(V[i].greutate,V[j].greutate);
            }
}

void selectare(detalii V[])
{
    int i=0;
    int P=0;
    while(G-V[i].greutate>=0)
    {
        G-=V[i].greutate;
        P+=V[i].profit;
        i++;
       /// g<<V[i].profit<< " " << V[i].greutate <<'\n';
    }
    g<<P;
}
void citire(detalii V[])
{
    f>>n>>G;
    for(int i=0;i<n;i++)
        f>> V[i].greutate >> V[i].profit;
}
int main()
{
    detalii V[maxn];
    citire(V);
    sortare(V);
    selectare(V);
    f.close();
    g.close();
    return 0;
}