#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int INF = INT_MAX / 10; struct bit { vector v; bit(int n) : v(n + 1) {} ll sum(int i) { ll res = 0; for (; i > 0; i -= i & -i) res += v[i]; return res; } void add(int i, ll x) { for (i++; i < v.size(); i += i & -i) v[i] += x; } }; int main() { int N, K; cin >> N >> K; vector p(N), B(N); for (int i = 0; i < N; i++) scanf("%d", &B[i]); ll ans = 0, sum = 0; bit b(N); for (int i = 0; i < N; i++) { sum += i - b.sum(B[i] - 1); b.add(B[i] - 1, 1); } while (K--) { ans += sum; int i; for (i = N - 2; i >= 0 && B[i] > B[i + 1]; i--); if (i == -1) { sum = 0; sort(B.begin(), B.end()); continue; } int n = N - i - 1; sum -= (ll)n * (n - 1) / 2; for (int j = i + 1; j < N; j++) if (B[i] > B[j]) sum--; next_permutation(B.begin(), B.end()); for (int j = i + 1; j < N; j++) if (B[i] > B[j]) sum++; /*for (int i = 0; i < N; i++) cout << B[i] << ' '; cout << endl; cout << sum << endl;*/ } cout << ans << endl; }