ll c,d,v;{ll@n;VI@a(n);rsortV(a);rep(i,17){v=min(pow(2,i),n-=v);rep(v)c+=i*a[d++];}wt(c);}