#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); ++i) cin >> A[i];
  vector<int> diff(N - 1);
  for (int i = 0; i < (N - 1); ++i) {
    diff[i] = A[i + 1] - A[i];
  }
  ll sum = 0;
  ll continue_cnt = 0;
  for (auto v : diff) {
    if (v < 0) {
      ++continue_cnt;
      sum += -1 * v * continue_cnt;
    } else {
      continue_cnt = 0;
    }
  }
  cout << sum << endl;
  return 0;
}
