#include #include #include #include #include #include #include #include #include #define LL long long int main(void) { std::vector alist; int n, k; std::cin >> n >> k; for (int i = 0; i < n; i++) { LL a; std::cin >> a; alist.push_back(a); } std::sort(alist.begin(), alist.end()); std::vector difflist; LL diffsum = 0; for (size_t i = 1; i < alist.size(); i++) { LL tmp = alist[i] - alist[i - 1]; diffsum += tmp; difflist.push_back(tmp); } std::sort(difflist.begin(), difflist.end(), [](const LL& a, const LL& b) {return a > b; }); for (int i = 0; i < k - 1; i++) { diffsum -= difflist[i]; } std::cout << diffsum << std::endl; return 0; }