Cod sursa(job #645205)

Utilizator MciprianMMciprianM MciprianM Data 8 decembrie 2011 20:30:20
Problema Avioane Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>

int n, maxPayment [100000];

#if RAND_MAX < 100000
inline int myRand () {
	int randomNr = rand () % n;
	if (randomNr & 1) {
		randomNr *= (rand () % 10000);
		randomNr = randomNr % n;
	}
	return randomNr;
}
#else
inline int myRand () {
	int randomNr = rand () % n;
	return randomNr;
}
#endif

int main () {
	int i;
	srand (time(0));
	freopen ("avioane.in", "rt", stdin);
	freopen ("avioane.out", "wt", stdout);
	scanf ("%d", &n);
	for (i = 0; i < n; ++ i) {
		scanf ("%d", &maxPayment [i]);
	}
	std::sort (maxPayment, maxPayment + n);
	long long maxProfit = -1;
	for (i = 0; i < 1500000; ++ i) {
		int pos1 = myRand ();
		int pos2 = myRand ();
		if (pos1 > pos2) {
			std :: swap (pos1, pos2);
		}
		maxProfit = std::max (maxProfit, (n - pos2) * 1ll * maxPayment [pos2] + (pos2 - pos1) * 1ll * maxPayment [pos1]);
	}
	printf ("%lld", maxProfit);
	return 0;
}