Pagini recente » Cod sursa (job #1911546) | Cod sursa (job #1258979) | Cod sursa (job #2235670) | Cod sursa (job #1233053) | Cod sursa (job #587585)
Cod sursa(job #587585)
#include <stdio.h>
#include <fstream>
#include <algorithm>
#define Nmax 100002
#define LL long long
using namespace std;
ofstream fout("avioane.out");
int N;
LL a[Nmax], St[Nmax], Valk[Nmax];
int main(){
int i,j,nst,k,top; LL sol=0;
freopen("avioane.in","r",stdin);
scanf("%d",&N);
for(i=1;i<=N;++i) scanf("%d",&a[i]);
sort(a+1,a+N+1);
nst=0;
for(i=1;i<=N;++i){
top=St[nst];
if( a[i] != a[top] ) k=(a[i]*i-a[top]*top)/(a[i]-a[top]) +1;
else k=N+2;
while( nst && k<=Valk[nst] ){
nst--;
top=St[nst];
if( a[i] != a[top] ) k=(a[i]*i-a[top]*top)/(a[i]-a[top]) +1;
else k=N+2;
}
if( k<=N )
St[++nst]=i, Valk[nst]=k;
}
Valk[nst+1]=N+1; k=1;
for(i=1;i<=nst;++i){
for( ; k<Valk[i+1]; ++k)
if( a[St[i]]*(k-St[i]) + a[k]*(N-k+1) > sol )
sol = a[St[i]]*(k-St[i]) + a[k]*(N-k+1);
}
fout<<sol<<"\n";
fclose(stdin); fout.close();
return 0;
}