Pagini recente » Cod sursa (job #167446) | Cod sursa (job #1847973) | Cod sursa (job #931325) | Cod sursa (job #631961) | Cod sursa (job #3263941)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("energii.in");
ofstream g ("energii.out");
const int NMAX = 1e3;
const int WMAX = 5e3;
const int VMAX = 1e9;
int dp[2][WMAX + 1];
int main()
{
for(int i=0; i<2; i++)
for(int j=0; j<=WMAX; j++)
dp[i][j] = VMAX;
int n, w;
f >> n >> w;
dp[0][0] = 0;
int costmin = 2e9;
for(int i=1; i<=n; i++){
int x, y;
f >> x >> y;
for(int j=0; j<w; j++){
if(dp[(i + 1) % 2][j] != 2e9){
int cost = dp[(i + 1) % 2][j] + y;
int pret = j + x;
if(pret >= w)
costmin = min(costmin, cost);
else{
dp[i % 2][j + x] = min(dp[(i + 1) % 2][j + x], dp[(i + 1) % 2][j] + y);
}
}
}
}
if(costmin == 2e9)
g << -1;
else g << costmin;
return 0;
}