import java.util.*;

public class Main {

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

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    }

} 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[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    }

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

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int ans = 0; ans 0 for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        } {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        } int mm = 0; mm 0 int idx = 0; idx 0 for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            } int j = 0; int j = 0; j 0 j < n j n j++ j++ j {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            } {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            } if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                } (mm > a[j] || mm == 0) && a[j] != 0 (mm > a[j] || mm == 0) mm > a[j] mm a[j] a j mm == 0 mm 0 a[j] != 0 a[j] a j 0 {
                    mm = a[j];
                    idx = j;
                } mm = a[j] mm a[j] a j idx = j idx j if (mm == 0)
                break; mm == 0 mm 0 break; ans += mm ans mm int idl = 0; idl 0 int idr = 0; idr 0 for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            } int j = idx; int j = idx; j idx j >= 0 j 0 j-- j-- j {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            } {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            } if (a[j] == 0) {
                    idl = j;
                    break;
                } a[j] == 0 a[j] a j 0 {
                    idl = j;
                    break;
                } idl = j idl j break; if (j == 0) {
                    idl = -1;
                    break;
                } j == 0 j 0 {
                    idl = -1;
                    break;
                } idl = -1 idl -1 1 break; for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            } int j = idx; int j = idx; j idx j < n j n j++ j++ j {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            } {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            } if (a[j] == 0) {
                    idr = j;
                    break;
                } a[j] == 0 a[j] a j 0 {
                    idr = j;
                    break;
                } idr = j idr j break; if (j == n - 1) {
                    idr = n;
                    break;
                } j == n - 1 j n - 1 n 1 {
                    idr = n;
                    break;
                } idr = n idr n break; for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            } int j = idx; int j = idx; j idx j < idr j idr j++ j++ j {
                a[j] -= mm;
            } {
                a[j] -= mm;
            } a[j] -= mm a[j] a j mm for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            } int j = idx - 1; int j = idx - 1; j idx - 1 idx 1 j > idl j idl j-- j-- j {
                a[j] -= mm;
            } {
                a[j] -= mm;
            } a[j] -= mm a[j] a j mm 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 sc = new Scanner(System.in);

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    }

} public class Main {

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

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    }

} Main
import java.util.*;

public class Main {

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

        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int mm = 0;
            int idx = 0;
            for (int j = 0; j < n; j++) {
                if ((mm > a[j] || mm == 0) && a[j] != 0) {
                    mm = a[j];
                    idx = j;
                }

            }
            if (mm == 0)
                break;

            ans += mm;

            int idl = 0;
            int idr = 0;
            for (int j = idx; j >= 0; j--) {
                if (a[j] == 0) {
                    idl = j;
                    break;
                }
                if (j == 0) {
                    idl = -1;
                    break;
                }
            }
            for (int j = idx; j < n; j++) {
                if (a[j] == 0) {
                    idr = j;
                    break;
                }
                if (j == n - 1) {
                    idr = n;
                    break;
                }
            }

            for (int j = idx; j < idr; j++) {
                a[j] -= mm;
            }
            for (int j = idx - 1; j > idl; j--) {
                a[j] -= mm;
            }

        }

        System.out.println(ans);
    }

}
