#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &item) {
  out << '(' << item.first << ", " << item.second << ')';
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const auto &item : v) out << item << ' ';
  return out;
}
int main() {
  int n, k, diff = 0, *a, pos = 0, neg = 0;
  cin >> n >> k;
  a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      pos++;
    } else {
      neg++;
    }
  }
  for (int j = 0; j < k; j++) {
    int pos2 = pos, neg2 = neg;
    for (int i = j; i < n; i += k) {
      if (a[i] == 1) {
        pos2--;
      } else {
        neg2--;
      }
    }
    diff = max(diff, abs(neg2 - pos2));
  }
  cout << diff << endl;
}
