import java.util.*;
class Main {

    int trialNum;
    
    Main() {
        trialNum = 0;
    }

    int findMin(int[] h, int start, int end) {
        int min = h[start];
        for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        }
        return min;
    }

    int[] findZeroPos(int[] h, int start, int end) {
        List<Integer> list = new ArrayList<>();
        for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        }
        int[] zeroPos = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        }
        return zeroPos;
    }

    void watering(int[] h, int start, int end, int n) {
        for(int i = start; i <= end; i++) {
            h[i] -= n;
        }
        trialNum += n;
    }

    void growFlower(int[] h, int l, int r) {
        if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        }
    }

    void print(int[] h) {
        for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        }
        System.out.print("\n");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] h = new int[N];
        for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        }
        Main myG = new Main();
        myG.growFlower(h, 0, N-1);
        System.out.println(myG.trialNum);
    }
} import java.util.*; util.* java class Main {

    int trialNum;
    
    Main() {
        trialNum = 0;
    }

    int findMin(int[] h, int start, int end) {
        int min = h[start];
        for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        }
        return min;
    }

    int[] findZeroPos(int[] h, int start, int end) {
        List<Integer> list = new ArrayList<>();
        for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        }
        int[] zeroPos = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        }
        return zeroPos;
    }

    void watering(int[] h, int start, int end, int n) {
        for(int i = start; i <= end; i++) {
            h[i] -= n;
        }
        trialNum += n;
    }

    void growFlower(int[] h, int l, int r) {
        if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        }
    }

    void print(int[] h) {
        for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        }
        System.out.print("\n");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] h = new int[N];
        for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        }
        Main myG = new Main();
        myG.growFlower(h, 0, N-1);
        System.out.println(myG.trialNum);
    }
} Main int trialNum; trialNum Main() {
        trialNum = 0;
    } Main {
        trialNum = 0;
    } trialNum = 0 trialNum 0 int findMin(int[] h, int start, int end) {
        int min = h[start];
        for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        }
        return min;
    } findMin {
        int min = h[start];
        for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        }
        return min;
    } int min = h[start]; min h[start] h start for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        } int i = start; int i = start; i start i <= end i end i++ i++ i {
            if(min > h[i]) min = h[i];
        } {
            if(min > h[i]) min = h[i];
        } if(min > h[i]) min = h[i]; min > h[i] min h[i] h i min = h[i] min h[i] h i return min; min int[] h h int start start int end end int[] findZeroPos(int[] h, int start, int end) {
        List<Integer> list = new ArrayList<>();
        for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        }
        int[] zeroPos = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        }
        return zeroPos;
    } findZeroPos {
        List<Integer> list = new ArrayList<>();
        for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        }
        int[] zeroPos = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        }
        return zeroPos;
    } List<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        } int i = start; int i = start; i start i <=end i end i++ i++ i {
            if(h[i] == 0) list.add(i);
        } {
            if(h[i] == 0) list.add(i);
        } if(h[i] == 0) list.add(i); h[i] == 0 h[i] h i 0 list.add(i) list.add list i int[] zeroPos = new int[list.size()]; zeroPos new int[list.size()] list.size() list.size list for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        } int i = 0; int i = 0; i 0 i < list.size() i list.size() list.size list i++ i++ i {
            zeroPos[i] = list.get(i);
        } {
            zeroPos[i] = list.get(i);
        } zeroPos[i] = list.get(i) zeroPos[i] zeroPos i list.get(i) list.get list i return zeroPos; zeroPos int[] h h int start start int end end void watering(int[] h, int start, int end, int n) {
        for(int i = start; i <= end; i++) {
            h[i] -= n;
        }
        trialNum += n;
    } watering {
        for(int i = start; i <= end; i++) {
            h[i] -= n;
        }
        trialNum += n;
    } for(int i = start; i <= end; i++) {
            h[i] -= n;
        } int i = start; int i = start; i start i <= end i end i++ i++ i {
            h[i] -= n;
        } {
            h[i] -= n;
        } h[i] -= n h[i] h i n trialNum += n trialNum n int[] h h int start start int end end int n n void growFlower(int[] h, int l, int r) {
        if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        }
    } growFlower {
        if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        }
    } if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        } l >= r l r {
            trialNum += h[l];
            return;
        } trialNum += h[l] trialNum h[l] h l return; {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        } if(h[l] == 0) l++; h[l] == 0 h[l] h l 0 l++ l if(h[r] == 0) r--; h[r] == 0 h[r] h r 0 r-- r int min = findMin(h, l, r); min findMin(h, l, r) findMin h l r watering(h, l, r, min) watering h l r min int[] zeroPos = findZeroPos(h, l, r); zeroPos findZeroPos(h, l, r) findZeroPos h l r growFlower(h, l, zeroPos[0]-1) growFlower h l zeroPos[0]-1 zeroPos[0] zeroPos 0 1 for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            } int i = 1; int i = 1; i 1 i < zeroPos.length i zeroPos.length zeroPos zeroPos.length i++ i++ i {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            } {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            } growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1) growFlower h zeroPos[i-1]+1 zeroPos[i-1] zeroPos i-1 i 1 1 zeroPos[i]-1 zeroPos[i] zeroPos i 1 growFlower(h, zeroPos[zeroPos.length-1], r) growFlower h zeroPos[zeroPos.length-1] zeroPos zeroPos.length-1 zeroPos.length zeroPos zeroPos.length 1 r int[] h h int l l int r r void print(int[] h) {
        for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        }
        System.out.print("\n");
    } print {
        for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        }
        System.out.print("\n");
    } for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        } int i = 0; int i = 0; i 0 i < h.length i h.length h h.length i++ i++ i {
            System.out.print(h[i]+" ");
        } {
            System.out.print(h[i]+" ");
        } System.out.print(h[i]+" ") System.out.print System.out System System.out h[i]+" " h[i] h i " " System.out.print("\n") System.out.print System.out System System.out "\n" int[] h h public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] h = new int[N];
        for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        }
        Main myG = new Main();
        myG.growFlower(h, 0, N-1);
        System.out.println(myG.trialNum);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] h = new int[N];
        for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        }
        Main myG = new Main();
        myG.growFlower(h, 0, N-1);
        System.out.println(myG.trialNum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] h = new int[N]; h new int[N] N for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            h[i] = sc.nextInt();
        } {
            h[i] = sc.nextInt();
        } h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc Main myG = new Main(); myG new Main() myG.growFlower(h, 0, N-1) myG.growFlower myG h 0 N-1 N 1 System.out.println(myG.trialNum) System.out.println System.out System System.out myG.trialNum myG myG.trialNum String[] args args
