Problem:
Score : 1200 points

Problem Statement

There are N arrays.
The length of each array is M and initially each array contains integers (1，2，...，M) in this order.

Mr. Takahashi has decided to perform Q operations on those N arrays.
For the i-th (1≤i≤Q) time, he performs the following operation.

Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5，4，3，2，1), this array becomes (2，5，4，3，1).

Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations.
Determine if it is possible or not.

Constraints

2≤N≤10^5

2≤M≤10^5

1≤Q≤10^5

1≤a_i≤M

Input

The input is given from Standard Input in the following format:

N M
Q
a_1 a_2 ... a_Q

Output

Print Yes if it is possible to make N arrays exactly the same after performing the Q operations.
Otherwise, print No.

Sample Input 1

2 2
3
2 1 2

Sample Output 1

Yes

You can perform the operations as follows.

Sample Input 2

3 2
3
2 1 2

Sample Output 2

No

Sample Input 3

2 3
3
3 2 1

Sample Output 3

Yes

You can perform the operations as follows.

Sample Input 4

3 3
6
1 2 2 3 3 3

Sample Output 4

No

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
typedef pair<int,int> pa;
typedef vector<int> vec;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
vec v[100010];
bool u[500010];
int now,q,n,f[500010],m,a[500010],used;
bool rem(){
	int t=0;
	FOR(i,1,m) if (SZ(v[i]) && !u[i]){
		if (v[i][0]<=t) return 0;
		t=v[i][0];
	}
	return 1;
}
int main(){
	cin>>n>>m>>q;
	FOR(i,1,q) getint(a[i]);
	reverse(a+1,a+q+1);
	FOR(i,1,n) f[i]=0;
	FOR(i,1,q) v[a[i]].pb(i);
	now=1;
	while (now<=q){
		if (u[a[now]]){++now;continue;}
		if (a[now]==1){
			if (rem()) return puts("Yes"),0;
		}
		u[a[now]]=1;
		used+=SZ(v[a[now]]);
		int t=0;
		FOR(i,1,n){
			while (v[a[now]][t]<=f[i] && t<SZ(v[a[now]])) ++t;
			if (t>=SZ(v[a[now]])) return puts("No"),0;
			f[i]=v[a[now]][t];
			++t;
		}
		++now;
	}
	puts("Yes");
	return 0;
}