This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair <ll, ll>
#define st first
#define nd second
#define rep(i, n, m) for (ll i = (n); i <= (m); i ++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i --)
using namespace std;
const long long N = 1e5 + 5;
ll n, a[N], k;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
rep(i, 1, n) cin >> a[i];
vector <pii> vi;
vector <pii> pos;
vi.push_back({a[1], a[1] + 1});
ll ans = 0;
rep(i, 2, n) {
if (a[i] == vi.back().nd) {
vi.back().nd ++;
continue;
}
vi.push_back({a[i], a[i] + 1});
}
int cnt = 0;
for (pii x: vi) {
ans += x.nd - x.st;
cnt ++;
}
rep(i, 1, (int)vi.size() - 1)
pos.push_back({vi[i].st - vi[i - 1].nd, i});
sort(pos.begin(), pos.end());
int j = 0;
while (cnt > k) {
ans += pos[j].st;
j ++;
cnt --;
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |