import sequtils,strutils,algorithm,math,sugar,macros,strformat proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int = var minus = false while true: var k = getchar_unlocked() if k == '-' : minus = true elif k < '0' or k > '9': break else: result = 10 * result + k.ord - '0'.ord if minus: result *= -1 let n = scan() let Y0 = newSeqWith(n,scan()).sorted(cmp) let Y = Y0.mapIt(it - Y0[0]) var preY = Y[0] var ans = Y.sum() for i,y in Y: let diff = y - preY if diff == 0: continue let w = 2 * i - n if w >= 0 : break ans += diff * w preY = y echo ans