#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
#define LOCAL

// std::mt19937 rnd(time(0));

void solve();

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    #ifdef LOCAL
        std::freopen("input.txt", "r", stdin);
        std::freopen("output.txt", "w", stdout);
    #endif

    int t = 1;
    // std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

class SegTree {
    class Node {
     public:
        long long sum = 0;
        int cnt = 0;
        int l = -1, r = -1;
    };
 public:
    std::vector <Node> body = {{0, 0, -1, -1}};

    const int L = 0, R = 1'000'000'000 + 1;

    int make() {
        body.push_back(Node());
        return (int) body.size() - 1;
    }

    inline void add(int curV, int l, int r, int x) {
        if (x < l || r <= x) return;

        if (l + 1 == r) {
            body[curV].cnt++;
            body[curV].sum += x;
            return;
        }

        int m = (l + r) / 2;
        if (x < m && body[curV].l == -1) body[curV].l = make();
        if (m <= x && body[curV].r == -1) body[curV].r = make();

        if (x < m) add(body[curV].l, l, m, x);
        if (m <= x) add(body[curV].r, m, r, x);

        body[curV].cnt = 0;
        body[curV].sum = 0;

        if (body[curV].l != -1) {
            body[curV].cnt += body[body[curV].l].cnt;
            body[curV].sum += body[body[curV].l].sum;
        }
        if (body[curV].r != -1) {
            body[curV].cnt += body[body[curV].r].cnt;
            body[curV].sum += body[body[curV].r].sum;
        }
    }

    inline long long sum(int curV, int l, int r, int& k) {
        if (k == 0 || body[curV].cnt == 0) return 0;

        if (body[curV].cnt <= k) {
            k -= body[curV].cnt;
            return body[curV].sum;
        }

        if (l + 1 == r) {
            int d = k;
            k = 0;
            return 1ll * l * d;
        }

        int m = (l + r) / 2;

        long long s = 0;

        if (body[curV].r != -1) s += sum(body[curV].r, m, r, k);
        if (body[curV].l != -1) s += sum(body[curV].l, l, m, k);

        return s;
    }
};

void solve() {
    int n, k;
    std::cin >> n >> k;

    std::vector <int> array(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> array[i];
    }

    SegTree system;

    long long res = 0;

    for (int i = 0; i < k - 1; ++i) {
        system.add(0, system.L, system.R, array[i]);
    }

    for (int i = k - 1; i < n; ++i) {
        int need = k - 1;
        long long max = system.sum(0, system.L, system.R, need);
        res = std::max(res, array[i] + max + i - k + 1 + k);
        system.add(0, system.L, system.R, array[i]);
    }

    std::cout << res;
}
