Cod sursa(job #587891)

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

#define ff first
#define ss second
#define mp make_pair
#define pb push_back

typedef long long LL;

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

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

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;
	int pmax = 1;
	LL best = (LL)x[1]*N;
	for (int i=2;i<=N;++i)
	{
		while (front<=back && get_pos(Q[front], i) <= get_pos(pmax, Q[front]) ) ++front;
		if (get_pos(pmax, i) <= N) Q[++back] = i;
		if (front <= back && get_pos(pmax, Q[front]) == i) pmax = Q[front++];
		
		best = max(best, (LL)(N-i+1)*x[i] + (LL)x[pmax]*(i-pmax));
	}
	

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

	return 0;
}