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>

using namespace std;

struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}init;

typedef long long LL;

template<typename T>void chmin(T &a,T b){a=min(a,b);}
template<typename T>void chmax(T &a,T b){a=max(a,b);}
template<typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}
typedef pair<int,int> P;
typedef priority_queue<P> Queue;
int main(){
    int N,M,Q;
    cin>>N>>M>>Q;
    vector<int> last_used_id(N,Q);
    vector<int> a(Q);
    cin>>a;
    vector<vector<int>> v(M);
    for(int i=0;i<Q;i++){
        v[a[i]-1].push_back(i);
    }
    Queue que;
    set<int> s;
    for(int i=0;i<M;i++){
        if(v[i].size()!=0)
            que.push(P(v[i].back(),i));
        s.insert(i);
    }
    int f=[&](){
    while(que.size()){
        int id=que.top().second;
        if(v[id].size()>=N){
            for(int i=0;i<N;i++){
                while(v[id].size()>0&&v[id].back()>last_used_id[i])
                    v[id].pop_back();
                if(v[id].size()==0){
                    if(id==(*s.begin())&&i>0){
                        que.pop();
                        return 1;
                    }
                    cout<<"No"<<endl;
                    return 0;
                }
                int t=v[id].back();
                v[id].pop_back();
                last_used_id[i]=t;
            }
            s.erase(id);
        }
        else break;
        que.pop();
        
    }
    return 1;
    }();
    if(f==0)return 0;
    while(que.size()){
        int id=que.top().second;que.pop();
        int lb=*(s. begin());
        if(id!=lb){
            cout<<"No"<<endl;
            return 0;
        }
        int t=v[id].back();
        if(t>last_used_id[0]){
            cout<<"No"<<endl;
            return 0;
        }
        last_used_id[0]=t;
        s.erase(id);
    }
    cout<<"Yes"<<endl;
    
    return(0);
}