Source Code
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long arr[100005];
int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long N, K, m = 1000000000000000000;

    cin >> N >> K;

    for (int i = 1; i <= N; i++)
    {
        cin >> arr[i];
    }

    for (int i = 1; i <= N; i++)
    {
        arr[i] += arr[i - 1];
    }

    for (int i = 1; i <= N; i++)
    {
        long long s = 0, l = 0, r = 0;
        l = i, r = i + K, s = arr[l - 1];
        while (r <= N)
        {
            s += (arr[r - 1] - arr[l]);
            l = r;
            r += K;

        }
        if (r > N)
        {
            r -= K;
            s += arr[N] - arr[r];
        }
        m = min(m, s);
    }

    cout << m;
}
Copy
Offer OmarNasr
GNU G++17
2 ms
412 KB
Accepted