#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 50;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void solve() {
  int n, res = 0;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; ++i) cin >> vec[i];
  for (int i = 0; i < n - 1; ++i) {
    if (vec[i] > vec[i + 1]) {
      res += vec[i] - vec[i + 1];
      vec[i + 1] = vec[i];
    }
  }
  cout << res << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
}
