#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, tmp;
  cin >> N;
  int m = -1;
  cin >> tmp;
  m = max(m, tmp);
  int ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    cin >> tmp;
    if (tmp < m) ans += m - tmp;
    m = max(m, tmp);
  }
  cout << ans << endl;
  return 0;
}
