Cod sursa(job #426490)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 27 martie 2010 08:32:45
Problema Energii Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
//energii - infoarena
#include<fstream.h>
 ifstream f("energii.in");
 ofstream g("energii.out");
    int main(){
        double max,v[1001];
        int ok=1,p1,p2,a1[1001],a2[1001],i,j,n,ene,p;
        long bani=0;
        f>>n>>ene;
        for(i=1;i<=n;++i){
            f>>a1[i]>>a2[i];
            v[i]=(double)a1[i]/a2[i];
        }
        while(ene>0 && ok==1){
            max=0;
            ok=0;
            for(i=1;i<=n;++i){
                if(max<v[i]){
                    ok=1;
                    max=v[i];
                    p1=a1[i];
                    p2=a2[i];
                    p=i;
                }  
            }
            v[p]=0;
            ene-=p1;
            bani+=p2;
        }
        if(ok==0) g<<-1;
        else
        g<<bani;
        g.close();
        return 0;
    }