#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vint = vector<int>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template <typename T>
T sq(T x) {
  return x * x;
}
ll mod = 1e9 + 7;
signed main() {
  int N;
  cin >> N;
  vint A(N);
  for (int i = (int)(0); i < (int)(N); i++) {
    cin >> A.at(i);
  }
  int a_max = 0;
  int ans = 0;
  for (int i = (int)(0); i < (int)(N); i++) {
    if (i == 0) {
      a_max = A.at(0);
    } else {
      if (A.at(i) < a_max) {
        ans += a_max - A.at(i);
      } else {
        a_max = A.at(i);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
