#include <bits/stdc++.h>
using namespace std;
int a[100010];
int main() {
  int n;
  long long max_tmp;
  long long cnt = 0;
  scanf("%d", &n);
  scanf("%d", a);
  max_tmp = a[0];
  for (int i = 1; i < n; i++) {
    scanf("%d", a + i);
    if (a[i] + cnt > max_tmp)
      max_tmp = a[i] + cnt;
    else {
      cnt += (max_tmp - a[i] - cnt);
    }
  }
  cout << cnt << endl;
  return 0;
}
