Cod sursa(job #2262271)

Utilizator Robert_VRVRobert Vadastreanu Robert_VRV Data 17 octombrie 2018 09:38:59
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <algorithm>
#include <stdio.h>

const int MAX_G = 15000;

int dp[1 + MAX_G];

struct Object {
  int p;
  int g;
};

int main() {

  freopen("energii.in", "r", stdin);
  freopen("energii.out", "w", stdout);

  int n, g;
  scanf("%d%d", &n, &g);
  Object objects[1 + n];
  for (int i = 1; i <= n; i++)
	scanf("%d%d", &objects[i].g, &objects[i].p);
  for (int j = 1; j <= MAX_G; j++)
	dp[j] = 2e9;
  for (int i = 0; i <= g; i++)
    for (int j = 1; j <= n; j++)
	  if (dp[i] + objects[j].p < dp[i + objects[j].g])
		dp[i + objects[j].g] = dp[i] + objects[j].p;

  int ans = 2e9;
  for (int i = g; i <= MAX_G; i++)
    ans = std::min(ans, dp[i]);
  if (ans == 2e9)
	ans = -1;
  printf("%d\n", ans );

  fclose(stdin);
  fclose(stdout);

  return 0;
}