#include <bits/stdc++.h>
using namespace std;
void printSet(int vS) {
  printf("S = %2d = ", vS);
  stack<int> st;
  while (vS) st.push(vS % 2), vS /= 2;
  while (!st.empty()) printf("%d", st.top()), st.pop();
  printf("\n");
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  int large = v[0], cnt = 0;
  for (int i : v) {
    large = max(large, i);
    cnt += (large - i);
  }
  cout << cnt << endl;
}
