#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int MOD = (int)998244353;
const int INF = (int)1e18;
const long long N = 1e5 + 5;
bool cmp(const pair<long long, long long>& a,
         const pair<long long, long long>& b) {
  return a.second < b.second;
}
void solve() {
  long long n, cnt = 0;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1]) cnt += a[i + 1] - a[i];
  }
  cout << cnt;
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
}
