import java.util.*;

public class Main{

    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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    }

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

    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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    }

} Main 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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    } 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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    } 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 int l = 0 l 0 j j min min answer = 0; answer 0 while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        } l < n l n {
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        } while(l < n && h[l] == 0){
                l++;
            } l < n && h[l] == 0 l < n l n h[l] == 0 h[l] h l 0 {
                l++;
            } l++ l if(l == n) break; l == n l n break; j = l j l min = h[l] min h[l] h l while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            } j < n && h[j] != 0 j < n j n h[j] != 0 h[j] h j 0 {
                min = Math.min(h[j], min);
                j++;
            } min = Math.min(h[j], min) min Math.min(h[j], min) Math.min Math h[j] h j min j++ j for(int k = 0; k < j; k++){
                h[k] -= min;
            } int k = 0; int k = 0; k 0 k < j k j k++ k++ k {
                h[k] -= min;
            } {
                h[k] -= min;
            } h[k] -= min h[k] h k min answer += min answer min System.out.println(answer) System.out.println System.out System System.out answer String args[] args public class Main{

    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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    }

} public class Main{

    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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    }

} Main
import java.util.*;

public class Main{

    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();
        }

        int l = 0, j, min, answer = 0;
        while(l < n){
            while(l < n && h[l] == 0){
                l++;
            }
            if(l == n) break;
            j = l;
            min = h[l];
            while(j < n && h[j] != 0){
                min = Math.min(h[j], min);
                j++;
            }
            for(int k = 0; k < j; k++){
                h[k] -= min;
            }
            answer += min;
        }

        // 出力
        System.out.println(answer);

    }

}