#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  int arr[n], p = 0, q = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1) {
      p++;
    } else {
      q++;
    }
  }
  for (int i = 0; i < n; i++) {
    int x = 0, y = 0;
    for (int j = i; j < n; j += k) {
      if (arr[j] == 1) {
        x++;
      } else {
        y++;
      }
    }
    for (int j = i - k; j >= 0; j -= k) {
      if (arr[j] == 1) {
        x++;
      } else {
        y++;
      }
    }
    int t1 = p - x;
    int t2 = q - y;
    v.push_back(abs(t1 - t2));
  }
  sort(v.begin(), v.end());
  cout << v[v.size() - 1] << endl;
  return 0;
}
