#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  stack<int> st;
  int num;
  cin >> num;
  --n;
  st.push(num);
  int stool = 0;
  while (n--) {
    cin >> num;
    if (st.top() > num) {
      stool += (st.top() - num);
    } else {
      st.push(num);
    }
  }
  cout << stool << endl;
  return 0;
}
