#include using namespace std; #define INF 9999999999999999 #define rep(i,m,n) for(long long i = m;i < n;i++) #define rrep(i,m,n) for(long long i=m-1;i>=n;i--) #define chmin(a, b) a = (((a)>(b)) ? (b) : (a)) int main() { long long N, K, sum = 0, num = INF; cin >> N >> K; assert(1 <= N && 1 <= K && K <= N && N <= 200000); vector A(N); vector CM(N, INF); //後ろからA[i]の累積min rep(i, 0, N) { cin >> A[i]; assert(1 <= A[i] && A[i] <= 1000000000); } if(!(K - 1)) cout << A[0] << endl; //K=1なら何もしないのが最適 else { CM[N - 1] = A[N - 1]; rrep(i, N-1, 0) CM[i] = min(CM[i + 1], A[i]); priority_queue pque; //max{A[i+1,min(N,i+X)]} rrep(i, N, K - 1) { //前から見て最初にK番目になるindex long long X = (i + K - 2) / (K - 1); //前から見てi番目が最初にK番目になるとき,部分列の個数の最小値 if(!(X - 1)) chmin(num, A[i]); //部分列の個数が1つならこの時点で確定 else if(K * X <= N) { while(pque.size() - X + 1) { sum -= pque.top(); pque.pop(); } if(CM[K * X - 1] <= pque.top()) chmin(num, A[i] + sum); else chmin(num, A[i] + sum + CM[K * X - 1] - pque.top()); } pque.push(A[i]); sum += A[i]; } cout << num << endl; } }