#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;
        Node *l = nullptr, *r = nullptr;
    };
 public:
    Node *root;

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

    SegTree() {
        root = new Node;
    }

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

        if (l + 1 == r) {
            curV->cnt++;
            curV->sum += x;
            return;
        }

        int m = (l + r) / 2;
        if (!curV->l) curV->l = new Node;
        if (!curV->r) curV->r = new Node;

        add(curV->l, l, m, x);
        add(curV->r, m, r, x);

        curV->cnt = curV->l->cnt + curV->r->cnt;
        curV->sum = curV->l->sum + curV->r->sum;
    }

    inline long long sum(Node* curV, int l, int r, int& k) {
        if (k == 0 || curV->cnt == 0) return 0;

        if (curV->cnt <= k) {
            k -= curV->cnt;
            return curV->sum;
        }

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

        int m = (l + r) / 2;
        if (!curV->l) curV->l = new Node;
        if (!curV->r) curV->r = new Node;

        return sum(curV->r, m, r, k) + sum(curV->l, l, m, k);
    }
};

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(system.root, system.L, system.R, array[i]);
    }

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

    std::cout << res;
}
