#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (int)(N); i++) {
    cin >> A[i];
  }
  ll a = accumulate(A.begin(), A.end(), 0);
  for (int i = 1; i < (int)(N); i++) {
    if (A[i - 1] > A[i]) {
      A[i] = A[i - 1];
    }
  }
  ll b = accumulate(A.begin(), A.end(), 0);
  cout << b - a << endl;
  return 0;
}
