Cod sursa(job #1128231)

Utilizator AlexxanderXPrisacariu Alexandru AlexxanderX Data 27 februarie 2014 16:11:31
Problema Problema rucsacului Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>

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

using namespace std;

int a[5001];

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

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

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

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

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

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

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