Pagini recente » Cod sursa (job #13220) | Cod sursa (job #2757283) | Cod sursa (job #1183961) | Cod sursa (job #1062352) | Cod sursa (job #1717386)
#include<bits/stdc++.h>
using namespace std;
int i,n,a[100005];
long long rs,maybe[100005];
void Solve(int left,int right) {
int pivot=(left+right)/2;
maybe[pivot]=-1e18;
for(int i=max(left-(right-left)/2,1);i<=pivot;++i) maybe[pivot]=max(maybe[pivot],1LL*(pivot-i+1)*a[i]);
if(left<pivot) Solve(left,pivot-1);
if(right>pivot) Solve(pivot+1,right);
}
int main()
{
ifstream cin("avioane.in");
ofstream cout("avioane.out");
ios_base::sync_with_stdio(0);
cin>>n;
for(i=1;i<=n;++i) cin>>a[i];
sort(a+1,a+n+1);
Solve(1,n);
for(i=0;i<=n;++i) rs=max(rs,maybe[i]+1LL*(n-i)*a[i+1]);
cout<<rs<<'\n';
return 0;
}