#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  long long a[n + 1];
  long long cure = 0, curs = 0;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    cin >> a[i];
    if (a[i] == 1)
      cure++;
    else
      curs++;
  }
  long long ans = 0;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    long long e = 0, s = 0;
    if (i - k >= 1) break;
    for (long long j = i; j <= n; j += k) {
      if (a[j] == 1)
        e++;
      else
        s++;
    }
    long long newe = cure - e;
    long long news = curs - s;
    ans = max(ans, abs(newe - news));
  }
  cout << ans;
}
