#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0

    for i in range(N - 1):
        diff = A[i] - A[i - 1]
        if diff > 0:
            ans += diff
    print(ans)


if __name__ == "__main__":
    main()
