#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long n,j, i, mi, y, mi1 = 2e9, ma, s = 0;

    cin >> n;

    mi = 2e9;
    ma = 0;

    vector <long long> mas(n);

    for (i = 0;i < n;i++){
        cin >> mas[i];
    }


    for (i = 0;i < n;i++){

        if (mas[i] < mi){
            mi = mas[i];
        }

        if (mas[i] > ma){
            ma = mas[i];
        }
    }


    s = s + mi;


    for (i = 0;i < n;i++){
        mas[i] -= mi;
    }








    while (ma != 0){
            i = 0;
            mi1 = 2e9;
            ma = 0;
        while (mas[i] == 0){
            i++;
        }

     y = i;
     while (mas[i] != 0){
        i++;
     }
     for (j = y; j < i;j++){
       if (mas[j] < mi1){
            mi1 = mas[j];

        }
     }
     s =s+mi1;
     for (j = y; j < i;j++){
       mas[j] -= mi1;
     }


        for (j = 0; j < n;j++){
         if (mas[j] > ma){
            ma = mas[j];
        }
        }

     }

     cout << s << endl;
     return 0;
}
