#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int ans = 0;
  int nowmax = 0;
  for (int i = 0; i < N; ++i) {
    int num;
    cin >> num;
    if (num < nowmax) {
      ans += (nowmax - num);
    } else {
      nowmax = num;
    }
  }
  cout << ans << endl;
  return 0;
}
