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