#include <cstdio>

typedef long long LL;
int n;
LL a, b, ans;

int main() {
    scanf("%d", &n);
    for (int i = 0 ; i < n ; i++) {
        scanf("%lld", &b);
        if (b < a) ans += a - b, b = a;
        a = b;
    }
    printf("%lld", ans);
}
