Cod sursa(job #2946948)

Utilizator NiffSniffCojocaru Calin Marcu NiffSniff Data 25 noiembrie 2022 14:17:24
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin ("energii.in");
ofstream cout ("energii.out");
int s, n, k, maxim;
struct gen
{
	int E,C;
};
vector <gen> v(1001);
vector <int> profit;
inline void  citire()
{
	cin >> n >> k;
	for (int i=0; i<n; i++)
	{
		cin >> v[i].E >> v[i].C;
		maxim = max(maxim,v[i].E);
		s += v[i].E;
	}
}
int main ()
{
	citire();
	if (s < k)
	{
		cout << -1;
		return 0;
	}
	s = maxim + k + 1;
	profit.resize(s+1);
	for (int i=0; i<=s; i++)
		profit[i] = -1;
	for (int i=0; i<n; i++)
	{
		for (int j = s - v[i].E; j>=0; j--)
		{
			if (profit[j] != -1)
			{
				if (profit[j+v[i].E] == -1)
				{
					profit[j+v[i].E] = profit[j] + v[i].C;
				}
				else
					profit[j+v[i].E] = min(profit[j+v[i].E], profit[j] + v[i].C);
			}
		}
		if (profit[v[i].E] == -1)
			profit[v[i].E] = v[i].C;
		else
			profit[v[i].E] = min(profit[v[i].E],v[i].C);
	}
	int raspuns = 10e7;
	for (int i = k; i<=s; i++)
	{
		if (profit[i] != -1)
			raspuns = min(raspuns,profit[i]);
	}
	cout << raspuns;
}