#include <bits/stdc++.h>
using namespace std;
int INF = 100100101;
int stoi(char c) {
  int ret = c - 48;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
  }
  int max = a[0];
  int ans = 0;
  for (int i = 0; i < (n - 1); ++i) {
    if (a[i] > max) {
      max = a[i];
    }
    if (a[i] > a[i + 1]) {
      ans += (max - a[i + 1]);
    }
  }
  cout << ans << endl;
  return 0;
}
