#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned N, a, b, ans = 0;
  cin >> N >> a;
  for (int i = 1; i < N; ++i) {
    cin >> b;
    if (a > b) {
      ans += a - b;
    } else {
      a = b;
    }
  }
  cout << ans << endl;
  getchar();
}
