#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 10;
const long long INF = 1e9 + 100;
const int mod = 998244353;
int a[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  printf("%d", ans);
}
