#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const long long INF = 1ll << 60;
int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < (int)(N); i++) {
    cin >> A[i];
  }
  int step = 0;
  int sum = 0;
  for (int i = 1; i <= N; i++) {
    step = A[i - 1] - A[i];
    if (step > 0) {
      A[i] += step;
      sum += step;
    }
  }
  cout << sum << endl;
  return 0;
}
