#include <bits/stdc++.h>
using namespace std;
int size_ = 0;
vector<long long int> prime;
void seive() {
  bool hash[100001];
  for (long long int i = 0; i < 100001; i++) hash[i] = 1;
  for (long long int i = 2; i * i <= 100000; i++) {
    if (hash[i] == 1) {
      for (long long int p = i * 2; p <= 100000; p = p + i) {
        hash[p] = 0;
      }
    }
    if (i != 2) i++;
  }
  for (long long int i = 2; i < 100001; i++) {
    if (hash[i]) {
      prime.push_back(i);
      size_++;
    }
  }
  return;
}
void print(vector<long long int> &v) {
  long long int n = v.size();
  for (long long int i = 0; i < n; i++) cout << v[i];
  cout << endl;
  return;
}
void print1(vector<pair<long long int, long long int> > &v) {
  long long int n = v.size();
  for (long long int i = 0; i < n; i++)
    cout << "(" << v[i].first << "," << v[i].second << ")";
  cout << endl;
}
void print2(map<long long int, long long int> &m) {
  map<long long int, long long int>::iterator it;
  for (it = m.begin(); it != m.end(); it++) {
    cout << it->first << ": " << it->second << endl;
  }
}
long long int binexp(long long int a, long long int x) {
  long long int res = 1;
  long long int temp = a % 1000000007;
  while (x) {
    if (x & 1) res = res * temp % 1000000007;
    temp = temp * temp % 1000000007;
    temp = temp % 1000000007;
    x = x >> 1;
  }
  return res % 1000000007;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long int n, k;
  cin >> n >> k;
  vector<long long int> v(n);
  long long int cnt = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> v[i];
    if (v[i] == 1) cnt++;
  }
  long long int cnt2 = n - cnt;
  vector<long long int> tes(k, 0), so(k, 0);
  for (long long int i = 0; i < n; i++) {
    if (v[i] == 1)
      tes[i % k]++;
    else
      so[i % k]++;
  }
  long long int maxi = 0;
  for (long long int i = 0; i < k; i++) {
    long long int p = cnt - tes[i];
    long long int q = cnt2 - so[i];
    maxi = max(maxi, abs(p - q));
  }
  cout << maxi << endl;
  return 0;
}
