Pagini recente » Cod sursa (job #4840) | Cod sursa (job #2963645) | Cod sursa (job #2473846) | Cod sursa (job #165640) | Cod sursa (job #2485046)
#include <iostream>
#include <fstream>
#define MAXL 5007
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int dp[MAXL];
int main() {
int n, w, x, y, z, pos, mine = -1;
fin>>n>>w;
for(int i=0;i<=w;i++)
dp[i] = -1;
for(int i=0;i<n;i++){
fin>>x>>y;
for(int j=w;j>=0;j--)
if(dp[j]){
z = dp[j]+y;
pos = x+j;
if(pos>=w && (mine==-1 || z<mine))
mine = z;
else if(pos<w && (dp[pos]==-1 || z<dp[pos]))
dp[pos] = z;
}
if(x>=w && (mine==-1 || y<mine))
mine = y;
else if(x<w && (dp[x]==-1 || y<dp[x]))
dp[x] = y;
}
fout<<mine;
return 0;
}