#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
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;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
ll N;
ll A[201010];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int i = 1; i < N; i++) {
    ll diff = A[i] - A[i - 1];
    if (diff > 0) {
      ans += diff;
      A[i] += diff;
    }
  }
  cout << ans;
}
