#include <bits/stdc++.h>
int main() {
  int n, k;
  std::cin >> n >> k;
  int *workSum = new int[k];
  for (int i = 0; i < k; i++) {
    workSum[i] = 0;
  }
  int bigSum = 0;
  int p;
  for (int i = 0; i < n; i++) {
    std::cin >> p;
    workSum[i % k] += p;
    bigSum += p;
  }
  int minworkSum = workSum[0];
  int maxworkSum = workSum[0];
  for (int i = 1; i < k; i++) {
    if (minworkSum > workSum[i]) {
      minworkSum = workSum[i];
    } else if (maxworkSum < workSum[i]) {
      maxworkSum = workSum[i];
    }
  }
  int res = abs(bigSum - minworkSum);
  if (abs(bigSum - maxworkSum) > abs(bigSum - minworkSum)) {
    res = abs(bigSum - maxworkSum);
  }
  std::cout << res;
  delete[] workSum;
}
