#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) cin >> a[i];
  vector<int> b;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      int c;
      c = a[i] - a[i + 1];
      b.push_back(c);
      a[i + 1] += c;
    }
  }
  int ans = 0;
  for (int i = 0; i < b.size(); i++) {
    ans += b[i];
  }
  cout << ans << endl;
}
