#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const long long mod = 1e9 + 7;
const int N = 1e3 + 4;
template <typename... T>
void read(T &...args) {
  ((cin >> args), ...);
}
template <typename... T>
void write(T &&...args) {
  ((cout << args), ...);
}
void vjudge() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const int MAX = 2e4 + 5;
const int INF = 1e9 + 5;
void solve(int test) {
  int n;
  read(n);
  vector<int> a(n);
  int ans = 0;
  for (int i = 0; i < n; i++) read(a[i]);
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      ans += (a[i] - a[i + 1]);
      a[i + 1] += (a[i] - a[i + 1]);
    }
  }
  write(ans);
}
int32_t main() {
  vjudge();
  int t = 1;
  for (int i = 1; i <= t; i++) solve(i);
}
