#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long MOD = 1e9 + 7;
const double EPS = 1E-9;
long long ar[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i];
  long long sum = 0, m = ar[0];
  for (int i = 1; i < n; i++) {
    m = max(m, ar[i]);
    sum += (m - ar[i]);
  }
  cout << sum << "\n";
  return 0;
}
