Cod sursa(job #14650)
#include<stdio.h>
struct nod{ int e, c; } v[1024];
int n, w, cost[5020];
void solve()
{
for(int i=1; i<=n; ++i) {
for(int j=w-1; j>=0; --j) if( cost[j]!=-1)
if( cost[j]+ v[i].c < cost[j+v[i].e] || cost[j+v[i].e]==-1) {
if( j+v[i].e > w) cost[ w+1]= cost[j]+v[i].c;
else cost[j + v[i].e]= cost[j]+ v[i].c;
}
}
}
int main()
{
freopen("energii.in","r",stdin);
scanf("%d\n%d",&n,&w);
for(int i=1; i<=n; ++i) scanf("%d %d",&v[i].e, &v[i].c);
for(int i=1; i<=5010; ++i) cost[i]=-1;
solve();
freopen("energii.out","w",stdout);
if( cost[w]==-1) {
if( cost[w+1]==-1) printf("-1\n");
else printf("%d\n",cost[w+1]);
}
else {
if( cost[w+1]==-1) printf("%d\n",cost[w]);
else {
if( cost[w+1] > cost[w]) printf("%d\n",cost[w]);
else printf("%d\n",cost[w+1]);
}
}
return 0;
}