#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
void addSelf(long long &a, long long b) {
  a += b;
  while (a >= 100000007) a -= 100000007;
}
void subSelf(long long &a, long long b) {
  a -= b;
  while (a < 0) a += 100000007;
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] >= a[i - 1]) {
    } else {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  clock_t start, end;
  start = clock();
  solve();
  end = clock();
}
