Pagini recente » Cod sursa (job #1317749) | Cod sursa (job #1359863) | Cod sursa (job #2098498) | Cod sursa (job #2983859) | Cod sursa (job #649390)
Cod sursa(job #649390)
#include <stdio.h>
#include <algorithm>
#define NMAX 100005
#define ll long long
using namespace std;
int n,A[NMAX];
ll B[NMAX],act,rez;
void rezolva(int dr)
{
int i,step,st=0;
ll val1,val2;
for (step=1; step<=dr; step<<=1);
for (i=0; step; step>>=1)
{
val1=val2=0;
if (st+step<=dr)
val1=(ll)A[st+step]*(dr-st-step+1);
if (st-step>0)
val2=(ll)A[st-step]*(dr-st+step+1);
if (val1>val2 && B[dr]<val1)
B[dr]=val1,st+=step;
if (val2>val1 && B[dr]<val2)
B[dr]=val2,st-=step;
}
}
int main()
{
freopen("avioane.in","r",stdin);
freopen("avioane.out","w",stdout);
scanf("%d",&n);
int i;
for (i=1; i<=n; i++)
scanf("%d",&A[i]);
sort(A+1,A+n+1);
for (i=1; i<=n; i++)
rezolva(i);
for (i=1; i<=n; i++)
{
act=B[i-1]+(ll)A[i]*(n-i+1);
if (act>rez)
rez=act;
}
printf("%lld\n",rez);
return 0;
}