Source Code
#include<bits/stdc++.h>
using namespace std;

void thabit(){
    int n, k;
    cin>>n>>k;
    int a[n];
    long long p[n]{};
    long long sum = 0;
    for(int i = 0 ; i<n; i++){
        cin>>a[i];
        sum+=a[i];
        for(int j = i; j>=0 ; j-=k){
            p[j] += a[i];
        }
    }
    long long mx = 0;
    for(int i = 0 ; i<n ; i++)
        mx = max(mx, p[i]);
    cout<<sum - mx;
}

int main(){    
    
    int t = 1;
    while(t--){
        thabit();
    }
}
Copy
Offer hashemtb
GNU G++17
12 ms
328 KB
Accepted