#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
void print() { cout << "\n"; }
template <typename T, typename... Types>
void print(T var1, Types... var2) {
  cout << var1 << " ";
  print(var2...);
}
template <typename T>
T fceil(T a, T b) {
  return (T)(a + b - 1) / b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int maxm = 0;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      maxm = max(maxm, a[i]);
      sum += maxm - a[i];
    }
    cout << sum << "\n";
  }
  return 0;
}
