Pagini recente » Borderou de evaluare (job #1633360) | Cod sursa (job #1970225) | Cod sursa (job #1441402) | Cod sursa (job #671786)
Cod sursa(job #671786)
#include<stdio.h>
#include<algorithm>
using namespace std;
int G,W,H,e[100],c[100],i,Cost=0,Wramas,x,s;
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",&e[i]);
s=s+e[i];
scanf("%d",&c[i]);
}
do
{
H=1;
for(i=1;i<=G-1;i++)
if(c[i]*e[i+1]<c[i+1]*e[i])
{
swap(c[i],c[i+1]);
swap(e[i],e[i+1]);
H=0;
}
}while(H==0);
i=G;
Wramas=W;
if(s>=W)
{
while(i>=1&&Wramas!=0)
{ if(e[i]<=Wramas)
{
Wramas=Wramas-e[i];
Cost=Cost+c[i];
i++;
}
else
if(c[i]>=Cost)
{ Cost=c[i];
Wramas=0;
}
}
printf("%d",Cost);
}
else
printf("-1");
return 0;
}