#include <bits/stdc++.h>
using namespace std;
const int manx = 200400;
const long long mod = 998244353;
int n, k, a[manx];
int main() {
  while (~scanf("%d%d", &n, &k)) {
    int ans = 0, x, y;
    x = y = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i] == 1)
        x++;
      else
        y++;
    }
    int X, Y;
    for (int i = 1; i <= k; i++) {
      X = Y = 0;
      for (int j = i; j <= n; j += k) {
        if (a[j] == 1)
          X++;
        else
          Y++;
      }
      ans = max(ans, abs((x - X) - (y - Y)));
    }
    printf("%d\n", ans);
  }
  return 0;
}
