def abc176_c():
    n = int(input())
    a = list(map(int, input().split(' ')))
    ans = 0

    for i in range(len(a) - 1):
        if a[i] < a[i + 1]:
            ans += a[i + 1] - a[i]

    print(ans)



if __name__ == '__main__':
    abc176_c()