#include <bits/stdc++.h>
using namespace std;
int N;
int arr[100001];
int main() {
  int i, j, k;
  int TC, tc;
  cin >> N;
  for (int i = (int)1; i <= (int)N; i++) {
    cin >> arr[i];
  }
  long long move = 0;
  long long min, max;
  min = max = arr[1];
  for (i = 2; i <= N; i++) {
    if (arr[i] > arr[i - 1]) {
      move += (max - min);
      min = max = arr[i];
    } else if (arr[i] < arr[i - 1])
      min = arr[i];
  }
  move += (max - min);
  cout << move << "\n";
  return 0;
}
