#include #include #include //#define SHOWARRY(a) {printf("---"#a"---\n");for(int i=0;i> n; std::vector v(n, 0); std::vector check(n, 0); for (int i = 0; i < n; ++i) { std::cin >> v[i]; } int sum = 0; switch (n) { case 1: sum = v[0]; break; case 2: sum = (v[0] < v[1]) ? v[1] : v[0]; break; case 3: sum = (v[0] + v[2] < v[1]) ? v[1] : v[0] + v[2]; break; default: for (int i = 0; i < n - 3; ++i) { int tmp1 = v[i] + v[i + 2]; int tmp2 = v[i] + v[i + 3]; int tmp3 = v[i + 1] + v[i + 3]; int tmp = std::max({ tmp1, tmp2, tmp3 }); if (tmp == tmp1) { ++check[i]; ++check[i + 2]; } else if (tmp == tmp2) { ++check[i]; ++check[i + 3]; } else if (tmp == tmp3) { ++check[i + 1]; ++check[i + 3]; } }//SHOWARRY(check); for (int i = 0; i < n - 1; ++i) { if (check[i] > 0 && check[i + 1] > 0) { if (v[i] < v[i + 1]) { check[i] = 0; } else { check[i + 1] = 0; } } }//SHOWARRY(check); for (int i = 0; i < n; ++i) { if (check[i] > 0) { sum += v[i]; } } break; } std::cout << sum << "\n"; return 0; }