#include <bits/stdc++.h>
using namespace std;
int i, j, k, l;
static const int MAX_N = 10e5 * 2;
int main(void) {
  int N;
  int max, res = 0, sum = 0, kaz = 0;
  cin >> N >> max;
  for ((i) = 0; (i) < (N - 1); (i)++) {
    int a;
    cin >> a;
    if (max <= a) {
      if (kaz != 0) {
        res += sum + kaz * (a - max);
        kaz = 0;
        sum = 0;
      }
      max = a;
    } else {
      sum += max - a;
      kaz++;
    }
  }
  cout << res << endl;
  ;
  return 0;
}
