#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int sum = 0;
void solve(vector<int> v, int start, int end) {
  if (start < 0 || end < 0) return;
  if (start >= end) return;
  int m = INT_MIN;
  int index = 0;
  for (int i = start; i <= end; i++) {
    if (v[i] > m) {
      m = v[i];
      index = i;
    }
  }
  for (int i = index; i < end + 1; i++) {
    sum = sum + (m - v[i]);
  }
  solve(v, start, index - 1);
}
int main() {
  int n;
  cin >> n;
  sum = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  solve(v, 0, n - 1);
  cout << sum << endl;
  return 0;
}
