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