#include<iostream>
#include<vector>
using namespace std;
int c = 0;
void build(vector<int> a,int sta, int end)
{
if (sta == end)
{
c += a[end];
return;
}
int Min = 10000;
int t = 0;
for (int i = sta; i <= end; i++)
{
if (Min > a[i])
{
Min = a[i];
t = i;
}
}
c += a[t];
for (int i = sta; i <= end; i++)
{
a[i] -= Min;
}
if (sta != t)
{
build(a, sta, t - 1);
}
if(t != end)
{
build(a, t + 1, end);
}
return;
}
int main()
{
int n;
while (cin >> n)
{
vector<int> a;
int x;
for (int i = 0; i < n; i++)
{
cin >> x;
a.push_back(x);
}
build(a, 0, n - 1);
cout << c << endl;
c = 0;
}
return 0;
}