Cod sursa(job #1139882)

Utilizator GrandmasterSoucup Bogdan Grandmaster Data 11 martie 2014 16:19:31
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include<fstream>
using namespace std;
const int MAX = 10004;
int x[MAX], y[MAX];
int main()
{
	int G, W, i, j, sumX = 0, sumY = 0, suma = 0, max = 0;
	ifstream g("energii.in");
	ofstream f("energii.out");
	g >> G >> W;
	for (i = 0; i < G; i++)
	{
		g >> x[i];
		g >> y[i];
		suma += x[i];
		if (y[i] > max)
			max = y[i];
	}
	if (suma < W)
		f << "-1";
	else
	{
		for (i = 0; i < G; i++)
		{
			for (j = i + 1; j < G; j++)
			{
				if (x[i] < x[j])
				{
					swap(x[i], x[j]);
					swap(y[i], y[j]);
				}
			}
		}
		sumX = x[0];
		sumY = y[0];
		for (i = G - 1; i >= 0; i--)
		{
			if (sumX + x[i] <= W)
			{
				sumX += x[i];
				sumY += y[i];
			}
		}
		f << sumY;
	}
}