#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main(void) {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> vl(n);
  for (int i = 0; i < n; ++i) {
    cin >> vl[i];
  }
  int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (vl[i] > vl[i + 1]) {
      ans += vl[i] - vl[i + 1];
      vl[i + 1] = vl[i];
    }
  }
  cout << ans;
  return 0;
}
