import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  }

  public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  }
} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  }

  public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] h = new long[n]; h new long[n] n for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      h[i] = sc.nextLong();
    } {
      h[i] = sc.nextLong();
    } h[i] = sc.nextLong() h[i] h i sc.nextLong() sc.nextLong sc System.out.println(func(h)) System.out.println System.out System System.out func(h) func h String[] args args public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  } func {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  } long m = Long.MAX_VALUE; m Long.MAX_VALUE Long Long.MAX_VALUE for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    } int i = 0; int i = 0; i 0 i < h.length i h.length h h.length i++ i++ i {
      if(m > h[i]) m = h[i];
    } {
      if(m > h[i]) m = h[i];
    } if(m > h[i]) m = h[i]; m > h[i] m h[i] h i m = h[i] m h[i] h i ArrayList<Integer> list = new ArrayList<Integer>(); list new ArrayList<Integer>() for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    } int i = 0; int i = 0; i 0 i < h.length i h.length h h.length i++ i++ i {
      if(h[i] == m) list.add(i);
    } {
      if(h[i] == m) list.add(i);
    } if(h[i] == m) list.add(i); h[i] == m h[i] h i m list.add(i) list.add list i long ret = m; ret m int k1 = 0; k1 0 int k2 = list.get(0) - 1; k2 list.get(0) - 1 list.get(0) list.get list 0 1 if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    } k2 >= k1 k2 k1 {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    } long[] s = new long[k2 + 1]; s new long[k2 + 1] k2 + 1 k2 1 for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      } int i = 0; int i = 0; i 0 i <= k2 i k2 i++ i++ i {
        s[i] = h[i] - m;
      } {
        s[i] = h[i] - m;
      } s[i] = h[i] - m s[i] s i h[i] - m h[i] h i m ret += func(s) ret func(s) func s for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    } int i = 0; int i = 0; i 0 i < list.size() i list.size() list.size list i++ i++ i {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    } {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    } int t1 = list.get(i) + 1; t1 list.get(i) + 1 list.get(i) list.get list i 1 int t2 = 0; t2 0 if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      } i < list.size() - 1 i list.size() - 1 list.size() list.size list 1 {
        t2 = list.get(i + 1) - 1;
      } t2 = list.get(i + 1) - 1 t2 list.get(i + 1) - 1 list.get(i + 1) list.get list i + 1 i 1 1 {
        t2 = h.length - 1;
      } t2 = h.length - 1 t2 h.length - 1 h.length h h.length 1 if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      } t2 >= t1 t2 t1 {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      } long[] s = new long[t2 - t1 + 1]; s new long[t2 - t1 + 1] t2 - t1 + 1 t2 - t1 t2 t1 1 for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        } int j = 0; int j = 0; j 0 j < (t2 - t1 + 1) j (t2 - t1 + 1) t2 - t1 t2 t1 1 j++ j++ j {
          s[j] = h[t1 + j] - m;
        } {
          s[j] = h[t1 + j] - m;
        } s[j] = h[t1 + j] - m s[j] s j h[t1 + j] - m h[t1 + j] h t1 + j t1 j m ret += func(s) ret func(s) func s return ret; ret long[] h h public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  }

  public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  }

  public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  }
} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] h = new long[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextLong();
    }
    System.out.println(func(h));
  }

  public static long func(long[] h) {
    long m = Long.MAX_VALUE;
    for(int i = 0; i < h.length; i++) {
      if(m > h[i]) m = h[i];
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i = 0; i < h.length; i++) {
      if(h[i] == m) list.add(i);
    }
    long ret = m;
    int k1 = 0;
    int k2 = list.get(0) - 1;
    if(k2 >= k1) {
      long[] s = new long[k2 + 1];
      for(int i = 0; i <= k2; i++) {
        s[i] = h[i] - m;
      }
      ret += func(s);
    }
    for(int i = 0; i < list.size(); i++) {
      int t1 = list.get(i) + 1;
      int t2 = 0;
      if(i < list.size() - 1) {
        t2 = list.get(i + 1) - 1;
      } else {
        t2 = h.length - 1;
      }
      if(t2 >= t1) {
        long[] s = new long[t2 - t1 + 1];
        for(int j = 0; j < (t2 - t1 + 1); j++) {
          s[j] = h[t1 + j] - m;
        }
        ret += func(s);
      }
    }
    return ret;
  }
}