#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pi = pair<int, int>;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int y = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > a[i + 1] && a[i] >= a[i - 1]) {
      y = i;
      break;
    }
  }
  long long int c = 0, j = 0, first = 0, i = 0;
  for (int i = 0; i <= y - 1; i++) {
    c += a[y] - a[i];
  }
  while (i < n) {
    first = 0;
    if (a[i] > a[i + 1] && a[i] >= a[i - 1]) {
      j = i + 1;
      while (a[i] >= a[j]) {
        c += a[i] - a[j];
        j++;
      }
      i = j;
      first = 1;
    }
    if (first == 0) {
      i++;
    }
  }
  cout << c << endl;
}
