#include <bits/stdc++.h>
using namespace std;
void cin_ar(int n, int ar[]) {
  for (int i = 0; i < n; i++) cin >> ar[i];
}
void cin_ll(int n, long long ar[]) {
  for (int i = 0; i < n; i++) cin >> ar[i];
}
void cin_v(int n, vector<int> v) {
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    ;
  }
}
bool cmp(int a, int b) { return a > b; }
bool cmp_pair(pair<int, int> &a, pair<int, int> &b) {
  return a.second < b.second;
}
int n, k;
int bs(int x1, int x2) { return abs(2 * x2 - x1) - k; }
void solve() {
  int n;
  cin >> n;
  int ar[n];
  cin_ar(n, ar);
  int cnt = 0;
  int mx = ar[0];
  for (int i = 1; i < n; i++) {
    if (ar[i] < ar[i - 1]) {
      mx = ar[i];
    } else {
      cnt += ar[i] - mx;
    }
  }
  cout << cnt << endl;
}
void solve_2() {
  int t;
  cin >> t;
  while (t--) solve();
}
int main() {
  solve();
  return 0;
}
