def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    cnt = 0
    while n > 1:
        a_max = max(a[:n])
        a_max_i = a.index(a_max)
        if a_max_i != n-1:
            cnt += (n - a_max_i) * a_max - sum(a[a_max_i:n])
        n = a_max_i
    print(cnt)

if __name__ == '__main__':
    main()