#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	UNDEFINED,
	USED,
	EITHER,
};

#define SIZE 300005

int N,R;
int table[SIZE],num[SIZE];
int dp[SIZE];
map<int,bool> MAP;
Type type[SIZE];


int main(){

	scanf("%d %d",&N,&R);

	int num_either;

	for(int i = 1; i <= N; i++){

		scanf("%d",&table[i]);
		if(i == table[i]){

			type[i] = EITHER; //どちらのグループでも良い
			num_either++;
		}else{

			type[i] = UNDEFINED;
		}
	}

	int sum = 0;

	for(int i = 0; i < SIZE; i++){

		num[i] = 0;
	}

	vector<int> V;

	//場所 != 数字となっている数字の、本来の位置を指したループを作る(グループ1)
	for(int i = 1; i <= N; i++){

		if(type[i] == UNDEFINED){

			int count = 0;

			type[i] = USED;
			count++;

			int loc = i;

			while(type[table[loc]] == UNDEFINED){

				type[table[loc]] = USED;
				count++;
				loc = table[loc];
			}

			sum += count;
			num[count] += 1;

			auto at = MAP.find(count); //初登場の数字
			if(at == MAP.end()){

				MAP[count] = true;
				V.push_back(count);
			}
		}
	}

	dp[0] = 0;
	for(int i = 1; i <= N; i++){

		dp[i] = -1;
	}
	for(int i = 0; i < V.size(); i++){ //数の種類のループ
		for(int k = 0; k <= N; k++){ //作りたい数のループ

			if(dp[k] >= 0){

				dp[k] = num[V[i]]; //V[i]の個数

			}else if(k < V[i] || dp[k-V[i]] <= 0){

				dp[k] = -1;

			}else{

				dp[k] = dp[k-V[i]]-1; //V[i]を1つ使用
			}
		}
	}

	for(int i = 0; i <= max(R,N-R); i++){
		if(dp[i] < 0)continue; //数字iを作れないならSKIP

		if(min(i,sum-i) <= min(R,N-R) && max(i,sum-i) <= max(R,N-R)){
			//差分は、eitherを適当に割り振って調整
			printf("Yes\n");
			return 0;
		}
	}

	printf("No\n");

	return 0;
}


### Prompt

Please formulate a CPP solution to the following problem:

D: Two Colors Sort

problem

During the walk, umg found a sequence of length N, P_1, P_2, ..., P_N, which can be made by rearranging 1,2, ..., N.

umg can use mysterious powers to exchange places by choosing two different numbers painted in the same color.

umg wanted to be able to sort the sequence in ascending order by painting R of the numbers in the sequence in red and the remaining N-R in blue.

umg Determine if you can reach your goal.

However, the numbers are so heavy that they cannot be moved without mysterious force. Also, since umg is a genius, you can use mysterious powers any number of times.

Input format


N R
P_1 P_2 ... P_N


Constraint

* 1 \ leq N \ leq 3 \ times 10 ^ 5
* 1 \ leq R \ leq N
* 1 \ leq P_i \ leq N
* P_i \ neq P_j (1 \ leq i <j \ leq N)
* All inputs are integers.



Output format

umg Print `Yes` if you can achieve your goal, otherwise` No` on one line.

Input example 1


3 2
1 3 2


Output example 1


Yes

* You can achieve your goal by painting 1 in blue and 2 and 3 in red.



Input example 2


5 2
1 2 3 4 5


Output example 2


Yes

* They are arranged in ascending order from the beginning.



Input example 3


10 7
3 4 8 5 7 6 2 10 1 9


Output example 3


No





Example

Input

3 2
1 3 2


Output

Yes

### Response

```cpp
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	UNDEFINED,
	USED,
	EITHER,
};

#define SIZE 300005

int N,R;
int table[SIZE],num[SIZE];
int dp[SIZE];
map<int,bool> MAP;
Type type[SIZE];


int main(){

	scanf("%d %d",&N,&R);

	int num_either;

	for(int i = 1; i <= N; i++){

		scanf("%d",&table[i]);
		if(i == table[i]){

			type[i] = EITHER; //どちらのグループでも良い
			num_either++;
		}else{

			type[i] = UNDEFINED;
		}
	}

	int sum = 0;

	for(int i = 0; i < SIZE; i++){

		num[i] = 0;
	}

	vector<int> V;

	//場所 != 数字となっている数字の、本来の位置を指したループを作る(グループ1)
	for(int i = 1; i <= N; i++){

		if(type[i] == UNDEFINED){

			int count = 0;

			type[i] = USED;
			count++;

			int loc = i;

			while(type[table[loc]] == UNDEFINED){

				type[table[loc]] = USED;
				count++;
				loc = table[loc];
			}

			sum += count;
			num[count] += 1;

			auto at = MAP.find(count); //初登場の数字
			if(at == MAP.end()){

				MAP[count] = true;
				V.push_back(count);
			}
		}
	}

	dp[0] = 0;
	for(int i = 1; i <= N; i++){

		dp[i] = -1;
	}
	for(int i = 0; i < V.size(); i++){ //数の種類のループ
		for(int k = 0; k <= N; k++){ //作りたい数のループ

			if(dp[k] >= 0){

				dp[k] = num[V[i]]; //V[i]の個数

			}else if(k < V[i] || dp[k-V[i]] <= 0){

				dp[k] = -1;

			}else{

				dp[k] = dp[k-V[i]]-1; //V[i]を1つ使用
			}
		}
	}

	for(int i = 0; i <= max(R,N-R); i++){
		if(dp[i] < 0)continue; //数字iを作れないならSKIP

		if(min(i,sum-i) <= min(R,N-R) && max(i,sum-i) <= max(R,N-R)){
			//差分は、eitherを適当に割り振って調整
			printf("Yes\n");
			return 0;
		}
	}

	printf("No\n");

	return 0;
}


```