//WTF??? NR...  it's end of my fucking life
//ONE LOVE...???
//ONE HATE...???
//one ignore????

#include <bits/stdc++.h>
#define en "\n"
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef long double ld;

using namespace std;
int arr[100100];
map <int,vector<int> > cnt;
int n;
ll ans=0;
void minuss(int pos, int delt){
    int l=pos;
    int r=pos;
    while (l>0 && arr[l-1]==arr[pos]) {
        --l;
        arr[l]-=delt;
    }
    while (r<n && arr[r+1]==arr[pos]) {
        ++r;
        arr[r]-=delt;
    }
    arr[pos]-=delt;
    ans+=delt;
}
set <int> SET;
int main()
{
    //ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin>>n;
   // cout<<1<<en;
    for (int i=0; i<n; ++i){
        cin>>arr[i];
        cnt[arr[i]].push_back(i);
        SET.insert(arr[i]);
    }

    vector <int> vec;
    while (true){
        int x=(*SET.rbegin());
        SET.erase(x);
        if (!SET.size()){
                //cout<<ans<<en;
            ans+=x;

            //cout<<x<<en;
            break;
        }
        int y=(*SET.rbegin());
        if (x==y){
            ans+=x;
             break;
        }
        for (int i=0; i<cnt[x].size(); ++i){
            if (arr[cnt[x][i]]==x){
                cnt[y].push_back(cnt[x][i]);
                minuss(cnt[x][i],x-y);
            }
        }
        cnt[x].clear();
        //cout<<ans<<en;
    }
    cout<<ans<<en;
    return 0;
}
