#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int n;
int a[200005];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0) {
      if (a[i] > a[i - 1]) cnt += a[i] - a[i - 1];
    }
  }
  cout << cnt << endl;
  return 0;
}
