for i, x in enumerate(a):
    r = rank[x]
num_smaller = counts.read_prefix(r)
sum_smaller = sums.read_prefix(r)
res.append(total_sum - 2 * sum_smaller + x * (2 * num_smaller - i))
counts.update_point(r, 1)
sums.update_point(r, x)
total_sum += x
return res
print(solve([3, 5, 6, 7, 1]))
print(solve([2, 0, 1]))
sum of absolute differences of a number in an array