n=int(input()) A=sorted(map(int,input().split()),reverse=True) ans=cnt=0;N=n for i in range(17): a=min(pow(2,i),N) for j in range(a): ans+=i*A[cnt] cnt+=1 N-=a if N<=0: break print(ans)