def main():
    N = map(int, input().split())
    A = list(map(int, input().split()))[::-1]
    cont = 0
    ans_val = 0
    for i in range(len(A) - 1):
        if A[i] <= A[i+1]:
            cont += 1
            diff = A[i+1] - A[i]
            ans_val += diff * cont
        else:
            cont = 0
    return ans_val

if __name__ == '__main__':
    print(main())