Source Code
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n,k;
    long long sum=0;
    cin>>n>>k;
    int a[100010];
    for (int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    long long sumsale=INT_MIN;
    for (int i=0;i<n;i++)
    {
        long long sumtemp=0;
        for (int j=i;j<n;j+=k)
        {
            sumtemp+=a[j];
        }
        if (sumtemp>sumsale)
        {
            sumsale=sumtemp;
        }
    }
    cout<<sum-sumsale;
    return 0;
}
Copy
Offer Mohamedmaher
GNU G++17
14 ms
1.1 MB
Accepted