#C11321. Smallest Greater Number

    ID: 40625 Type: Default 1000ms 256MiB

Smallest Greater Number

Smallest Greater Number

Given two sets of integers (S) and (T), for each integer (t \in T), find the smallest integer in (S) that is strictly greater than (t). If no such integer exists, output -1.

The task can be formalized as: for each (t), find (\min { s \in S \mid s > t }) (if the set is non-empty) or return -1 otherwise.

Efficient solutions should sort the array (S) and use binary search to answer each query in (T).

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains two space-separated integers (n) and (m) representing the number of elements in set (S) and set (T), respectively.
  2. The second line contains (n) space-separated integers representing the elements of set (S).
  3. The third line contains (m) space-separated integers representing the elements of set (T).

outputFormat

Print to standard output (stdout) a single line containing (m) integers separated by a space. Each integer is the smallest element in (S) that is strictly greater than the corresponding integer in (T), or -1 if no such element exists.## sample

5 3
10 20 30 40 50
5 25 60
10 30 -1