Language: C++

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;

constexpr double EPS = 1e-7;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
#define PI (acos(-1))

constexpr int MOD = 1e9 + 7;

void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
	if (rest == 0) {
		f(indexes);
	} else {
		if (s < 0)
			return;
		recursive_comb(indexes, s - 1, rest, f);
		indexes[rest - 1] = s;
		recursive_comb(indexes, s - 1, rest - 1, f);
	}
}

// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(int *)> f) {
	int indexes[k];
	recursive_comb(indexes, n - 1, k, f);
}

int main(int argc, char const *argv[]) {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);

	int N, K;
	cin >> N >> K;

	vector<int> A(N);
	for (auto &&i : A) {
		cin >> i;
	}

	sort(A.begin(), A.end());

	ll res = 0;
	foreach_comb(N, K, [&](int *indexes) {
		int min_v = A[indexes[0]];
		int max_v = A[indexes[K - 1]];
		res += (max_v - min_v);
		res %= MOD;
	});

	cout << res << endl;

	return 0;
}


Compiler output:
a.cc:34:52: error: 'std::function' has not been declared
   34 | void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
      |                                                    ^~~
a.cc:34:65: error: expected ',' or '...' before '<' token
   34 | void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
      |                                                                 ^
a.cc: In function 'void recursive_comb(int*, int, int, int)':
a.cc:36:17: error: 'f' was not declared in this scope
   36 |                 f(indexes);
      |                 ^
a.cc:40:54: error: 'f' was not declared in this scope
   40 |                 recursive_comb(indexes, s - 1, rest, f);
      |                                                      ^
a.cc: At global scope:
a.cc:47:33: error: 'std::function' has not been declared
   47 | void foreach_comb(int n, int k, std::function<void(int *)> f) {
      |                                 ^~~
a.cc:47:46: error: expected ',' or '...' before '<' token
   47 | void foreach_comb(int n, int k, std::function<void(int *)> f) {
      |                                              ^
a.cc: In function 'void foreach_comb(int, int, int)':
a.cc:49:43: error: 'f' was not declared in this scope
   49 |         recursive_comb(indexes, n - 1, k, f);
      |                                           ^
a.cc: In function 'int main(int, const char**)':
a.cc:68:28: error: cannot convert 'main(int, const char**)::<lambda(int*)>' to 'int'
   68 |         foreach_comb(N, K, [&](int *indexes) {
      |                            ^~~~~~~~~~~~~~~~~~~
      |                            |
      |                            main(int, const char**)::<lambda(int*)>
   69 |                 int min_v = A[indexes[0]];
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                 int max_v = A[indexes[K - 1]];
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   71 |                 res += (max_v - min_v);
      |                 ~~~~~~~~~~~~~~~~~~~~~~~
   72 |                 res %= MOD;
      |                 ~~~~~~~~~~~ 
   73 |         });
      |         ~                   
a.cc:47:33: note:   initializing argument 3 of 'void foreach_comb(int, int, int)'
   47 | void foreach_comb(int n, int k, std::function<void(int *)> f) {
      |                                 ^~~~~~~~~~~~~