Cod sursa(job #487444)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 25 septembrie 2010 11:21:21
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>

using namespace std;

int g, w, energ;

struct cv
{
    int eg, cg;
    double cost;
}; cv chestie [1005];

void citire()
{
    scanf ("%d ",&g);
    scanf ("%d ",&w);
    for (int i=0; i<g; i++)
        scanf ("%d %d ", &chestie[i].eg, &chestie[i].cg);
}

void ceva()
{
    for (int i=0; i<g; i++)
        chestie[i].cost=(double)chestie[i].eg/(double)chestie[i].cg;
    for (int i=0; i<g-1; i++)
        for (int j=i+1; j<g; j++)
            if (chestie[i].cost<chestie[j].cost)
            {
                cv aux=chestie[i];
                chestie[i]=chestie[j];
                chestie[j]=aux;
            }
    for (int i=0; i<g; i++)
    {
        if (chestie[i].eg<=w)
        {
            w-=chestie[i].eg;
            energ+=chestie[i].cg;
        }
        if (w<=0)
            break;
    }
}

int main()
{
    freopen ("energii.in","r",stdin);
    freopen ("energii.out","w",stdout);
    citire();
    ceva();
    if (energ==0)
        printf ("-1\n");
    else
        printf ("%d\n",energ);
    return 0;
}