#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int arr[n + 1];
  bool brr[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> arr[i];
    brr[i] = true;
  }
  int res = -1;
  for (int b = 1; b <= k; b++) {
    int temp = 0;
    int in = b;
    while (in <= n + 1) {
      brr[in] = false;
      temp++;
      in = b + temp * k;
    }
    int e = 0, r = 0;
    for (int i = 1; i < n + 1; i++) {
      if (brr[i]) {
        if (arr[i] == 1)
          e++;
        else
          r++;
      }
    }
    if (abs(e - r) > res) res = abs(e - r);
    for (int i = 1; i < n + 1; i++) brr[i] = true;
  }
  cout << res << endl;
}
