#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, a[N], ans, ma;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  ma = a[1];
  for (int i = 2; i <= n; i++) {
    ans += max(0, ma - a[i]);
    ma = max(ma, a[i]);
  }
  cout << ans << endl;
  return 0;
}
