Pagini recente » Cod sursa (job #2509505) | Cod sursa (job #3132057) | Cod sursa (job #1989990) | Cod sursa (job #2108142) | Cod sursa (job #1132830)
#include <stdio.h>
#include<algorithm>
int g,w,i,aux,pos,sum,sum1,flag;
int main()
{
FILE*f1,*f2;
f1=fopen("energii.in","r");
f2=fopen("energii.out","w");
fscanf(f1,"%d %d",&g,&w);
int mat[g][2];
float v[g],aux2,m;
for(i=0;i<g;i++)fscanf(f1,"%d %d",&mat[i][0],&mat[i][1]);
for(i=0;i<g;i++)v[i]=(float)mat[i][1]/mat[i][0];
for(i=0;i<g;i++)
{
m=v[i];
for(int j=i+1;j<g;j++)
if(v[j]<m)
{
m=v[j];
pos=j;
}
aux=v[i];
v[i]=v[pos];
v[pos]=aux;
aux2=mat[i][0];
mat[i][0]=mat[pos][0];
mat[pos][0]=aux2;
aux2=mat[i][1];
mat[i][1]=mat[pos][1];
mat[pos][1]=aux2;
}
i=0;
while(sum<w&&!flag)
{
sum+=mat[i][0];
sum1+=mat[i][1];
i++;
if(i==g)flag=1;
}
if(flag)fprintf(f2,"-1");
else fprintf(f2,"%d",sum1);
return 0;
}