#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5, Mod = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n, k;
  cin >> n >> k;
  vector<long long> v(n + 1);
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  long long mx = 0;
  for (long long i = 1; i <= 2 * n; i++) {
    long long fn = sum;
    for (long long j = -2 * n; j <= 2 * n; j++) {
      long long x = i + j * k;
      if (x >= 1 && x <= n) fn -= v[x];
    }
    mx = max(mx, abs(fn));
  }
  cout << mx;
  return 0;
}
