#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
const int inf = 0x3f3f3f3f;
const int N = 2e6 + 10;
int a[N];
void solve() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 2; i <= n; i++)
    if (a[i] < a[i - 1]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
