#include <bits/stdc++.h>
using namespace std;
bool check9(string s) {
  int res = 0, i;
  for (i = 0; i < s.length(); i++) {
    res = (res * 10 + s[i] - '0') % 9;
  }
  if (res == 0) {
    return true;
  } else {
    return false;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, count = 0, i;
  cin >> n;
  int arr[n], maxi = 0, idx = 0;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] > maxi) {
      maxi = arr[i];
      idx = i;
    }
  }
  for (i = 1; i < idx; i++) {
    if (arr[i] < arr[i - 1]) {
      count += arr[i - 1] - arr[i];
    }
  }
  for (i = idx + 1; i < n; i++) {
    count += maxi - arr[i];
  }
  cout << count << "\n";
}
