#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int i, j, k, m, n, a[11400], sum = 0, t1, t2, o, l, k1 = 0, k0 = 0;
  char s[10500];
  pair<long long int, long long int> p[10500];
  long double x, y;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      k1++;
    else
      k0++;
  }
  long long int ans = 0;
  for (j = 0; j < k; j++) {
    t1 = k1;
    t2 = k0;
    for (i = j; i < n; i += k) {
      if (a[i] == 1)
        t1--;
      else
        t2--;
    }
    ans = max(ans, abs(t1 - t2));
  }
  cout << ans << endl;
  return 0;
}
