#include <bits/stdc++.h>
const int maxn = 200005;
int n;
int a[maxn], b[maxn];
int ans;
long long dp[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = 0;
  dp[1] = 0;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] > a[i]) {
      b[i] = a[i - 1] - a[i];
      a[i] += a[i - 1] - a[i];
    }
    dp[i] = dp[i - 1] + b[i];
  }
  printf("%d\n", dp[n]);
  return 0;
}
