import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static int total = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    }

    public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

    private static int total = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    }

    public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    }
} Main private static int total = 0; total 0 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] nums = new int[N]; nums new int[N] N for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            nums[i] = sc.nextInt();
        } {
            nums[i] = sc.nextInt();
        } nums[i] = sc.nextInt() nums[i] nums i sc.nextInt() sc.nextInt sc me(nums) me nums System.out.println(Main.total) System.out.println System.out System System.out Main.total Main Main.total String[] args args public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    } me {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int num : nums) {
            min = num < min ? num : min;
        } int num nums {
            min = num < min ? num : min;
        } {
            min = num < min ? num : min;
        } min = num < min ? num : min min num < min ? num : min num < min num min num min Main.total += min Main.total Main Main.total min int ti = 0; ti 0 int i; i for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        } i = 0; i = 0 i 0 i < nums.length i nums.length nums nums.length i++ i++ i {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        } {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        } nums[i] -= min nums[i] nums i min if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            } nums[i] == 0 nums[i] nums i 0 {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            } if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                } ti < i ti i {
                    me(Arrays.copyOfRange(nums, ti, i));
                } me(Arrays.copyOfRange(nums, ti, i)) me Arrays.copyOfRange(nums, ti, i) Arrays.copyOfRange Arrays nums ti i ti = i + 1 ti i + 1 i 1 if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        } ti < i ti i {
            me(Arrays.copyOfRange(nums, ti, i));
        } me(Arrays.copyOfRange(nums, ti, i)) me Arrays.copyOfRange(nums, ti, i) Arrays.copyOfRange Arrays nums ti i int[] nums nums public class Main {

    private static int total = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    }

    public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    }
} public class Main {

    private static int total = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    }

    public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    private static int total = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }
        me(nums);
        System.out.println(Main.total);

    }

    public static void me(int[] nums) {

        int min = Integer.MAX_VALUE;
        for (int num : nums) {
            min = num < min ? num : min;
        }
        Main.total += min;
        int ti = 0;
        int i;
        for (i = 0; i < nums.length; i++) {
            nums[i] -= min;
            if (nums[i] == 0) {
                if (ti < i) {
                    me(Arrays.copyOfRange(nums, ti, i));
                }
                ti = i + 1;
            }
        }
        if (ti < i) {
            me(Arrays.copyOfRange(nums, ti, i));
        }
    }
}