#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int MAX = 1010;
const int mod = 1e9 + 7;
const int MAXLV = 16;
using namespace std;
int n, a[maxn];
int main() {
  cin >> 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]) {
      int dif = a[i - 1] - a[i];
      a[i] += dif;
      ans += dif;
    }
  }
  cout << ans << '\n';
  return 0;
}
