C - Card Concatenation Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は数字が書かれたカードを持っています。高橋君の手元には N 枚のカードがあり、i 番目のカードには整数 A_i が書かれています。

高橋君は、書かれている数が X 以上であるカードの中からちょうど K 枚を選び、選んだカードを好きな順番で左から横一列に並べることで、1 つの整数を作ることにしました。カードを並べてできる整数は、選んだカードに書かれた数を十進表記の文字列とみなして左から順に連結したものとします。

作ることができる整数の最大値を求めてください。ただし、書かれている数が X 以上であるカードが K 枚未満である場合は、代わりに -1 を出力してください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq \min(N, 18)
  • 1 \leq X \leq 9
  • 1 \leq A_i \leq 9(1 \leq i \leq N)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N K X
A_1 A_2 \ldots A_N
  • 1 行目には、カードの枚数を表す N、選ぶカードの枚数を表す K、基準となる値を表す X が、スペース区切りで与えられる。
  • 2 行目には、各カードに書かれた整数を表す N 個の整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。

出力

作ることができる整数の最大値を 1 行で出力せよ。ただし、書かれている数が X 以上であるカードが K 枚未満である場合は、代わりに -1 を出力せよ。


入力例 1

5 3 3
1 5 3 7 2

出力例 1

753

入力例 2

5 3 6
2 3 5 6 1

出力例 2

-1

入力例 3

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

出力例 3

998877

入力例 4

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

出力例 4

999988887777666555

入力例 5

1 1 1
1

出力例 5

1

Score : 300 pts

Problem Statement

Takahashi has cards with numbers written on them. He has N cards, and the i-th card has the integer A_i written on it.

Takahashi has decided to choose exactly K cards from among those with a number greater than or equal to X written on them, and arrange the chosen cards in a row from left to right in any order he likes to form a single integer. The integer formed by arranging the cards is defined as the concatenation, from left to right, of the numbers written on the chosen cards treated as decimal strings.

Find the maximum integer that can be formed. However, if there are fewer than K cards with a number greater than or equal to X written on them, output -1 instead.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq \min(N, 18)
  • 1 \leq X \leq 9
  • 1 \leq A_i \leq 9 (1 \leq i \leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K X
A_1 A_2 \ldots A_N
  • The first line contains N (the number of cards), K (the number of cards to choose), and X (the threshold value), separated by spaces.
  • The second line contains N integers A_1, A_2, \dots, A_N representing the integers written on each card, separated by spaces.

Output

Output the maximum integer that can be formed on a single line. However, if there are fewer than K cards with a number greater than or equal to X written on them, output -1 instead.


Sample Input 1

5 3 3
1 5 3 7 2

Sample Output 1

753

Sample Input 2

5 3 6
2 3 5 6 1

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

998877

Sample Input 4

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

Sample Output 4

999988887777666555

Sample Input 5

1 1 1
1

Sample Output 5

1