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: Accepted)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld PI = acos(-1.0);
const ll LINF = (ll)1e18 + 5;
const int INF = (int)1e9 + 5;

template<class T>
T sqr(T x) { return x * x; }

template<class T>
T abs(T x) { return x < 0 ? -x : x; }

template<class T>
ll round(T x) { return x < 0 ? x - 0.5 : x + 0.5; }

template<class T>
bool chmin(T & x, const T & y) {
	if (y < x) {
		x = y;
		return true;
	}
	return false;
}

template<class T>
bool chmax(T & x, const T & y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}


template<class P, class Q>
ostream & operator <<(ostream & os, const pair<P, Q> & p) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template<class T>
ostream & operator <<(ostream & os, const vector<T> & v) {
	bool was = false;
	os << "{";
	for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {
		if (was) {
			os << ", ";
		}
		else {
			was = true;
		}
		os << *it;
	}
	os << "}";
	return os;
}

template<class T>
ostream & operator <<(ostream & os, const set<T> & v) {
	bool was = false;
	os << "{";
	for (typename set<T>::const_iterator it = v.begin(); it != v.end(); it++) {
		if (was) {
			os << ", ";
		}
		else {
			was = true;
		}
		os << *it;
	}
	os << "}";
	return os;
}

template<class T>
ostream & operator <<(ostream & os, const multiset<T> & v) {
	bool was = false;
	os << "{";
	for (typename multiset<T>::const_iterator it = v.begin(); it != v.end(); it++) {
		if (was) {
			os << ", ";
		}
		else {
			was = true;
		}
		os << *it;
	}
	os << "}";
	return os;
}

template<class P, class Q>
ostream & operator <<(ostream & os, const map<P, Q> & v) {
	bool was = false;
	os << "{";
	for (typename map<P, Q>::const_iterator it = v.begin(); it != v.end(); it++) {
		if (was) {
			os << ", ";
		}
		else {
			was = true;
		}
		os << *it;
	}
	os << "}";
	return os;
}

#define all(x) (x).begin(), (x).end()

template<class T>
T nextInt() {
	ll x = 0;
	bool p = false;
	char c;
	do {
		c = getchar();
	} while (c <= 32);
	if (c == '-') {
		p = true;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return (p ? -x : x);
}

set<int> tmp;
bool myCmp(vector<int> a, vector<int> b) {
	if (a.size() > b.size()) swap(a, b);
	for (int i = 0; i < (int)a.size(); i++) {
		if (b[i] != a[i]) return false;
	}
	for (int i = 0; i < (int)a.size(); i++) {
		tmp.erase(a[i]);
	}
	bool ok = true;
	static vector<int> erased;
	erased.resize(0);
	for (int i = (int)a.size(); i < (int)b.size(); i++) {
		int cur = *tmp.begin(); tmp.erase(tmp.begin());
		erased.push_back(cur);
		ok = ok && (cur == b[i]);
	}
	for (int i = 0; i < (int)a.size(); i++) {
		tmp.insert(a[i]);
	}
	for (int x : erased) {
		tmp.insert(x);
	}
	return ok;
}

int main() {
	srand(time(0));

#ifdef LOCAL
	//gen();
	freopen("input.txt", "r", stdin);
#else
	
#endif

	int n, m, q;

	scanf("%d%d", &n, &m);
	scanf("%d", &q);

	vector<int> a(q);

	for (int & x : a) {
		scanf("%d", &x);
	}

	reverse(all(a));

	map<int, set<int>> listOfHeight;
	vector<vector<int>> cols(n);

	for (int i = 0; i < n; i++) {
		listOfHeight[cols[i].size()].insert(i);
	}

	map<int, set<int>> whoNeed;
	set<int> was;

	for (int x : a) {
		auto & z = whoNeed[x];
		if (!z.empty()) {
			int idx = *z.begin(); z.erase(z.begin());
			listOfHeight[cols[idx].size()].erase(idx);
			cols[idx].push_back(x);
			listOfHeight[cols[idx].size()].insert(idx);
			was.insert(x);
			while ((--listOfHeight.end())->second.empty()) {
				listOfHeight.erase(--listOfHeight.end());
			}
			int h = (--listOfHeight.end())->first;
			auto & t = listOfHeight[h];
			int hiIdx = *t.begin();
			if ((int)cols[idx].size() < h) {
				whoNeed[cols[hiIdx][cols[idx].size()]].insert(idx);
			}
		}
		else {
			if (was.count(x)) continue;
			was.insert(x);
			while ((--listOfHeight.end())->second.empty()) {
				listOfHeight.erase(--listOfHeight.end());
			}
			int h = (--listOfHeight.end())->first;
			auto & t = listOfHeight[h];
			int idx = *t.begin(); t.erase(t.begin());
			cols[idx].push_back(x);
			listOfHeight[cols[idx].size()].insert(idx);
			for (int q : t) {
				whoNeed[x].insert(q);
			}
		}
	}


	for (int i = 1; i <= m; i++) {
		tmp.insert(i);
	}

	bool res = true;
	for (int i = 0; i + 1 < (int)cols.size(); i++) {
		res = res && myCmp(cols[i], cols[i + 1]);
	}

	puts(res ? "Yes" : "No");
}