#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, s = 0, e = 0;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1)
      e += 1;
    else
      s += 1;
  }
  int g = 0, max = 0;
  while (g < k) {
    int x = e, y = s;
    for (int i = g; i < n; i += k) {
      if (arr[i] == 1)
        x = x - 1;
      else
        y = y - 1;
    }
    if (abs(x - y) >= max) max = abs(x - y);
    g++;
  }
  cout << max << endl;
}
