#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

#pragma GCC optimize("Ofast")

#define int long long

struct SegmentTree {
	struct Node {
		int mn = 0;
		int toadd;

		void add(int x) {
			mn += x;
			toadd += x;
		}

		Node() {}

		Node(int x) {
			mn = x;
		}
	};

	vector <Node> t;
	int n;

	SegmentTree(vector <int>& a) {
		n = a.size();
		t.resize(4 * n);
		build(0, 0, n, a);
	}

	SegmentTree(int n) {
		this->n = n;
		t.resize(4 * n);
	}

	Node merge(int v) {
		Node res(t[v * 2 + 1].mn + t[v * 2 + 2].mn);
		return res;
	}

	Node merge(Node l, Node r) {
		Node res(l.mn + r.mn);
		return res;
	}

	void build(int v, int l, int r, vector <int>& a) {
		if (l + 1 == r) {
			t[v] = Node(a[v]);
			return;
		}
		int m = (l + r) / 2;
		build(v * 2 + 1, l, m, a);
		build(v * 2 + 2, m, r, a);
		t[v] = merge(v);
	}

	void add(int v, int l, int r, int ql, int qr, int x) {
		if (qr <= l || r <= ql) return;
		if (ql <= l && r <= qr) {
			t[v].add(x);
			return;
		}
		int m = (l + r) / 2;
		add(v * 2 + 1, l, m, ql, qr, x);
		add(v * 2 + 2, m, r, ql, qr, x);
		t[v] = merge(v);
	}

	void add(int ql, int qr, int x) {
		add(0, 0, n, ql, qr, x);
	}

	Node get(int v, int l, int r, int ql, int qr) {
		if (qr <= l || r <= ql) return Node();
		if (ql <= l && r <= qr) return t[v];
		int m = (l + r) / 2;
		return merge(
			get(v * 2 + 1, l, m, ql, qr),
			get(v * 2 + 2, m, r, ql, qr)
		);
	}

	Node get(int ql, int qr) {
		return get(0, 0, n, ql, qr);
	}
};

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector <int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector <int> ind(n);
	map <int, vector <int>> mapa;
	for (int i = 0; i < n; i++) {
		mapa[a[i]].push_back(i);
	}
	int T = 0;
	for (auto& [x, vec] : mapa) {
		for (auto i : vec) {
			ind[i] = T++;
		}
	}
	//multiset <int> st;
	int answer = 0;
	SegmentTree derosum(n);
	SegmentTree derocnt(n);
	for (int i = 0; i < n; i++) {
		int curr = i + 1 + a[i];
		int l = -1, r = n;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (derocnt.get(mid, n).mn <= k - 1) {
				r = mid;
			}
			else {
				l = mid;
			}
		}
		curr += derosum.get(r, n).mn;
		answer = max(answer, curr);
		derosum.add(ind[i], ind[i] + 1, a[i]);
		derocnt.add(ind[i], ind[i] + 1, 1);
	};
	cout << answer;
}