#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  int ans = 0;
  for (int j = n - 1; j >= 1; j--) {
    int max = v[j];
    for (int k = j - 1; k >= 0; k--) {
      if (v[k] > max) {
        max = v[k];
      }
    }
    ans = ans + (max - v[j]);
  }
  cout << ans;
  return 0;
}
