import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    }

} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    }

} Main public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    } main {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int ans = 0; ans 0 int N = in.nextInt(); N in.nextInt() in.nextInt in int[] values = new int[N]; values new int[N] N for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            values[i] = in.nextInt();
        } {
            values[i] = in.nextInt();
        } values[i] = in.nextInt() values[i] values i in.nextInt() in.nextInt in int[] valuesWithPadding = new int[N+2]; valuesWithPadding new int[N+2] N+2 N 2 for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            valuesWithPadding[i+1] = values[i];
        } {
            valuesWithPadding[i+1] = values[i];
        } valuesWithPadding[i+1] = values[i] valuesWithPadding[i+1] valuesWithPadding i+1 i 1 values[i] values i while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        } true {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        } int count = 0; count 0 for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            } int i = 1; int i = 1; i 1 i < N+2 i N+2 N 2 i++ i++ i {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            } {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            } if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                } valuesWithPadding[i] != 0 valuesWithPadding[i] valuesWithPadding i 0 {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                } if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    } valuesWithPadding[i - 1] == 0 valuesWithPadding[i - 1] valuesWithPadding i - 1 i 1 0 {
                        count++;
                    } count++ count if (count == 0) break; count == 0 count 0 break; for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            } int i = 0; int i = 0; i 0 i < N+2 i N+2 N 2 i++ i++ i {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            } {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            } if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                } valuesWithPadding[i] != 0 valuesWithPadding[i] valuesWithPadding i 0 {
                    valuesWithPadding[i] -= 1;
                } valuesWithPadding[i] -= 1 valuesWithPadding[i] valuesWithPadding i 1 ans += count ans count System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    }

} public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    }

} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int ans = 0;
        int N = in.nextInt();
        int[] values = new int[N];

        for (int i = 0; i < N; i++) {
            values[i] = in.nextInt();
        }
        // 先頭と末尾を0で埋める
        int[] valuesWithPadding = new int[N+2];
        for (int i = 0; i < N; i++) {
            valuesWithPadding[i+1] = values[i];
        }

        while (true) {
            
            // 0でない数字の塊の数を数える
            int count = 0;
            for (int i = 1; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    if (valuesWithPadding[i - 1] == 0) {
                        count++;
                    }
                }
            }
            if (count == 0) break;
            for (int i = 0; i < N+2; i++) {
                if (valuesWithPadding[i] != 0) {
                    valuesWithPadding[i] -= 1;
                }
            }
            ans += count;
        }
        System.out.println(ans);

    }

}
