#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int inf = 0xffffff;
int a[MAX];
int vis[MAX];
int maxx = -1;
int main() {
  int n, k;
  int e = 0, s = 0;
  int ans = 0;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      e++;
    } else {
      s++;
    }
  }
  for (int i = 1; i <= k; i++) {
    int e1 = 0;
    int s1 = 0;
    for (int j = i; j <= n; j += k) {
      if (a[j] == 1) {
        e1++;
      } else {
        s1++;
      }
    }
    ans = abs((s - s1) - (e - e1));
    maxx = max(maxx, ans);
  }
  cout << maxx << endl;
  return 0;
}
