#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int result = 0;
  int maxindex;
  int max = INT_MAX;
  for (int i = 0; i < n; i++) {
    if (max < arr[i]) {
      max = arr[i];
      maxindex = i;
    }
  }
  int temp;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      temp = arr[i];
    else if (i < maxindex) {
      if (temp < arr[i])
        result = result;
      else {
        result = result + arr[i] - temp;
        temp = arr[i];
      }
    } else if (i >= maxindex) {
      result = result + max - arr[i];
    }
  }
  cout << result << endl;
}
