#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int> >;
using GraphC = vector<vector<char> >;
const int INF = 1000000000;
const int MAX_N = 200000;
const int MAX_L = 100000000;
const int MAX_SQRT_B = 10000;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int N;
int A[200010];
int main() {
  cin >> N;
  int ans = 0;
  for (int i = 0; i < (int)N; i++) {
    cin >> A[i];
    if (i > 0 && A[i - 1] > A[i]) {
      ans += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << ans << endl;
}
