#include <bits/stdc++.h>
using namespace std;
const long long N = 666666, inf = 0x3f3f3f3f;
long long n, arr[N];
signed main() {
  cin >> n;
  cin >> arr[1];
  long long s = 0;
  for (long long i = 2; i <= n; i++) {
    cin >> arr[i];
    s += max(0ll, arr[i] - arr[i - 1]);
    arr[i] += max(0ll, arr[i] - arr[i - 1]);
  }
  cout << s;
  return 0;
}
