#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  int x, last;
  cin >> x;
  last = x;
  n--;
  while (n--) {
    cin >> x;
    ans += max(0, x - last);
    last = x;
  }
  cout << ans << '\n';
}
