#include <bits/stdc++.h>
int N;
int main() {
  std::cin >> N;
  int cur_h;
  std::cin >> cur_h;
  int ans = 0;
  for (int i = 1; i < N; ++i) {
    int x;
    std::cin >> x;
    ans += std::max(0, cur_h - x);
    cur_h = x;
  }
  std::cout << ans << "\n";
  return 0;
}
