#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, a, b, ans = 0;
  cin >> n;
  cin >> a;
  n--;
  while (n--) {
    cin >> b;
    if (b < a) {
      ans += (a - b);
    } else
      a = b;
  }
  cout << ans;
}
