#include <bits/stdc++.h>
using namespace std;
using in = int64_t;
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<in> a(n);
  for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
  in ans = 0ll;
  in zero = 0ll;
  for (int i = 0; i < (int)(n); i++) {
    if (i == 0) continue;
    ans += max(zero, a[i] - a[i - 1]);
  }
  cout << ans << endl;
}
