#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> v(N);
  for (int i = 0; i < N; ++i) cin >> v[i];
  long long ans = 0, tmp;
  for (int i = N - 1; i >= 1; --i) {
    tmp = 0;
    for (int j = i; j >= 0; --j) {
      tmp = max(tmp, v[j]);
    }
    ans += tmp - v[i];
  }
  cout << ans << endl;
}
