Cod sursa(job #1673161)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 3 aprilie 2016 15:07:15
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <cstdio>
using namespace std;
int g,w,i,j,s,max1,cost,c[1001],e[1001];
int main()
{
    freopen("energii.in","r",stdin);
    freopen("energii.out","w",stdout);
    scanf("%d",&g);
    scanf("%d",&w);
    for(i=1;i<=g;i++)
    {
        scanf("%d %d",&e[i],&c[i]);
        s+=e[i];
    }
    if(s<w)
        printf("-1");
    else
    {
        s=0;
        while(s<w)
        {
            max1=-1;
            for(i=1;i<=g;i++)
                if(e[i]>max1)
                {
                    max1=e[i];
                    j=i;
                }
                else
                    if(e[i]==max1 && c[i]<c[j])
                        j=i;
            s+=max1;
            cost+=c[j];
            e[j]=-1;
        }
        printf("%d",cost);
    }
    return 0;
}