#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  int tt;
  int n, k;
  cin >> n >> k;
  int arr[n + 1];
  for (int i = 1; i <= n; i++) cin >> arr[i];
  int maxn = 0;
  for (int b = 0; b < k; b++) {
    int e = 0;
    int s = 0;
    for (int i = 1; i <= n; i++) {
      if (i % k == b) continue;
      if (arr[i] == 1)
        e++;
      else
        s++;
    }
    maxn = max(maxn, abs(s - e));
  }
  cout << maxn << endl;
}
