import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    int count = 0;
    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    }

    void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    }

    // Main
    public static void main(String[] args) {
        new Main().solve();
    }
} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {

    int count = 0;
    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    }

    void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    }

    // Main
    public static void main(String[] args) {
        new Main().solve();
    }
} Main int count = 0; count 0 void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    } solve {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc List<Integer> heights = new ArrayList<>(); heights new ArrayList<>() for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            heights.add(sc.nextInt());
        } {
            heights.add(sc.nextInt());
        } heights.add(sc.nextInt()) heights.add heights sc.nextInt() sc.nextInt sc calcCount(heights) calcCount heights System.out.print(count) System.out.print System.out System System.out count void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    } calcCount {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    } if (heights.size() == 0) {
            return;
        } heights.size() == 0 heights.size() heights.size heights 0 {
            return;
        } return; if (heights.size() == 1) {
            count += heights.get(0);
            return;
        } heights.size() == 1 heights.size() heights.size heights 1 {
            count += heights.get(0);
            return;
        } count += heights.get(0) count heights.get(0) heights.get heights 0 return; int minHeight = Collections.min(heights); minHeight Collections.min(heights) Collections.min Collections heights if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        } minHeight > 0 minHeight 0 {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        } for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            } int i = 0; int i = 0; i 0 i < heights.size() i heights.size() heights.size heights i++ i++ i {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            } {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            } int height = heights.get(i); height heights.get(i) heights.get heights i heights.set(i, height - minHeight) heights.set heights i height - minHeight height minHeight count += minHeight count minHeight List<Integer> subHeights = new ArrayList<>(); subHeights new ArrayList<>() for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        } int i = 0; int i = 0; i 0 i < heights.size() i heights.size() heights.size heights i++ i++ i {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        } {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        } int height = heights.get(i); height heights.get(i) heights.get heights i if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            } height == 0 height 0 {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            } if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                } subHeights.size() != 0 subHeights.size() subHeights.size subHeights 0 {
                    calcCount(subHeights);
                    subHeights.clear();
                } calcCount(subHeights) calcCount subHeights subHeights.clear() subHeights.clear subHeights if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            } i == (heights.size() - 1) i (heights.size() - 1) heights.size() heights.size heights 1 {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            } subHeights.add(height) subHeights.add subHeights height calcCount(subHeights) calcCount subHeights subHeights.clear() subHeights.clear subHeights {
                subHeights.add(height);
            } subHeights.add(height) subHeights.add subHeights height List<Integer> heights heights // Main
    public static void main(String[] args) {
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args public class Main {

    int count = 0;
    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    }

    void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    }

    // Main
    public static void main(String[] args) {
        new Main().solve();
    }
} public class Main {

    int count = 0;
    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    }

    void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    }

    // Main
    public static void main(String[] args) {
        new Main().solve();
    }
} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    int count = 0;
    void solve() {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<Integer> heights = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            heights.add(sc.nextInt());
        }
        calcCount(heights);
        System.out.print(count);
    }

    void calcCount(List<Integer> heights) {
        // 脱出条件
        if (heights.size() == 0) {
            return;
        }
        if (heights.size() == 1) {
            count += heights.get(0);
            return;
        }

        int minHeight = Collections.min(heights);
        if (minHeight > 0) {
            // Integer 型のまま扱えば、もっと簡単にできるかも。
            for (int i = 0; i < heights.size(); i++) {
                int height = heights.get(i);
                heights.set(i, height - minHeight);
            }
            count += minHeight;
        }

        List<Integer> subHeights = new ArrayList<>();
        for (int i = 0; i < heights.size(); i++) {
            int height = heights.get(i);
            // 0 が来て一区切りなので、ここまでをcalcCount()にかける。
            if (height == 0) {
                if (subHeights.size() != 0) {
                    calcCount(subHeights);
                    subHeights.clear();
                }
            }
            // 部分集合の最後まで来たので、ここまでをcalcCount()にかける。
            else if (i == (heights.size() - 1)) {
                subHeights.add(height);
                calcCount(subHeights);
                subHeights.clear();
            }
            else {
                subHeights.add(height);
            }
        }
    }

    // Main
    public static void main(String[] args) {
        new Main().solve();
    }
}