#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i;
  cin >> n;
  int a[n + 1], s1 = 0, s2 = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    s1 += a[i];
  }
  a[0] = 0;
  for (i = 1; i <= n; i++) {
    if (a[i] < a[i - 1]) a[i] = a[i - 1];
    s2 += a[i];
  }
  cout << s2 - s1;
  return 0;
}
