Cod sursa(job #2532624)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 28 ianuarie 2020 00:33:57
Problema Energii Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int N = 5002;
const long long M = 10002;
int n, total_amount;
long long dp[N][M], amount[N], cost[N];

int main() {
	in >> n >> total_amount;
	for (int i = 1; i <= n; i++) 
		in >> amount[i] >> cost[i];

	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= total_amount; j++)
			dp[i][j] = 5 * 1e8;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= total_amount; j++) {
			if (amount[i] < j) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - amount[i]] + cost[i]);
			else
				dp[i][j] = min(dp[i - 1][j], cost[i]);
		}
	}
	if (dp[n][total_amount] == 5 * 1e8) out << "-1";
	else
		out << dp[n][total_amount];
	return 0;
}