#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  int n;
  std::vector<int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    v.push_back(temp);
  }
  int cnt = 0, maxx = v[0];
  for (int i = 1; i < n; i++) {
    maxx = max(maxx, v[i]);
    cnt += (maxx - v[i]);
  }
  cout << cnt;
  return 0;
}
