Cod sursa(job #2532620)

Utilizator mariaghinescu22Ghinescu Maria mariaghinescu22 Data 28 ianuarie 2020 00:11:22
Problema Energii Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 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[M][M], amount[N], cost[N];

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