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