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