#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  ll ans = 0;
  for (int i = 0; i < (int)(N); i++) cin >> A.at(i);
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  B.at(1) = A.at(0);
  ans += B.at(1) - A.at(1);
  for (int i = 1; i < N - 1; i++) {
    int maxtmp = 0;
    maxtmp = max(A.at(i + 1), B.at(i));
    B.at(i + 1) = maxtmp;
    ans += B.at(i + 1) - A.at(i + 1);
  }
  cout << ans << endl;
  return 0;
}
