#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, ans, final = 0, play = 0, study = 0, big, small, i, j;
  cin >> n >> m;
  int a[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      study++;
    } else {
      play++;
    }
  }
  for (i = 1; i <= m; i++) {
    j = i;
    big = study;
    small = play;
    while (j <= n) {
      if (a[j] == 1) {
        big--;
      } else {
        small--;
      }
      j += m;
    }
    ans = abs(big - small);
    if (ans > final) {
      final = ans;
    }
  }
  cout << final;
}
