import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    }

    static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    }

    static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) ABC116_C solver = new ABC116_C(); solver new ABC116_C() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    } ABC116_C public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        } solve {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        } int n = in.nextInt(); n in.nextInt() in.nextInt in int h[] = new int[n]; h new int[n] n int ans = 0; ans 0 int max = 0; max 0 for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            } {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            } h[i] = in.nextInt() h[i] h i in.nextInt() in.nextInt in max = Math.max(max, h[i]) max Math.max(max, h[i]) Math.max Math max h[i] h i int startIndex = 0; startIndex 0 int endIndex = h.length - 1; endIndex h.length - 1 h.length h h.length 1 for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            } int i = startIndex; int i = startIndex; i startIndex i <= endIndex i endIndex i++ i++ i {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            } {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            } if (h[i] != 0) {
                    startIndex = i;
                    break;
                } h[i] != 0 h[i] h i 0 {
                    startIndex = i;
                    break;
                } startIndex = i startIndex i break; for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            } int i = endIndex; int i = endIndex; i endIndex startIndex <= i startIndex i i-- i-- i {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            } {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            } if (h[i] != 0) {
                    endIndex = i;
                    break;
                } h[i] != 0 h[i] h i 0 {
                    endIndex = i;
                    break;
                } endIndex = i endIndex i break; boolean repeatingZero = false; repeatingZero false while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            } max > 0 max 0 {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            } for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                } int i = startIndex; int i = startIndex; i startIndex i <= endIndex i endIndex i++ i++ i {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                } {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                } if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    } h[i] == 0 && !repeatingZero h[i] == 0 h[i] h i 0 !repeatingZero repeatingZero {
                        ans++;
                        repeatingZero = true;
                    } ans++ ans repeatingZero = true repeatingZero true if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    } h[i] != 0 && repeatingZero h[i] != 0 h[i] h i 0 repeatingZero {
                        repeatingZero = false;
                    } repeatingZero = false repeatingZero false if (h[i] > 0) {
                        h[i]--;
                    } h[i] > 0 h[i] h i 0 {
                        h[i]--;
                    } h[i]-- h[i] h i ans++ ans for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                } int i = startIndex; int i = startIndex; i startIndex i <= endIndex i endIndex i++ i++ i {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                } {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                } if (h[i] != 0) {
                        startIndex = i;
                        break;
                    } h[i] != 0 h[i] h i 0 {
                        startIndex = i;
                        break;
                    } startIndex = i startIndex i break; for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                } int i = endIndex; int i = endIndex; i endIndex startIndex <= i startIndex i i-- i-- i {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                } {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                } if (h[i] != 0) {
                        endIndex = i;
                        break;
                    } h[i] != 0 h[i] h i 0 {
                        endIndex = i;
                        break;
                    } endIndex = i endIndex i break; max-- max out.print(ans) out.print out ans int testNumber testNumber Scanner in in PrintWriter out out public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    }

    static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    }

    static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ABC116_C solver = new ABC116_C();
        solver.solve(1, in, out);
        out.close();
    }

    static class ABC116_C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int h[] = new int[n];
            int ans = 0;
            int max = 0;
            for (int i = 0; i < n; i++) {
                h[i] = in.nextInt();
                max = Math.max(max, h[i]);
            }
            int startIndex = 0;
            int endIndex = h.length - 1;
            for (int i = startIndex; i <= endIndex; i++) {
                if (h[i] != 0) {
                    startIndex = i;
                    break;
                }
            }

            for (int i = endIndex; startIndex <= i; i--) {
                if (h[i] != 0) {
                    endIndex = i;
                    break;
                }
            }
            boolean repeatingZero = false;
            while (max > 0) {
                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] == 0 && !repeatingZero) {
                        ans++;
                        repeatingZero = true;
                    }

                    if (h[i] != 0 && repeatingZero) {
                        repeatingZero = false;
                    }

                    if (h[i] > 0) {
                        h[i]--;
                    }
                }

                ans++;

                for (int i = startIndex; i <= endIndex; i++) {
                    if (h[i] != 0) {
                        startIndex = i;
                        break;
                    }
                }

                for (int i = endIndex; startIndex <= i; i--) {
                    if (h[i] != 0) {
                        endIndex = i;
                        break;
                    }
                }

                max--;
            }

            out.print(ans);

        }

    }
}

