Cod sursa(job #469547)
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int a[1002],b[1002];
int main ()
{
int g,w;
long long min=2000000000;
ifstream fin ("energii.in" );
ofstream fout("energii.out");
fin>>g>>w;
for (int i=1;i<=g;i++) fin>>a[i]>>b[i];
a[g+1]=-1;
for (int i=1;i<=g-1;i++)
for (int j=i+1;j<=g;j++)
if (a[i]>a[j])
{
int aux;
aux=a[i];
a[i]=a[j];
a[j]=aux;
aux=b[i];
b[i]=b[j];
b[j]=aux;
}
int s;
for (int i=1;i<=g;i++)
{
int j=i;s=0;
int cost=0;
while (s<w && a[j]!=0)
{
s+=a[j];
cost+=b[j];
j++;
}
if(cost<min) min=cost;
if(a[j]==0) {
min=-1;
break;
}
}
fout<<min;
return 0;
}