#include <bits/stdc++.h>
using namespace std;

int main() {
  long N, A, mx, ans = 0;
  cin >> N >> mx;
  while (cin >> A) {
    if (A < mx) ans += mx - A;
    mx = max(mx, A);
  }
  cout << ans << "\n";
}