Pagini recente » Cod sursa (job #355542) | Cod sursa (job #2354418) | Cod sursa (job #1482745) | Cod sursa (job #1836871) | Cod sursa (job #19862)
Cod sursa(job #19862)
#include <fstream.h>
long a[100000];
int main()
{
int n,w,x,y,i,j;
long se,sc,min=-1,max=0;
ifstream f("energii.in");
ofstream g("energii.out");
f>>n>>w;
for (i=1;i<=n;i++)
{
f>>x>>y;
for (j=max;j>=0;j--)
{
if (a[j]!=0)
{
se=j+x;
sc=a[j]+y;
if (se>=w && (min>sc || min==-1))
min=sc;
else
{
if (a[se]==0 || (a[se]!=0 && a[se]>a[j]+y))
a[se]=a[j]+y;
}
}
}
if (x>=w && (min>y || min==-1))
min=y;
else
if (a[x]==0 || (a[x]!=0 && a[x]>y))
a[x]=y;
if (max+x>5001)
max=5001;
else
max=max+x;
}
g<<min<<"\n";
f.close();
g.close();
return 0;
}