package yukicoder; import java.util.Scanner; public class No45 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); int[] sara = new int[N]; for (int i = 0; i < N; i++) { sara[i] = sc.nextInt(); } int sum = 0; boolean[] b2 = new boolean[N]; boolean end = false; while(end == false){ for(int i = 0; i < N; i++){ if(b2[i] == true) continue; boolean b = false; if(i + 1 < N){ if(sara[i] < sara[i + 1] && b2[i + 1] == false){ b = true; } } if(i + 2 < N){ if(sara[i] < sara[i + 2] && b2[i + 2] == false){ b = true; } } if(b==false){ sum += sara[i]; if(i-1 >= 0){ b2[i - 1] = true; } b2[i] = true; if(i + 1 < N){ b2[i + 1] = true; } i++; } } for(int i = 0; i < N; i++){ if(b2[i] == false){ end = false; break; } end = true; } if(end == true){ System.out.println(sum); } } } }