Cod sursa(job #2214466)

Utilizator PetyAlexandru Peticaru Pety Data 19 iunie 2018 10:12:07
Problema Energii Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("energii.in");
ofstream fout ("energii.out");

int dp[5001], e[1001], c[1001], n, w;

int main()
{
  fin >> n >> w;
  for (int i = 1; i <= w; i++)
    dp[i] = 1000000;
  for (int i = 1; i <= n; i++)
    fin >> e[i] >> c[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 + e[i] < w) {
          dp[j + e[i]] = min(dp[j + e[i]], dp[j] + c[i]);
        }
        else
          sol = min(sol, dp[j] + c[i]);
      }
  if (sol == INT_MAX)
    fout << -1;
  else
    fout << sol - 1;
  return 0;
}