Cod sursa(job #3243553)

Utilizator Commander_XDunel Stefan-Octavian Commander_X Data 19 septembrie 2024 13:59:58
Problema Energii Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
//https://www.infoarena.ro/problema/energii
#include <fstream>
#include <vector>

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

using namespace std;

vector<int> dp, e, c;

int minim(int a, int b)
{
	return a < b ? a : b;
}
int main()
{
	int n, W;
	fin >> n >> W;
	e.resize(n);
	c.resize(n);
	for (int i = 0; i < n; ++i)
		fin >> e[i] >> c[i];

	dp.resize(W + 1, 2e9);
	dp[0] = 0;

	for (int i = 0; i < n; ++i)
	{
		for (int j = W; j > 0; --j)
			if (dp[j] != 0 && j + e[i] <= W)
				dp[j + e[i]] = minim(dp[j + e[i]], dp[j] + c[i]);
			else
				if (dp[j] != 0)
					dp[W] = minim(dp[W], dp[j] + c[i]);
		if (e[i] <= W)
			dp[e[i]] = minim(dp[e[i]], c[i]);
		else
			dp[W] = minim(dp[W], c[i]);
	}

	fout << dp[W];
}