#define ONLINE_JUDGE #if defined(ONLINE_JUDGE) #define _SECURE_SCL 0 #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(__GNUC__) #else // !defined(__GNUC__) #define _CrtDbgBreak() __builtin_trap() #endif // !defined(__GNUC__) #if defined(ONLINE_JUDGE) #define LOCAL_TEST 0 #else #define LOCAL_TEST 1 #endif #if LOCAL_TEST struct AssertsCounter { AssertsCounter(): counter(0) {} ~AssertsCounter() { std::cerr << std::endl << "DIAG: " << (counter == 0 ? "OK" : "FAIL!!!") << " Asserts count: " << counter << std::endl; } void Increment() { counter++; } uint32_t counter; }; AssertsCounter g_assertsCounter; #define LOCAL_ASSERT(expr) { if (!(expr)) {std::cerr << "ASSERT FAILED (" << __LINE__ << "): '" << #expr << "'" << std::endl; g_assertsCounter.Increment(); _CrtDbgBreak(); } } #define LOCAL_ASSERT_EQ(expr1, expr2) { if ((expr1) != (expr2)) {std::cerr << "ASSERT FAILED (" << __LINE__ << "): '" << #expr1 << "' == '" << #expr2 << "' (" << (expr1) << " vs " << (expr2) << "')" << std::endl; g_assertsCounter.Increment(); _CrtDbgBreak(); } } #else volatile bool isLocalTestEnabled = 0; #define LOCAL_ASSERT(expr) #define LOCAL_ASSERT_EQ(expr1, expr2) #endif bool g_isLocalPrintEnabled = (bool)(LOCAL_TEST); #define LOCAL_PRINT() if (!g_isLocalPrintEnabled) { } else std::cerr // << .. << .. typedef std::string string8_t; typedef long double ld_t; typedef std::vector vector_size_t; typedef std::vector vector_uint8_t; typedef std::vector vector_int32_t; typedef std::vector vector_uint32_t; typedef std::vector vector_int64_t; typedef std::vector vector_uint64_t; typedef std::vector vector_string8_t; typedef std::vector vector_2d_size_t; typedef std::vector vector_2d_int32_t; typedef std::vector vector_2d_uint32_t; typedef std::vector vector_2d_int64_t; typedef std::vector vector_2d_uint64_t; typedef std::set set_size_t; typedef std::set set_int32_t; typedef std::set set_uint32_t; typedef std::set set_int64_t; typedef std::set set_uint64_t; typedef std::set set_string8_t; typedef std::multiset multiset_size_t; typedef std::multiset multiset_string8_t; // Auxiliary functions definition // template void UpdateMin(T& a, const T b) {a = std::min(a, b);} template void UpdateMax(T& a, const T b) {a = std::max(a, b);} const ld_t Pi = std::atan(1.0L) * 4.0L; static const ld_t Eps = 1.0e-09; template bool IsEqual(const T a, const T b) { return std::abs(a - b) < Eps; } template bool IsGreater(const T a, const T b) { return a > b + Eps; } template bool IsLess(const T a, const T b) { return a + Eps < b; } template bool IsGreaterEqual(const T a, const T b) { return !IsLess(a, b); } template bool IsLessEqual(const T a, const T b) { return !IsGreater(a, b); } template string8_t ToStr(const T& val) { std::ostringstream ostr; ostr << val; return ostr.str(); } template bool FromStr(const string8_t& str, T& val) {std::istringstream istr(str); istr >> val; return !!istr; } template std::istream& operator>>(std::istream& ist, std::vector& data) { LOCAL_ASSERT(!!ist); for (size_t i = 0; i < data.size(); i++) { ist >> data[i]; } return ist; } template T Read(std::istream& ist) { LOCAL_ASSERT(!!ist); T val; ist >> val; return val; } template std::ostream& operator<<(std::ostream& ost, const std::vector& data) { for (size_t i = 0; i < data.size(); i++) { if (i != 0) { ost << ' '; } ost << data[i]; } return ost; } #if defined(ONLINE_JUDGE) template class StopWatch { }; #else #include #include library::random::Rand g_rnd; #endif bool GetSum(const vector_int64_t& c, const size_t k, const size_t start, int64_t& ans) { int64_t sum = 0; size_t lastIndex = start; for (size_t i = start; i < c.size(); i += 2 * k + 1) { sum += c[i]; lastIndex = i; } ans = sum; if (lastIndex + k >= c.size() - 1) return true; return false; } int64_t GetAns(const vector_int64_t& c, const size_t k) { int64_t minAns = std::numeric_limits::max(); for (size_t i = 0; i <= k && i < c.size(); i++) { int64_t ans = 0; if (GetSum(c, k, i, ans)) { UpdateMin(minAns, ans); } } return minAns; } bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) { StopWatch<1> sw; (void)sw; // size_t n; ist >> n; if (multipleTestMode && !ist) return false; // LOCAL_PRINT() << std::endl << "Next test" << std::endl; int32_t k; ist >> k; vector_int64_t c(n); ist >> c; const int64_t ans1 = GetAns(c, k); std::reverse(c.begin(), c.end()); const int64_t ans2 = GetAns(c, k); const int64_t ans = std::min(ans1, ans2); ost << ans << std::endl; return multipleTestMode; } #if LOCAL_TEST void Test() { using namespace library::random; const auto genVector = GenFactory::CreateGenVector(GenRange(2, 10), GenRange(-100, +200)); for (size_t t = 0; t < 10; t++) { const vector_int32_t data = genVector(); // size_t ans = GetAns(data); } } #endif int main(int argc, const char *argv[]) { #if LOCAL_TEST Test(); #endif std::ios_base::sync_with_stdio(false); std::istream* ist = &std::cin; std::ostream* ost = &std::cout; std::unique_ptr fileInput; if (argc > 1) { fileInput.reset(new std::ifstream(argv[1])); if (!(*fileInput)) { std::cout << "File not found: " << argv[1] << std::endl; } ist = fileInput.get(); } #if defined(ONLINE_JUDGE) Solve(*ist, *ost, false); #else while(Solve(*ist, *ost, true)) {}; #endif }