#include <bits/stdc++.h>
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define ln << '\n'
#define int long long
using namespace std;

const int N = 5e5 + 22;
int n, k, a[N];
vi ku[N];

int32_t main() {
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int cur = 0;
    for (int i = 1; i <= n; i++) {
        ku[cur].pb(a[i]);

        cur++;
        cur %= k;
    }
    int ans1 = 0;
    for (int i = 0; i < k; i++) {
        ans1 += max(ku[i][0], ku[i].back()) + ku[i].size();
        ku[i].clear();
    }
    reverse(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        ku[cur].pb(a[i]);

        cur++;
        cur %= k;
    }
    int ans2 = 0;
    for (int i = 0; i < k; i++) {
        ans2 += max(ku[i][0], ku[i].back()) + ku[i].size();
    }
    cout << max(ans1, ans2);
    return 0;
}