#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, maxm = INT_MIN, e = 0, s = 0;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      e++;
    else
      s++;
  }
  for (int i = 0; i < k; i++) {
    int p = e, q = s;
    for (int j = i; j < n; j += k) {
      if (a[j] == 1)
        p--;
      else
        q--;
    }
    if (abs(p - q) > maxm) maxm = abs(p - q);
  }
  cout << maxm;
  return 0;
}
