#include <bits/stdc++.h>
using namespace std;
long long ar[1000005];
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> ar[i];
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    if (ar[i + 1] - ar[i] > 0) ans += abs(ar[i + 1] - ar[i]);
  }
  cout << ans << "\n";
  return 0;
}
