#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
int n, ans;
int a, _max = -1;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    if (a <= _max) ans += _max - a;
    if (a > _max) _max = a;
  }
  cout << ans;
  return 0;
}
