#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, k, i, j, E = 0, S = 0, e, s, mx;
  cin >> n >> k;
  vector<long long> v(n);
  for (i = 0; i < n; ++i) {
    cin >> v[i];
    if (v[i] == 1)
      ++E;
    else
      ++S;
  }
  for (i = mx = 0; i < k; ++i) {
    for (j = i, e = E, s = S; j < n; j += k) {
      if (v[j] == 1)
        --e;
      else
        --s;
    }
    mx = max(mx, abs(e - s));
  }
  cout << mx;
}
