#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a, b;
  int ans = 0;
  cin >> n;
  cin >> a;
  for (int i = 0; i < n - 1; i++) {
    cin >> b;
    if (b - a >= 0) {
      ans += b - a;
    }
    a = b;
  }
  cout << ans << endl;
}
