Submission #1149815

#TimeUsernameProblemLanguageResultExecution timeMemory
1149815h1euctStove (JOI18_stove)C++20
100 / 100
12 ms2244 KiB
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second

#define int long long

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7;

int n, k;
int a[MX];

namespace sub1{
	
	int x[25];
	vector<int> v;
	int res;
	
	void duyet(int i) {
		if (i > n) {
			int cnt = 0;
			for (int j = 1; j <= n; j++) cnt += x[j];
			if (cnt != k) return;
			if (x[n] != 1) return;
			v.clear();
			v.push_back(a[1]);
			for (int j = 1; j <= n; j++) {
				if (x[j] == 1) {
					v.push_back(a[j] + 1);
					v.push_back(a[j + 1]);
				} 
			} 
			int fn = 0;
			for (int i = 0; i < v.size() - 1; i += 2) {
				fn += (v[i + 1] - v[i]);
				// cerr << v[i] << ' ' << v[i+1] << '\n';
			}
			res = min(res, fn);
			// for (int j = 1; j <= n; j++) cerr << x[j] << ' ';
			// cerr << '\n';
			// for (auto it : v) cerr << it << ' ';
			return;
		}
		for (int j = 0; j <= 1; j++) {
			x[i] = j;
			duyet(i + 1);
		}
	}
	
	void solve() {
		res = 1e18;
		duyet(1);
		cout << res;
	}
	
}

namespace sub3{
	
	vector<int> v;
	
	void solve() {
		for (int i = 2; i <= n; i++) {
			v.push_back(a[i - 1] + 1 - a[i]);
		}
		sort(v.begin(), v.end());
		int fn = a[n] + 1 - a[1];
		for (int i = 0; i < k - 1; i++) {
			fn += v[i];
		}
		cout << fn;
	}
	
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	// int test; cin>>test; while (test--) {}	
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	// if (n <= 20) sub1::solve();
	// else sub3::solve();
	sub3::solve();
	return 0;
}
// binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...