#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main() {
  int N;
  cin >> N;
  int count = 0;
  int max = 0;
  for (ll i = 0, i_len = (N); i < i_len; ++i) {
    int A;
    cin >> A;
    if (max < A)
      max = A;
    else {
      count += (max - A);
    }
  }
  cout << count << endl;
}
