#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, tot1 = 0, tot2 = 0;
  scanf("%d %d", &n, &k);
  vector<int> vals;
  for (int i = 0; i < n; i++) {
    int val;
    scanf("%d", &val);
    if (val == 1) {
      tot1++;
    } else
      tot2++;
    vals.push_back(val);
  }
  int ans = -1;
  for (int i = 0; i < k; i++) {
    int cont1 = tot1, cont2 = tot2;
    for (int j = i; j < vals.size(); j += k) {
      if (vals[j] == 1) {
        cont1--;
      } else
        cont2--;
    }
    ans = max(ans, abs(cont1 - cont2));
  }
  printf("%d\n", ans);
}
