#include #include #include #include constexpr long long neg_inf = std::numeric_limits::min() / 2; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::size_t n, k; std::cin >> n >> k; std::vector a(n); for (auto &e : a) std::cin >> e; std::vector pd(k + 1, neg_inf); pd[1] = a[0]; for (std::size_t i = 1; i < n; ++i) { std::vector dp(k + 1, neg_inf); dp[0] = pd[0]; for (std::size_t j = 1; j <= k; ++j) { dp[j] = std::max(dp[j], pd[j] + a[i] * static_cast(j)); if (j < k) { dp[j + 1] = std::max(dp[j + 1], pd[j] + a[i] * static_cast(j + 1)); } else { dp[0] = std::max(dp[0], pd[k]); } } pd.swap(dp); } std::cout << std::max(pd[0], pd[k]) << std::endl; return 0; }