Pagini recente » Cod sursa (job #2006865) | Cod sursa (job #1923046) | Cod sursa (job #625844) | Cod sursa (job #2500114) | Cod sursa (job #2220406)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("energii.in");
ofstream fout ("energii.out");
int n,w,Energy[1001],Cost[1001],Dp[5001];// n is the number of generators and w is the energy needed
int main ()
{
fin >> n >> w;
for (int i = 1; i <= n; i++)
fin >> Energy[i] >> Cost[i];
int sol = INT_MAX;
Dp[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = w; j >= 0; j--)
if (Dp[j])
if (j + Energy[i] < w)
if (Dp[j + Energy[i]]) Dp [j + Energy[i]] = min (Dp[j + Energy[i]] , Dp[j] + Cost[i]);
else Dp[j + Energy[i]] = Dp[i] + Cost[i];
else sol = min (sol , Dp[j] + Cost[i]);
if (sol == INT_MAX) fout << "-1";
else fout << sol - 1;
return 0;
}