#include <algorithm>
#include <numeric>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std;

int main(void) {
    int N;
    cin >> N;
    vector<long long> A(N), height(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    height[0]=0;
    for (int i = 1; i < N; i++) {
        long long prev = A[i-1] + height[i-1];
        height[i] = max(0, A[i] - prev);
    }
    cout << accumulate(height.begin(), height.end(), 0) << endl;
    
    return 0;
}