Cod sursa(job #1605157)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 18 februarie 2016 20:02:53
Problema Energii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<cstdio>
#include<iostream>
using namespace std;
int G,W,cost;
float r[1005],e[1005],c[1005];
int main()
{
    freopen("energii.in","r",stdin);
   freopen("energii.out","w",stdout);
    scanf("%d",&G);
    scanf("%d",&W);
    for (int i=1;i<=G;i++)
    {
        scanf("%f%f",&e[i],&c[i]);
        r[i]=e[i]/c[i];
    }
    bool ok=1;
    int da=G;
    do
    {
        ok=0;
        for (int i=1;i<da;i++)
            if (r[i]<r[i+1])
            {
             ok=1;
             swap(r[i],r[i+1]);
                swap(e[i],e[i+1]);
                swap(c[i],c[i+1]);
            }
        da--;
    }
    while(ok);
    int i=1;
    int P=0;
    while (P<W)
    {
        int x=e[i];
        int y=c[i];
        P+=x;
        cost+=y;
        i++;
    }
    printf("%d\n",cost);

    return 0;
}