#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MOD = 998244353;
const int N = 1e6 + 10, M = N;
int main() {
  clock_t c1 = clock();
  int n, a;
  cin >> n;
  int res = 0, num = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    if (a < num)
      res += abs(num - a);
    else
      num = a;
  }
  cout << res << endl;
  return 0;
}
