import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    }

    public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    }

    public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    }

    public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    }

    public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    }
} Main public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    } main {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    } Scanner console = new Scanner(System.in); console new Scanner(System.in) int n = console.nextInt(); n console.nextInt() console.nextInt console console.nextLine() console.nextLine console List<Integer> expectedHeights = new ArrayList<>(n); expectedHeights new ArrayList<>(n) List<Integer> currentHeights = new ArrayList<>(n); currentHeights new ArrayList<>(n) List<Integer> differences = new ArrayList<Integer>(n); differences new ArrayList<Integer>(n) for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        } {
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        } currentHeights.add(0) currentHeights.add currentHeights 0 differences.add(0) differences.add differences 0 expectedHeights.add(console.nextInt()) expectedHeights.add expectedHeights console.nextInt() console.nextInt console int count = 0; count 0 int beginIndex = -1; beginIndex -1 1 int endIndex = -1; endIndex -1 1 while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        } true {
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        } differences = getDifferences(expectedHeights, currentHeights) differences getDifferences(expectedHeights, currentHeights) getDifferences expectedHeights currentHeights beginIndex = getRange(differences)[0] beginIndex getRange(differences)[0] getRange(differences) getRange differences 0 endIndex = getRange(differences)[1] endIndex getRange(differences)[1] getRange(differences) getRange differences 1 if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            } beginIndex == -1 beginIndex -1 1 {
                break;
            } break; {
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            } count++ count for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                } int i = beginIndex; int i = beginIndex; i beginIndex i <= endIndex i endIndex i++ i++ i {
                    currentHeights.set(i, currentHeights.get(i) + 1);
                } {
                    currentHeights.set(i, currentHeights.get(i) + 1);
                } currentHeights.set(i, currentHeights.get(i) + 1) currentHeights.set currentHeights i currentHeights.get(i) + 1 currentHeights.get(i) currentHeights.get currentHeights i 1 System.out.println(count) System.out.println System.out System System.out count String[] args args public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    } getDifferences {
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    } int arraySize = expectedHeights.size(); arraySize expectedHeights.size() expectedHeights.size expectedHeights List<Integer> differences = new ArrayList<Integer>(arraySize); differences new ArrayList<Integer>(arraySize) for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        } int i = 0; int i = 0; i 0 i < arraySize i arraySize i++ i++ i {
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        } {
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        } differences.add(expectedHeights.get(i) - currentHeights.get(i)) differences.add differences expectedHeights.get(i) - currentHeights.get(i) expectedHeights.get(i) expectedHeights.get expectedHeights i currentHeights.get(i) currentHeights.get currentHeights i return differences; differences List<Integer> expectedHeights expectedHeights List<Integer> currentHeights currentHeights public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    } getRange {
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    } int[] answer = new int[2]; answer new int[2] 2 int beginIndex = -1; beginIndex -1 1 int endIndex = -1; endIndex -1 1 boolean isBegin = true; isBegin true for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        } int i = 0; int i = 0; i 0 i < differences.size() i differences.size() differences.size differences i++ i++ i {
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        } {
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        } int num = differences.get(i); num differences.get(i) differences.get differences i if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            } isBegin && num > 0 isBegin num > 0 num 0 {
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            } beginIndex = i beginIndex i endIndex = i endIndex i isBegin = false isBegin false if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            } !isBegin && num > 0 !isBegin isBegin num > 0 num 0 {
                endIndex = i;
            } endIndex = i endIndex i if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            } isBegin && num == 0 isBegin num == 0 num 0 {
                continue;
            } continue; if(!isBegin && num == 0){
                break;
            } !isBegin && num == 0 !isBegin isBegin num == 0 num 0 {
                break;
            } break; answer[0] = beginIndex answer[0] answer 0 beginIndex answer[1] = endIndex answer[1] answer 1 endIndex return answer; answer List<Integer> differences differences public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    }

    public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    }

    public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    }
} public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    }

    public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    }

    public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // 入力されたNを変数に格納する
        int n = console.nextInt();
        console.nextLine();

        // 理想の状態、現状、それらの差分をそれぞれリストに格納する
        List<Integer> expectedHeights = new ArrayList<>(n);
        List<Integer> currentHeights = new ArrayList<>(n);
        List<Integer> differences = new ArrayList<Integer>(n);
        for(int i = 0; i < n; i++){
            currentHeights.add(0);
            differences.add(0);
            expectedHeights.add(console.nextInt());
        }

        // 水やり操作を行う。またその回数をカウントする
        int count = 0;
        int beginIndex = -1;
        int endIndex = -1;

        while(true){
            differences = getDifferences(expectedHeights, currentHeights);
            beginIndex = getRange(differences)[0];
            endIndex = getRange(differences)[1];
            if(beginIndex == -1){
                break;
            }else{
                count++;
                for(int i = beginIndex; i <= endIndex; i++){
                    currentHeights.set(i, currentHeights.get(i) + 1);
                }
            }
        }
        System.out.println(count);
    }

    public static List<Integer> getDifferences(List<Integer> expectedHeights, List<Integer> currentHeights){
        int arraySize = expectedHeights.size();
        List<Integer> differences = new ArrayList<Integer>(arraySize);

        for(int i = 0; i < arraySize; i++){
            differences.add(expectedHeights.get(i) - currentHeights.get(i));
        }
        return differences;
    }

    public static int[] getRange(List<Integer> differences){
        int[] answer = new int[2];
        int beginIndex = -1;
        int endIndex = -1;
        boolean isBegin = true;

        for(int i = 0; i < differences.size(); i++){
            int num = differences.get(i);
            if(isBegin && num > 0){
                beginIndex = i;
                endIndex = i;
                isBegin = false;
            }else if(!isBegin && num > 0){
                endIndex = i;
            }else if(isBegin && num == 0){
                continue;
            }else if(!isBegin && num == 0){
                break;
            }
        }

        answer[0] = beginIndex;
        answer[1] = endIndex;
        return answer;
    }
}
