Cod sursa(job #1128564)

Utilizator AlexxanderXPrisacariu Alexandru AlexxanderX Data 27 februarie 2014 17:39:46
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>

#define DEBUG 0
#if DEBUG
	#include <iostream>
	#include <iomanip>
#endif

using namespace std;

int a[5001];

int main()
{
	ifstream in("energii.in");
	ofstream out("energii.out");

	int n, total;
	in >> n >> total;

	int g, p;
	in >> g >> p;
	for (int j=g; j>0; --j)
	{
		a[j] = p;
	}

	for (int i=total; i>g; --i)
	{
		a[i] = 10001;
	}

	for (int i=0; i<n-1; ++i)
	{
		in >> g >> p;

		if (g < total)
		{
			int s = (g>=total/2) ? total-g : g;
			for (int j=1; j<=s; ++j)
			{
				if (a[j]+p < a[j+g])
				{
					a[j+g] = a[j] + p;
				}
			}
		}

		for (int j=g; j>=1; --j)
		{
			if (a[j]>p)
			{
				a[j] = p;
			}
		}

		#if DEBUG
			for (int j=1; j<=total; ++j)
			{
				cout << setw(5) << j << " ";
			}
			cout << " #" << g << " " << p << "\n";

			for (int j=1; j<=total; ++j)
			{
				cout << setw(5) << a[j] << " ";
			}
			cout << "\n\n";
		#endif
	}

	#if DEBUG
		cout << a[total] << "\n";
	#endif
	out << a[total];
}