#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void _IOS() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
}
int sx, sy, tx, ty;
struct threeElements {
  int _1st, _2nd, _3rd;
};
int main() {
  _IOS();
  int n;
  cin >> n;
  int a[100009];
  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]) {
      ans += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << ans;
}
