#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int res = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      int diff = abs(a[i] - a[i - 1]);
      res += diff;
      a[i] += diff;
    }
  }
  cout << res << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ofstream out("output.txt");
  auto start = chrono::high_resolution_clock::now();
  long long t = 1;
  for (long long test = 1; test <= t; test++) {
    solve();
  }
  auto stop = chrono::high_resolution_clock::now();
}
