import java.util.*;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;

public class Main {

    static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    }

    public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    }

    public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    }
} import java.util.*; util.* java import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

    static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    }

    public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    }

    public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    }
} Main public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    } main {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    } IO fs = new IO(); fs new IO() int n = fs.nextInt(); n fs.nextInt() fs.nextInt fs int[] h = fs.readarray(n); h fs.readarray(n) fs.readarray fs n int op = 0; op 0 while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        } true {
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        } SortedSet<Integer> st = new TreeSet<Integer>(); st new TreeSet<Integer>() int prev = 0; prev 0 for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            } {
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            } if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue; i == n-1 i n-1 n 1 {
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                } st.add(h[i]) st.add st h[i] h i while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    } !st.isEmpty() && st.first() == 0 !st.isEmpty() st.isEmpty() st.isEmpty st st.first() == 0 st.first() st.first st 0 {
                        st.remove(st.first());
                    } st.remove(st.first()) st.remove st st.first() st.first st if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    } !st.isEmpty() st.isEmpty() st.isEmpty st {
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    } int minval = ((TreeSet<Integer>) st).first(); minval ((TreeSet<Integer>) st).first() ((TreeSet<Integer>) st).first ((TreeSet<Integer>) st) op+=minval op minval for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        } int j = prev; int j = prev; j prev j<=i j i j++ j++ j {
                            if(h[j]!=0)h[j] -= minval;
                        } {
                            if(h[j]!=0)h[j] -= minval;
                        } if(h[j]!=0)h[j] -= minval; h[j]!=0 h[j] h j 0 h[j] -= minval h[j] h j minval st.clear() st.clear st prev = i+1 prev i+1 i 1 if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue; h[i] == 0 && !st.isEmpty() h[i] == 0 h[i] h i 0 !st.isEmpty() st.isEmpty() st.isEmpty st {
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                } while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    } !st.isEmpty() && st.first() == 0 !st.isEmpty() st.isEmpty() st.isEmpty st st.first() == 0 st.first() st.first st 0 {
                        st.remove(st.first());
                    } st.remove(st.first()) st.remove st st.first() st.first st if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    } !st.isEmpty() st.isEmpty() st.isEmpty st {
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    } int minval = ((TreeSet<Integer>) st).first(); minval ((TreeSet<Integer>) st).first() ((TreeSet<Integer>) st).first ((TreeSet<Integer>) st) op+=minval op minval for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        } int j = prev; int j = prev; j prev j<=i j i j++ j++ j {
                            if(h[j]!=0)h[j] -= minval;
                        } {
                            if(h[j]!=0)h[j] -= minval;
                        } if(h[j]!=0)h[j] -= minval; h[j]!=0 h[j] h j 0 h[j] -= minval h[j] h j minval st.clear() st.clear st prev = i+1 prev i+1 i 1 if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue; h[i] == 0 && st.isEmpty() h[i] == 0 h[i] h i 0 st.isEmpty() st.isEmpty st prev+=1 prev 1 if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue; !st.contains(h[i]) && h[i] != 0 !st.contains(h[i]) st.contains(h[i]) st.contains st h[i] h i h[i] != 0 h[i] h i 0 st.add(h[i]) st.add st h[i] h i continue; boolean ok = true; ok true for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
                if(h[i]>0)ok = false;
            } {
                if(h[i]>0)ok = false;
            } if(h[i]>0)ok = false; h[i]>0 h[i] h i 0 ok = false ok false if(ok)break; ok break; System.out.println(op) System.out.println System.out System System.out op String[] args args public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    } check {
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    } for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
            if(h[i]>0)return false;
        } {
            if(h[i]>0)return false;
        } if(h[i]>0)return false; h[i]>0 h[i] h i 0 return false; false return true; true int [] h h int n n static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    } IO BufferedReader br; br StringTokenizer st; st public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        } IO {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } nextLine {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } String str = ""; str "" try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } IOException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                str = br.readLine();
            } str = br.readLine() str br.readLine() br.readLine br return str; str int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        } readarray {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        } int[] ret = new int[sz]; ret new int[sz] sz for (int i = 0; i < sz; i++)
                ret[i] = nextInt(); int i = 0; int i = 0; i 0 i < sz i sz i++ i++ i ret[i] = nextInt(); ret[i] = nextInt() ret[i] ret i nextInt() nextInt return ret; ret int sz sz public class Main {

    static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    }

    public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    }

    public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    }
} public class Main {

    static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    }

    public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    }

    public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    }
} Main
import java.util.*;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader;

public class Main {

    static class IO {
        BufferedReader br;
        StringTokenizer st;

        public IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readarray(int sz) {
            int[] ret = new int[sz];
            for (int i = 0; i < sz; i++)
                ret[i] = nextInt();
            return ret;
        }
    }

    public static void main(String[] args) {
        IO fs = new IO();
        int n = fs.nextInt();
        int[] h = fs.readarray(n);
        int op = 0;
        while(true){
            SortedSet<Integer> st = new TreeSet<Integer>();
            //System.out.println(1);
            int prev = 0;
            for(int i = 0; i<n; i++){
                if(i == n-1){
                    st.add(h[i]);
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && !st.isEmpty()){
                    while(!st.isEmpty() && st.first() == 0){
                        st.remove(st.first());
                    }
                    if(!st.isEmpty()){
                        int minval = ((TreeSet<Integer>) st).first();
                        //System.out.println(minval);
                        op+=minval;
                        for(int j = prev; j<=i; j++){
                            if(h[j]!=0)h[j] -= minval;
                        }
                        st.clear();
                    }
                    prev = i+1;
                }
                else if(h[i] == 0 && st.isEmpty())prev+=1;
                else if(!st.contains(h[i]) && h[i] != 0) st.add(h[i]);
                else continue;
            }
            boolean ok = true;
            for(int i = 0; i<n; i++){
                if(h[i]>0)ok = false;
            }
            if(ok)break;
        }
        System.out.println(op);
    }

    public static boolean check(int [] h,int n){
        for(int i = 0; i<n; i++){
            if(h[i]>0)return false;
        }
        return true;
    }
}
