#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
  for (int i = 0; i < ((int)((t).size())); i++) s << t[i] << " ";
  return s;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  long long ie = 0, is = 0;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    long long k;
    cin >> arr[i];
    k = arr[i];
    if (k == 1) {
      ie++;
    } else {
      is++;
    }
  }
  long long max = 0;
  vector<int> v(n, 0);
  for (int i = 0; i < n; ++i) {
    long long t1 = ie;
    long long t2 = is;
    if (v[i] == 0) {
      for (int j = 0; i + j * k < n; ++j) {
        if (arr[i + j * k] == 1) {
          t1--;
        } else {
          t2--;
        }
        v[i + j * k] = 1;
      }
      if (max < abs(t1 - t2)) {
        max = abs(t1 - t2);
      }
    }
  }
  cout << max << "\n";
}
