Source Code
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;

    long long ar[n];
    long long sum=0;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        sum+=ar[i];
    }
    if(k==1){
        cout<<0<<endl;
        return 0;
    }
    vector<long long>v;
    long long maxx=0;
    for(int i=0;i<n;i++){
        v.push_back(0);
        for(int j=i;j<n;j+=k){
            v[i]+=ar[j];
        }
        if(i==0)
            maxx=v[i];
        else if(v[i]>maxx){
            maxx=v[i];
        }
    }
    sum-=maxx;
    cout<<sum<<endl;


    return 0;
}
Copy
Offer YousefShaheen
GNU G++17
2 ms
296 KB
Accepted