Source Code
#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
#include<vector>
#include<iomanip>
#define OmarNasr {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
using namespace std;
long long a[200005],sum1[200005];
int n, k;
int main()
{
    OmarNasr;
    long long sum1 = 0,mn=1e15;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum1 += a[i];
    }
    if (k == 1) {
        cout << 0;
    }
    else {
        int x = 0;
        for (int i = 0; i < n-1; i++) {
            x = i;
            long long sum = 0; int ind = x, b = 1;
            while (ind < n) {
                sum += a[ind];
                ind = x + b * k;
                b++;
            }
            mn = min(mn, sum1-sum);
        }
    }
    cout << mn;
    return 0;
}
Copy
Offer OmarNasr
GNU G++17
1 ms
376 KB
Wrong Answer