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