Pagini recente » Cod sursa (job #671786) | Cod sursa (job #7822) | Cod sursa (job #863823) | Cod sursa (job #3222739) | Cod sursa (job #705275)
Cod sursa(job #705275)
#include<stdio.h>
#include<algorithm>
using namespace std;
int En,E[100000],C[100000],ok,s,i,W,G;
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]);
}
if(s<W)
printf("-1");
else
{
do
{
ok=1;
for(i=1;i<G;i++)
if(C[i]>C[i+1]&&E[i]<E[i+1])
{swap(C[i],C[i+1]);
swap(E[i],E[i+1]);ok=0;
}
}while(ok==0);
while(En<=W&&i<=G)
{
En+=E[i];
i++;
}
if(En<W&&i<G)
{En+=E[++i];
}
printf("%d",En);
}
return 0;
}