#include <bits/stdc++.h>
using namespace std;
long long int i, n, m, x, y, z, j, k, l, mn = LLONG_MAX, mx = 0, c, a[100005],
                                         r, cnt = 0, sum = 0;
string str;
char ch;
set<long long int> second;
map<long long int, long long int> mp;
vector<vector<long long int>> g;
vector<long long int> v;
void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (long long int i = 0; i < n; i++) cin >> a[i];
  long long int mx = 0, step = 0;
  for (long long int i = 0; i < n; i++) {
    mx = max(mx, a[i]);
    step += mx - a[i];
  }
  cout << step << "\n";
  return 0;
}
