#924
Gold II
MATRICA
시간 제한
0.2s
메모리 제한
32MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

A matrix is a rectangular table of letters. A square matrix is a matrix with an equal number of rows and columns. A square matrix M is called symmetric if its letters are symmetric with respect to the main diagonal (Mij = Mji for all pairs of i and j). The following figure shows two symmetric matrices and one which is not symmetric:

AAB ACC BCC AAA ABA AAA ABCD ABCD ABCD ABCD AAB ACA DAA Two symmetric matrices. Two matrices that are not symmetric.

Given a collection of available letters, you are to output a subset of columns in the lexicographically smallest symmetric matrix which can be composed using all the letters. If no such matrix exists, output "IMPOSSIBLE". To determine if matrix A is lexicographically smaller than matrix B, consider their elements in row- major order (as if you concatenated all rows to form a long string). If the first element in which the matrices differ is smaller in A, then A is lexicographically smaller than B.

입력

The first line of input contains two integers N (1 ≤ N ≤ 30000) and K (1 ≤ K ≤ 26). N is the dimension of the matrix, while K is the number of distinct letters that will appear. Each of the following K lines contains an uppercase letter and a positive integer, separated by a space. The integer denotes how many corresponding letters are to be used. For example, if a line says "A 3", then the letter A must appear three times in the output matrix. The total number of letters will be exactly N2. No letter will appear more than once in the input. The next line contains an integer P (1 ≤ P ≤ 50), the number of columns that must be output. The last line contains P integers, the indices of columns that must be output. The indices will be between 1 and N inclusive, given in increasing order and without duplicates.

출력

If it is possible to compose a symmetric matrix from the given collection of letters, output the required columns on N lines, each containing P character, without spaces. Otherwise, output "IMPOSSIBLE" (quotes for clarity).

예제 입력 1

3 3
A 3
B 2
C 4
3
1 2 3

예제 출력 1

AAB
ACC
BCC

예제 입력 2

4 4
A 4
B 4
C 4
D 4
4
1 2 3 4

예제 출력 2

AABB
AACC
BCDD
BCDD

예제 입력 3

4 5
E 4
A 3
B 3
C 3
D 3
2
2 4

예제 출력 3

AC
BE
DE
ED

예제 입력 4

4 6
F 1
E 3
A 3
B 3
C 3
D 3
4
1 2 3 4

예제 출력 4

IMPOSSIBLE
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Gold II1명 투표· 약 1개월 전
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.