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