#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using vpii = vector<pii>;
int main() {
  int N;
  cin >> N;
  vi A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  int ans = 0;
  for (int i = 1; i < N; i++) {
    int tmp = A.at(i - 1) - A.at(i);
    if (tmp <= 0) continue;
    A.at(i) += tmp;
    ans += tmp;
  }
  cout << ans << endl;
}
