#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, INF = (1LL << 60);
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int m = 0, a, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a < m) {
      ans += m - a;
    } else {
      m = a;
    }
  }
  cout << ans << endl;
  return 0;
}
