#P4050. [JSOI2007] 麻将

[JSOI2007] 麻将

Description

Mahjong is one of China's traditional recreational tools. Mahjong tiles can be divided into honor tiles (there are seven kinds: east, south, west, north, red, green, and white) and numbered tiles (three suits: bamboo, dots, and characters, each suit has nine ranks from one to nine), and there are four copies of each tile.

In mahjong, a typical complete hand consists of fourteen tiles. Two of the fourteen tiles form a pair (two identical tiles), and the remaining twelve tiles form four groups of three, each group being either a sequence (three consecutive ranks in the same suit, such as 3, 4, 5 of bamboo) or a triplet (three identical tiles). A ready hand is a hand of thirteen tiles such that adding one tile can form a complete hand. That added tile is called a waiting tile.

Here, we consider a special kind of mahjong. In this special mahjong, there are no honor tiles, and there is only one suit. However, the ranks are not limited to the range from one to nine, but are in the range from 11 to nn. Also, there is no limit of four copies for each tile. A complete hand consists of 3m+23m + 2 tiles, of which two form a pair, and the remaining 3m3m tiles form mm groups of three, each group being either a sequence or a triplet. Given a set of 3m+13m + 1 tiles, determine whether this hand is ready (i.e., it is one tile away from a complete hand). If it is, output all possible waiting tiles.

Input Format

The input contains two lines.

The first line contains two integers n,mn, m separated by a space (9n4009 \le n \le 400, 4m10004 \le m \le 1000).

The second line contains 3m+13m + 1 integers separated by spaces, each in the range 11 to nn. These numbers represent the ranks of the tiles in the hand to be checked for readiness.

Output Format

Output a single line. If the hand is ready, output all possible waiting tiles’ ranks in ascending order, separated by a single space. If the hand is not ready, output NO.

9 4
1 1 2 2 3 3 5 5 5 7 8 8 8
6 7 9

Hint

Translated by ChatGPT 5