#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, max, t, c = 0;
  cin >> n;
  vector<int> arr(n);
  max = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > max) {
      t = i;
      max = arr[i];
    }
  }
  for (int i = t; i < n; i++) c += (arr[t] - arr[i]);
  for (int i = 0; i < t; i++) {
    if (arr[i] > arr[i + 1]) {
      c += (arr[i] - arr[i + 1]);
    }
  }
  cout << c;
}
