Pagini recente » Cod sursa (job #98266) | Cod sursa (job #429354) | Cod sursa (job #1302117) | Cod sursa (job #2239861) | Cod sursa (job #585799)
Cod sursa(job #585799)
#include <fstream>
#include <algorithm>
using namespace std;
const char InFile[]="avioane.in";
const char OutFile[]="avioane.out";
const int MaxN=100111;
ifstream fin(InFile);
ofstream fout(OutFile);
int N,v[MaxN],psolind;
long long sol,psol=-1,best[MaxN];
inline long long calc(int st, int dr)
{
return (long long)(N-dr+1)*(long long)(v[dr])+(long long)(dr-st)*(long long)(v[st]);
}
inline int mymin(const int &a, const int &b)
{
if(a<b)
{
return a;
}
return b;
}
inline int mymax(const int &a, const int &b)
{
if(a>b)
{
return a;
}
return b;
}
int main()
{
fin>>N;
for(register int i=1;i<=N;++i)
{
fin>>v[i];
}
fin.close();
sort(v+1,v+1+N);
for(register int i=1;i<=N;++i)
{
best[i]=(long long)((N-i+1))*(long long)(v[i]);
if(psol<=best[i])
{
psol=best[i];
psolind=i;
}
}
int ind;
for(register int i=1;i<=N;++i)
{
long long c=calc(mymin(psolind,i),mymax(psolind,i));
if(sol<c)
{
sol=c;
ind=i;
}
}
int st,dr;
st=min(psolind,ind);
dr=max(psolind,ind);
bool ok=true;
while((st>=1 || dr<=N) && ok)
{
ok=false;
long long c=calc(st-1,dr);
while(c>sol && st>=1)
{
ok=true;
sol=c;
--st;
c=calc(st-1,dr);
}
c=calc(st,dr+1);
while(c>sol && dr<=N)
{
ok=true;
sol=c;
++dr;
c=calc(st,dr+1);
}
}
fout<<sol;
fout.close();
return 0;
}