import java.util.*;
class Main {

    int trialNum;
    
    Main() {
        trialNum = 0;
    }

    int findMin(int[] h, int start, int end) {
        int min = h[start];
        for(int i = start; i <= end; i++) {
            if(min > h[i]) min = h[i];
        }
        return min;
    }

    int[] findZeroPos(int[] h, int start, int end) {
        List<Integer> list = new ArrayList<>();
        for(int i = start; i <=end; i++) {
            if(h[i] == 0) list.add(i);
        }
        int[] zeroPos = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            zeroPos[i] = list.get(i);
        }
        return zeroPos;
    }

    void watering(int[] h, int start, int end, int n) {
        for(int i = start; i <= end; i++) {
            h[i] -= n;
        }
        trialNum += n;
    }

    void growFlower(int[] h, int l, int r) {
        if(l >= r) {
            trialNum += h[l];
            return;
        }
        else {
            if(h[l] == 0) l++;
            if(h[r] == 0) r--;
            int min = findMin(h, l, r);
            watering(h, l, r, min);
            int[] zeroPos = findZeroPos(h, l, r);
            growFlower(h, l, zeroPos[0]-1);
            for(int i = 1; i < zeroPos.length; i++) {
                growFlower(h, zeroPos[i-1]+1, zeroPos[i]-1);
            }
            growFlower(h, zeroPos[zeroPos.length-1], r);
        }
    }

    void print(int[] h) {
        for(int i = 0; i < h.length; i++) {
            System.out.print(h[i]+" ");
        }
        System.out.print("\n");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] h = new int[N];
        for(int i = 0; i < N; i++) {
            h[i] = sc.nextInt();
        }
        Main myG = new Main();
        myG.growFlower(h, 0, N-1);
        System.out.println(myG.trialNum);
    }
}