Pagini recente » Cod sursa (job #2446923) | Cod sursa (job #529398) | Cod sursa (job #501474) | Cod sursa (job #1784125) | Cod sursa (job #2502882)
#include <bits/stdc++.h>
using namespace std;
int n;
long long int ans;
int v[100009];
int caut (int st, int dr, int poz)
{
long long int sum=-1;
int nr=0;
for (int i=st; i<=dr && i<=poz; ++i)
{
if (sum<(poz-i)*v[i])
{
sum=(poz-i)*v[i];
nr=i;
}
}
return nr;
}
void solve (int st, int dr, int dst, int ddr)
{
if (st>dr)
{
return;
}
int mij=(st+dr)/2;
int temp=caut(dst,ddr,mij);
ans=max(ans,1LL*(mij-temp)*v[temp]+1LL*(n-mij+1)*v[mij]);
solve(st,mij-1,dst,temp);
solve(mij+1,dr,temp,ddr);
}
int main()
{
ifstream fin ("avioane.in");
ofstream fout ("avioane.out");
fin>>n;
for (int i=1; i<=n; ++i)
{
fin>>v[i];
}
sort(v+1,v+n+1);
ans=-1;
solve(1,n,1,n);
fout<<ans<<"\n";
return 0;
}