#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long int s[300000];
  unsigned long long int n, x = 0, max = 0;
  cin >> n;
  cin >> s[0];
  for (int i = 1; i < n; i++) {
    cin >> s[i];
    if (max > s[i])
      x += max - s[i];
    else
      max = s[i];
  }
  cout << x;
  return 0;
}
