Cod sursa(job #1717844)

Utilizator TwoOfDiamondsDaniel Alexandru Radu TwoOfDiamonds Data 15 iunie 2016 22:00:16
Problema Problema rucsacului Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>

using namespace std;

struct obiect
{
	int greutate, valoare;
};

int main()
{
	ifstream in("rucsac.in");
	
	int n, g; in >> n >> g;

	int tabel[2][10001];

	for (int i = 0; i <= g; i++)
		tabel[0][i] = -1;

	for (int i = 0; i < n; i++)
	{
		int gr, val; in >> gr >> val;

		if (tabel[0][gr] < val)
			tabel[1][gr] = val;
		else
			tabel[1][gr] = tabel[0][gr];

		for (int j = 0; j <= g; j++)
		{
			if (j - gr > 0 && tabel[0][j - gr] != -1)
			{
				if (tabel[0][j] < tabel[0][j - gr] + val)
					tabel[1][j] = tabel[0][j - gr] + val;
				else
					tabel[1][j] = tabel[0][j];
			}
		}

		for (int j = 0; j <= g; j++)
		{
			tabel[0][j] = tabel[1][j];
		}
	}

	int max = -1;

	for (int i = 0; i <= g; i++)
	{
		if (tabel[0][i] > max)
			max = tabel[0][i];
	}

	ofstream out("rucsac.out");
	out << max << endl;
	return 0;
	
}