#include <bits/stdc++.h>
const int INF = 1 << 29;
using namespace std;
using ll = long long;
int main() {
  int N, count = 0;
  cin >> N;
  vector<int> A(N);
  int tallest = A[0];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    if (tallest > A[i]) {
      count += (tallest - A[i]);
      A[i] = tallest;
    } else {
      tallest = A[i];
    }
  }
  cout << count << endl;
}
