#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll N;
  ll count = 0;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> max(N);
  vector<ll> dis(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (ll k = 1; k <= N; k++) {
    max[k - 1] = A[k - 1];
    for (ll j = 0; j <= k - 1; j++) {
      if (max[k - 1] < A[j]) {
        max[k - 1] = A[j];
      }
    }
    dis[k - 1] = max[k - 1] - A[k - 1];
    count = count + dis[k - 1];
  }
  cout << count << endl;
}
