Pagini recente » Cod sursa (job #10586) | Cod sursa (job #1004425) | Cod sursa (job #1525527) | Cod sursa (job #2733417) | Cod sursa (job #1521160)
#include <cstdio>
#define MAXN 100000
int v[MAXN],poz[MAXN],n;
void myqsort(int begin,int end){
int b=begin,e=end,aux,pivot=v[(b+e)/2];
while(b<=e){
while(v[b]<pivot) b++;
while(v[e]>pivot) e--;
if(b<=e){
aux=v[b];
v[b]=v[e];
v[e]=aux;
b++;e--;
}
}
if(begin<e) myqsort(begin,e);
if(b<end) myqsort(b,end);
}
void cauta(int st,int dr){
if(st<=dr){
int max=0,j=-1,i;
for(i=poz[st-1];i<=poz[dr+1]&&i<=(st+dr)/2;i++)
if(max<((st+dr)/2-i)*v[i]+(n-(st+dr)/2)*v[(st+dr)/2]){
max=((st+dr)/2-i)*v[i]+(n-(st+dr)/2)*v[(st+dr)/2];
j=i;
}
if(j>-1)
poz[(st+dr)/2]=j;
cauta(st,(st+dr)/2-1);
cauta((st+dr)/2+1,dr);
}
}
int main(){
FILE*fi,*fout;
int i,j,s,max;
fi=fopen("avioane.in" ,"r");
fout=fopen("avioane.out" ,"w");
fscanf(fi,"%d" ,&n);
for(i=1;i<=n;i++)
fscanf(fi,"%d" ,&v[i]);
myqsort(1,n);
poz[0]=1;
poz[n+1]=n;
cauta(1,n);
max=0;
for(i=1;i<=n;i++)
if(max<(i-poz[i])*v[poz[i]]+(n-i+1)*v[i])
max=(i-poz[i])*v[poz[i]]+(n-i+1)*v[i];
fprintf(fout,"%d" ,max);
fclose(fi);
fclose(fout);
return 0;
}