#define _CRT_SECURE_NO_WARNINGS
#ifndef _DEBUG
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <set>
#include <unordered_set>
#include <map>
#include <deque>
#include <queue>
#include <unordered_map>
#include <iomanip>
using namespace std;
using ll = int;
using ld = long double;
using pll = pair<int, int>;
using str = string;
auto sd = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(sd);
vector<pll> k2[500002];
int idx[500002];
void sol1() {
	int n, k;
	cin >> n >> k;
	vector<pll> a;
	for (int i = 0; i < n; i++) {
		ll h; cin >> h;
		a.push_back({ h, i + 1 });
	}
	
	sort(a.begin(), a.end());
	vector<pll> w;
	for (int j = n - 1; j >= n - k; j--) {
		w.emplace_back(a[j].second, a[j].first);
	}
	//vector<vector<pll>> k2(k);
	sort(w.begin(), w.end());
	for (int i = 0; i < k; i++) {
		k2[i].push_back(w[i]);
		idx[w[i].first] = i;
	}
	vector<pll> all; bool is = false;
	for (int j = 0; j < n - k; j++) {
		//ll ai, ix;
		is = false;
		auto it = upper_bound(w.begin(), w.end(), make_pair(a[j].second,0ll));
		if (it != w.end()) {
			is = true;
			k2[idx[it->first]].push_back({ a[j].second,a[j].first });
		}
		if (!is) {
			all.push_back({ a[j].second, a[j].first });
		}
	}
	long long mn = 1e18;
	ll ix = 0;
	for (int ii = 0; ii < k; ii++) {
		sort(k2[ii].begin(), k2[ii].end());
		if (k2[ii].back().second < mn) {
			mn = k2[ii].back().second;
			ix = ii;
		}
	}
	for (auto v : all) {
		k2[ix].push_back(v);
	}
	sort(k2[ix].begin(), k2[ix].end());
	long long sum1 = 0;
	for (int v = 0; v < k; v++) {
		ll mx = 0;
		for (int j = 0; j < k2[v].size(); j++) {
			mx = max(mx, (ll)k2[v][j].second + j + 1);
		}
		sum1 += mx;
	}
	cout << sum1;
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ll t1 = 1; 
	//cin >> t1;
	for (int o_ = 0; o_ < t1; o_++) sol1();
}

