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<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[101000];
int n, m, Q, w[101000], pv[101000], CC;
struct point{
    int t, num;
    bool operator<(const point &p)const{
        return t<p.t;
    }
}P[101000];
int main(){
    int i, chk = 0, j, pp = -1;
    scanf("%d%d%d",&n,&m,&Q);
    for(i=1;i<=m;i++)P[i].num=i,P[i].t=i;
    for(i=1;i<=Q;i++){
        scanf("%d",&w[i]);
        E[w[i]].push_back(i);
        P[w[i]].t=-i;
    }
    sort(P+1,P+m+1);
    for(i=1;i<=m;i++){
        if(P[i].num == 1)pp = i;
    }
    if(pp!=-1){
        chk = 1;
        for(i=pp;i<m;i++)if(P[i].num > P[i+1].num)chk = 0;
    }
    if(!chk){
        for(i=1;i<=m;i++)if(P[i].t<0)CC++;
    }
    else{
        CC = pp-1;
    }
    if(1ll*CC*n > Q){
        printf("No\n");
        return 0;
    }
    for(i=1;i<=m;i++){
        pv[i] = E[i].size() - 1;
    }
    for(i=1;i<=n;i++){
        int tp = 1e9;
        int ck2 = 0;
        for(j=1;j<=m;j++){
            int x = P[j].num;
            if(P[j].t > 0)break;
            if(x==1)ck2=1;
            if(chk && ck2)break;
            if(P[j].t > 0)break;
            while(pv[x] >= 0 && E[x][pv[x]] > tp)pv[x]--;
            if(pv[x] == -1){
                printf("No\n");
                return 0;
            }
            tp = E[x][pv[x]];
            pv[x]--;
        }
    }
    printf("Yes\n");
}