Pagini recente » Cod sursa (job #2159798) | Cod sursa (job #423447) | Cod sursa (job #503030) | Cod sursa (job #2369688) | Cod sursa (job #1903002)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("energii.in");
ofstream fout("energii.out");
struct energie{
int cant,cost;
}a[10001];
int n,i,j,g,w,s,m,stot,costt;
bool cmp(energie a, energie b)
{
if(a.cant!=b.cant)
return a.cant<b.cant;
else
return a.cost<b.cost;
}
int main()
{
f>>g>>w;
for(i=1;i<=g;i++)
{f>>a[i].cant>>a[i].cost;
stot+=a[i].cant;
costt+=a[i].cost;
}
if(stot<w)
fout<<"-1";
else
if(stot==w)
fout<<costt;
else
{
m=g;
sort(a+1,a+g+1,cmp);
while(a[g].cant<=w)
{ w-=a[g].cant;
g--;
}
for(i=g+1;i<=m;i++)
s+=a[i].cost;
if(w!=0)
{i=1;
while(w>=0)
{
w-=a[i].cant;
i++;
}
for(j=1;j<=i-1;j++)
s+=a[j].cost;
}
fout<<s;
}
return 0;
}