#include <iostream>

using namespace std;
using ll = long long;
int main() {
  ll N, ans{}, A, M{};
  cin >> N;
  while (cin >> A) {
    M = max(A, M);
    ans += max(0ll, M - A);
  }
  cout << ans << endl;
}
