Cod sursa(job #2936390)

Utilizator lucaxsofLuca Sofronie lucaxsof Data 8 noiembrie 2022 19:17:37
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;

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

const int maxx = 5003;

const int INF = 2e9;

long long cost[maxx], e[maxx], c[maxx];

int n, necesar;

void read()
{
	fin >> n >> necesar;
	for (int i = 1; i <= n; ++i)
	{
		fin >> e[i] >> c[i];
	}
}

void fn()
{
	for (int i = 1; i <= necesar; ++i)	cost[i] = INF;

	for (int i = 1; i <= n; ++i)
		for (int j = necesar; j >= 0; --j)
			{
				int j_nou = j + e[i];
				j_nou = min(j_nou, necesar);	//in cazul in care trece peste 'necesar';
				if (cost[j_nou] > cost[j] + c[i])
				cost[j_nou] = cost[j] + c[i];
			}

	if (cost[necesar] != INF)
		fout << cost[necesar];
	else
		fout << -1;
}

int main()
{
	read();
	fn();
}