Cod sursa(job #587915)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 6 mai 2011 14:09:40
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long LL;

const int NMAX = 100005;
const int Inf = 0x3f3f3f3f;

int N, x[NMAX], front, back, Q[NMAX];

inline int get_pos(int i, int j) //pt i<j(x[i]<=x[j]) cand o sa-l depaseasca j pe i
{
	if (x[i] == x[j]) return Inf;
	LL pos = ((LL)j*x[j] - (LL)i*x[i])/(x[j]-x[i]);
	if (pos > Inf) pos = Inf;
	return (int)pos;
}

int main()
{
	ifstream fin("avioane.in");
	fin>>N;
	for (int i=1;i<=N;++i) fin>>x[i];
	fin.close();

	sort(x+1, x+N+1);
	
	front = 0; back = -1;
	Q[++back] = 1;
	LL best = (LL)x[1]*N;
	for (int i=2;i<=N;++i)
	{
		while (back - front >= 1 && get_pos(Q[back], i) <= get_pos(Q[back-1], Q[back])) --back;
		Q[++back] = i;
		if (back - front >= 1 && get_pos(Q[front], Q[front+1]) == i) ++front;
		
		best = max(best, (LL)(N-i+1)*x[i] + (LL)x[Q[front]]*(i-Q[front]));
	}
	

	ofstream fout("avioane.out");
	fout<<best<<"\n";
	fout.close();

	return 0;
}