#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vi = vector<int>;
const ll INF = 1000000000000000;
const ll MOD = 1000000007;
const double PI = acos(-1);
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll ans = 0;
  ll N, A;
  ll prev = 0;
  cin >> N;
  for (ll i = 0; i < N; ++i) {
    cin >> A;
    if (prev) {
      if (A > prev) ans += A - prev;
    }
    prev = A;
  }
  cout << ans << endl;
  return 0;
}
