inlinevoidmain(){ read(n); a.push_back(INF); for (int i = 1, x; i <= n; ++i) read(x), a.push_back(x); a.push_back(INF); int i, j, sum; while (n-- > 1) { for (i = 1; i <= n; ++i) if (a[i-1] < a[i+1]) break; sum = a[i-1] + a[i]; for (j = i - 1; ~j; --j) if (a[j] > sum) break; a.erase(a.begin() + i - 1); a.erase(a.begin() + i - 1); a.insert(a.begin() + j + 1, sum); ans += sum; } printf("%d\n", ans); } }