import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    } main {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] h = new int[N]; h new int[N] N int max = 0; max 0 for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        } int i= 0; int i= 0; i 0 i<N i N i++ i++ i {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        } {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        } h[i] = Integer.parseInt(sc.next()) h[i] h i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc if(max < h[i]) {
                max = h[i];
            } max < h[i] max h[i] h i {
                max = h[i];
            } max = h[i] max h[i] h i int count = 0; count 0 while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        } max > 0 max 0 {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        } if(N == 1) {
                count = h[0];
                break;
            } N == 1 N 1 {
                count = h[0];
                break;
            } count = h[0] count h[0] h 0 break; for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            } int i=1; int i=1; i 1 i<N i N i++ i++ i {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            } {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            } if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                } i<N-1 i N-1 N 1 {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                } if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    } h[i-1] == max h[i-1] h i-1 i 1 max {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    } h[i-1]-- h[i-1] h i-1 i 1 if(h[i] != max) {
                            count++;
                        } h[i] != max h[i] h i max {
                            count++;
                        } count++ count {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                } if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    } h[i-1] == max h[i-1] h i-1 i 1 max {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    } h[i-1]-- h[i-1] h i-1 i 1 count++ count if(h[i] == max) {
                            h[i]--;
                        } h[i] == max h[i] h i max {
                            h[i]--;
                        } h[i]-- h[i] h i {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    } if(h[i] == max) {
                            count++;
                            h[i]--;
                        } h[i] == max h[i] h i max {
                            count++;
                            h[i]--;
                        } count++ count h[i]-- h[i] h i max-- max System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    }
} Main
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int[] h = new int[N];
        int max = 0;
        for(int i= 0; i<N; i++) {
            h[i] = Integer.parseInt(sc.next());
            if(max < h[i]) {
                max = h[i];
            }
        }
        int count = 0;
        while(max > 0) {
            if(N == 1) {
                count = h[0];
                break;
            }
            for(int i=1; i<N; i++) {
                if(i<N-1) {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        if(h[i] != max) {
                            count++;
                        }
                    }
                }else {
                    if(h[i-1] == max) {
                        h[i-1]--;
                        count++;
                        if(h[i] == max) {
                            h[i]--;
                        }
                    }else {
                        if(h[i] == max) {
                            count++;
                            h[i]--;
                        }
                    }
                }
            }
            max--;
        }
        System.out.println(count);
        
    }
}