#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"def solve(h):\n"
"    if len(h)<2:\n"
"        return h[0]\n"
"    s=[i for i in range(len(h))if h[i]<1]\n"
"    s=[-1]+s+[len(h)]\n"
"    a=0\n"
"    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:\n"
"        if not l:continue\n"
"        v=min(l)\n"
"        l=[t-v for t in l]\n"
"        a+=v+solve(l)\n"
"    return a\n"
"input()\n"
"print(solve(list(map(int,input().split()))))\n"
"\"";
  system(s);
  return 0;
}            