import java.util.*;

public class Main {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    }


}
 import java.util.*; util.* java public class Main {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    }


} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    } run {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    } int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] arr = new int[n]; arr new int[n] n int[] check = new int[n]; check new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc int res = 0; res 0 while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        } !Arrays.equals(arr, check) Arrays.equals(arr, check) Arrays.equals Arrays arr check {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        } int l = 0; l 0 int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE while (l < n && arr[l] == 0) {
                l++;
            } l < n && arr[l] == 0 l < n l n arr[l] == 0 arr[l] arr l 0 {
                l++;
            } l++ l int cp = l; cp l while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            } l < n && arr[l] != 0 l < n l n arr[l] != 0 arr[l] arr l 0 {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            } min = Math.min(min, arr[l]) min Math.min(min, arr[l]) Math.min Math min arr[l] arr l l++ l for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            } int i=cp; int i=cp; i cp i<l i l i++ i++ i {
                arr[i] -= min;
//                System.out.println(cp);
            } {
                arr[i] -= min;
//                System.out.println(cp);
            } arr[i] -= min arr[i] arr i min res += min res min System.out.println(res) System.out.println System.out System System.out res public class Main {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    }


} public class Main {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    }


} Main
import java.util.*;

public class Main {

    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Main().run();
    }

    void run() {

        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] check = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int res = 0;

        while (!Arrays.equals(arr, check)) {
            int l = 0;
            int min = Integer.MAX_VALUE;
            while (l < n && arr[l] == 0) {
                l++;
            }
            int cp = l;
            while (l < n && arr[l] != 0) {
                min = Math.min(min, arr[l]);
                l++;
//                System.out.println(min);
            }
            for (int i=cp; i<l; i++) {
                arr[i] -= min;
//                System.out.println(cp);
            }
            res += min;

        }


        System.out.println(res);

    }


}

