시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 1024 MB 7 6 5 100.000%

문제

As you are probably aware, human DNA can be represented as a long string over an alphabet of size four (A, C, G, T), where each symbol represents a distinct nucleobase (respectively; adenine, cytosine, guanine, and thymine).

For martians, however, things are a bit different; research conducted on the latest martian captured by NASA revealed that martian DNA consists of a whopping K distinct nucleobases! Martian DNA may thus be represented as a string over an alphabet of size K.

Now, a certain research group interested in exploiting martian DNA in artificial intelligence applications has requested to get a single consecutive piece of a martian DNA string. For R of the nucleobases, they have specified a minimum quantity of how many they need of that particular nucleobase to be present in their sample.

You are interested in finding the shortest substring of the DNA which satisfies their requirements.

입력

The first line contains three integers N, K, and R, denoting the total length of the martian DNA, the alphabet size, and the number of nucleobases for which the researchers have a minimum quantity requirement, respectively. They obey 1 ≤ R ≤ K ≤ N.

The second line contains N space-separated integers: the complete martian DNA string. The i-th of these integers, Di, indicates what nucleobase is in the i-th position of the DNA string. Nucleobases are 0-indexed, i.e. 0 ≤ Di < K. Each nucleobase will occur at least once in the DNA string.

Each of the following R lines contains two integers B and Q representing a nucleobase and its mimimum required quantity, respectively (0 ≤ B < K, 1 ≤ Q ≤ N). No nucleobase will be listed more than once in these R lines.

출력

Output a single integer, the length of the shortest consecutive substring of the DNA that satisfies the researchers’ requirements. If no such substring exists, output “impossible”.

서브태스크 1 (16점)

  • 1 ≤ N ≤ 100
  • R ≤ 10

서브태스크 2 (24점)

  • 1 ≤ N ≤ 4000
  • R ≤ 10

서브태스크 3 (28점)

  • 1 ≤ N ≤ 200000
  • R ≤ 10

서브태스크 4 (32점)

  • 1 ≤ N ≤ 200000

예제 입력 1

5 2 2
0 1 1 0 1
0 1
1 1

예제 출력 1

2

예제 입력 2

13 4 3
1 1 3 2 0 1 2 0 0 0 0 3 1
0 2
2 1
1 2

예제 출력 2

7

예제 입력 3

5 3 1
1 2 0 1 2
0 2

예제 출력 3

impossible

힌트

In the first sample, there are three substrings of length 2 that contain one each of nucleobases 0 and 1 (namely “0 1”, “1 0” and “0 1”), but no substrings of length 1. Thus the shortest length is 2.

In the second sample, the (unique) optimal substring is “1 3 2 0 1 2 0”.

In the third sample, there aren’t enough nucleobases of type 0.