Pagini recente » Cod sursa (job #2467373) | Cod sursa (job #2958613) | Cod sursa (job #299817) | Cod sursa (job #3288415) | Cod sursa (job #587915)
Cod sursa(job #587915)
#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;
}