Pagini recente » Cod sursa (job #79411) | Cod sursa (job #22057)
Cod sursa(job #22057)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct gen{
int e,c;
};
int comp(const void*a,const void*b){
gen *aa=(gen*)a,*bb=(gen*)b;
gen x=*aa,y=*bb;
if((double)x.c/x.e>(double)y.c/y.e)
return 1;
if((double)x.c/x.e<(double)y.c/y.e)
return -1;
return 0;
}
int main(){
FILE*in=fopen("energii.in","r");
FILE*out=fopen("energii.out","w");
int g,w,i,s=0,cost=0;
gen x[1001];
fscanf(in,"%d %d",&g,&w);
for(i=0;i<g;i++)
fscanf(in,"%d %d",&x[i].e,&x[i].c);
qsort(x,g,sizeof(x[0]),comp);
i=0;
while((s<w)&&(i<g)){
s=s+x[i].e;
cost=cost+x[i].c;
i++;
}
if(s<w)
fprintf(out,"-1\n");
if(s>=w)
fprintf(out,"%d\n",cost);
fclose(in);
fclose(out);
return 0;
}