Source Code
#include<bits/stdc++.h>
#include<cstdlib>
#define ll long long
#define ln length()
#define sz size()
#define ff first
#define ss second
#define pq priority_queue<ll,vector<ll>,greater<ll>>
#define all(v) v.begin(),v.end()
#define FS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ul unsigned long long
#define tc ll tt; cin>>tt; while(tt--)
#define ld long double
#define ln length()
#define el "\n"
#define ts(c) to_string(c)

using namespace std;

int main()
{
    FS
    {
        ll n,k,res=INT_MAX,sum,sum1=0;
        cin>>n>>k;
        vector<ll>v(n);
        for (int i=0;i<n;i++){
            cin>>v[i];
            sum1 += v[i];
        }
        for (ll i=0;i<k;i++){
            sum = sum1;
            for (ll j=0;j<n;j++){
                if (i+k*j > n-1) break;
                sum -= v[i+k*j];
            }
            res = min(res,sum);
        }
        cout<<res<<el;
    }
}
Copy
Offer heba.daraghmeh
GNU G++17
3 ms
376 KB
Accepted