#include <bits/stdc++.h>
int main() {
  int N;
  std::cin >> N;
  std::vector<int> a;
  int t = 0;
  for (int i = 0; i < N; i++) {
    std::cin >> t;
    a.push_back(t);
  }
  int ans = 0;
  for (int i = 0; i < a.size() - 1; i++) {
    if (a[i] > a[i + 1]) {
      ans += a[i] - a[i + 1];
      a[i + 1] = a[i + 1] + (a[i] - a[i + 1]);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
