#include #include #include #include #include #include #include #include #include #include #include #include #define vll vector #define vvvl vector #define vvl vector> #define VV(a, b, c, d) vector>(a, vector(b, c)) #define VVV(a, b, c, d) vector(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c dat; BIT(ll N){ while(N>M) M*=2; dat.resize(M*2-1, 0); } void update(ll x, ll k){ for(int i=k+1;i<=M;i+=(i&(-i))){ dat[i] += x; } } ll sum(ll r){ ll ret = 0; for(int k=r;k>0;k-=(k&(-k))) ret += dat[k]; return ret; } ll query(ll l, ll r){ return sum(r) - sum(l); } }; int main(int argc, char const *argv[]) { ll n;std::cin >> n; vll a(n);get(a); sort(all(a)); BIT seg(n); re(i, n) seg.update(a[i], i); ll ans = -100000000000000000; for(ll c=0;c1){ ll mid = (l+r)/2; if(a[c-mid]+a[n-mid]>=2*a[c]) l = mid; else r = mid; } //std::cout << "now" << c << " " << l << '\n'; //std::cout << seg.query(c-l, c) << " " << seg.query(n-l, n) << " " << a[c]*2*l << '\n'; ans = max(ans, seg.query(c-l, c) + seg.query(n-l, n) - a[c] * 2 * l); } std::cout << ans << '\n'; }