import java.util.*;

class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        int length = Integer.valueOf(input1);
        input1 = sc.nextLine();
        String[] inputs = input1.split(" ");
        int[] numbers = new int[length];
        for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        }
        int sum = 0;
        boolean allZero = false;
        int start = 0;
        int k = 0;
        boolean minFound = false;
        while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        }
        System.out.println(sum);
    }
} import java.util.*; util.* java class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        int length = Integer.valueOf(input1);
        input1 = sc.nextLine();
        String[] inputs = input1.split(" ");
        int[] numbers = new int[length];
        for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        }
        int sum = 0;
        boolean allZero = false;
        int start = 0;
        int k = 0;
        boolean minFound = false;
        while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        }
        System.out.println(sum);
    }
} Main public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        int length = Integer.valueOf(input1);
        input1 = sc.nextLine();
        String[] inputs = input1.split(" ");
        int[] numbers = new int[length];
        for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        }
        int sum = 0;
        boolean allZero = false;
        int start = 0;
        int k = 0;
        boolean minFound = false;
        while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        }
        System.out.println(sum);
    } main {

        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        int length = Integer.valueOf(input1);
        input1 = sc.nextLine();
        String[] inputs = input1.split(" ");
        int[] numbers = new int[length];
        for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        }
        int sum = 0;
        boolean allZero = false;
        int start = 0;
        int k = 0;
        boolean minFound = false;
        while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        }
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String input1 = sc.nextLine(); input1 sc.nextLine() sc.nextLine sc int length = Integer.valueOf(input1); length Integer.valueOf(input1) Integer.valueOf Integer input1 input1 = sc.nextLine() input1 sc.nextLine() sc.nextLine sc String[] inputs = input1.split(" "); inputs input1.split(" ") input1.split input1 " " int[] numbers = new int[length]; numbers new int[length] length for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        } int i=0; int i=0; i 0 i<length i length i++ i++ i {
            numbers[i] = Integer.valueOf(inputs[i]);
        } {
            numbers[i] = Integer.valueOf(inputs[i]);
        } numbers[i] = Integer.valueOf(inputs[i]) numbers[i] numbers i Integer.valueOf(inputs[i]) Integer.valueOf Integer inputs[i] inputs i int sum = 0; sum 0 boolean allZero = false; allZero false int start = 0; start 0 int k = 0; k 0 boolean minFound = false; minFound false while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        } allZero==false allZero false {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        } allZero=true allZero true int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE start = 0 start 0 for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            } int i = 0; int i = 0; i 0 i<length i length i++ i++ i {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            } {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            } if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                } numbers[i]!=0 numbers[i] numbers i 0 {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                } if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    } numbers[i]< min numbers[i] numbers i min {
                        min = numbers[i];
                        minFound = true;
                    } min = numbers[i] min numbers[i] numbers i minFound = true minFound true allZero=false allZero false if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                } numbers[i]==0 || i==length-1 numbers[i]==0 numbers[i] numbers i 0 i==length-1 i length-1 length 1 {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                } int end = i-1; end i-1 i 1 if(numbers[i]!=0) {
                        end = end+1;
                    } numbers[i]!=0 numbers[i] numbers i 0 {
                        end = end+1;
                    } end = end+1 end end+1 end 1 for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    } int j = start ; int j = start ; j start j<=end j end j++ j++ j {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    } {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    } numbers[j] = numbers[j]-min numbers[j] numbers j numbers[j]-min numbers[j] numbers j min if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    } minFound {
                        minFound = false;
                        sum = sum + min;
                    } minFound = false minFound false sum = sum + min sum sum + min sum min start = i+1 start i+1 i 1 min = Integer.MAX_VALUE min Integer.MAX_VALUE Integer Integer.MAX_VALUE System.out.println(sum) System.out.println System.out System System.out sum String args[] args
import java.util.*;

class Main {
    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String input1 = sc.nextLine();
        int length = Integer.valueOf(input1);
        input1 = sc.nextLine();
        String[] inputs = input1.split(" ");
        int[] numbers = new int[length];
        for(int i=0;i<length;i++) {
            numbers[i] = Integer.valueOf(inputs[i]);
        }
        int sum = 0;
        boolean allZero = false;
        int start = 0;
        int k = 0;
        boolean minFound = false;
        while(allZero==false) {
            allZero=true;
            int min = Integer.MAX_VALUE;
            start = 0;
            for(int i = 0; i<length;i++) {
                if(numbers[i]!=0)
                {
                    if( numbers[i]< min ) {
                        min = numbers[i];
                        minFound = true;
                    }
                    allZero=false;
                }

                if(numbers[i]==0 || i==length-1)
                {
                    int end = i-1;
                    if(numbers[i]!=0) {
                        end = end+1;
                    }
                    //System.out.println("min: "+ min);
                    for(int j = start ; j<=end;j++) {
                        //System.out.println(j+": "+numbers[j]);
                        numbers[j] = numbers[j]-min;
                        //System.out.println(j+": "+numbers[j]);
                        //System.out.println();
                    }

                    if(minFound) {
                        minFound = false;
                        sum = sum + min;
                    }
                    start = i+1;
                    min = Integer.MAX_VALUE;
                }

            }
        }
        System.out.println(sum);
    }
}