#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) cin >> a[i];
  int ans = 0;
  int p = a[0];
  for (int i = 1; i < n; i++) {
    int f = 0;
    if (p > a[i])
      f = p - a[i];
    else
      f = 0;
    p = a[i] + f;
    ans += f;
  }
  cout << ans << endl;
  return 0;
}
