#include <iostream>

using namespace std;

int n, p, x;
long long ans;

int main() {
  cin >> n;
  cin >> p;
  for (int i = 2; i <= n; i++) {
    cin >> x;
    ans += max(0, p - x);
    p = max(p, x);
  }
  cout << ans;
  return 0;
}