local mmi, mma = math.min, math.max local mab = math.abs local ylim = 10001 local n = io.read("*n") local a = {} for i = 1, n do a[i] = 1 + io.read("*n") end local dp1, dp2 = {}, {} for i = 1, ylim do dp1[i] = mab(a[1] - i) end local inf = 1000000007 for i = 2, n do local src = i % 2 == 0 and dp1 or dp2 local dst = i % 2 == 0 and dp2 or dp1 local curmin = inf for j = 1, ylim do curmin = mmi(curmin, src[j]) dst[j] = curmin + mab(a[i] - j) end end local tbl = n % 2 == 0 and dp2 or dp1 local ret = tbl[1] for i = 2, ylim do ret = mmi(ret, tbl[i]) end print(ret)