A Christmas Carol 3

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Java 8 2.0s
Memory limit: 64M

Author:
Problem type

It's the night before Christmas and bitter old Ebenezer Scrooge has banished the Ghost of Christmas Past from his sight.
Little does he know, this is only the beginning of his plight.
For he will now receive a visit by the Ghost of Christmas Present.
And although old Ebenezer Scrooge is feeling quite irreverent.
The Ghost of Christmas Present shows the old man visions of his secretary's family.
And of little Tiny Tim, a terminally ill boy who still enjoys Christmas happily.
The secretary, Bob Cratchit, is out to buy a turkey for a Christmas feast.
Help him determine the size of turkey he will buy, and it would be a significant feat.

Ebenezer Scrooge's secretary, Bob Cratchit is buying turkeys for his family's Christmas dinner. In order to not be overwhelmed by food, he needs to only buy turkeys that are below a certain size. However, he wants to maximize the size of the turkey while still keeping it below this threshold. Given the maximum sizes of the turkeys he wants to buy as well as the turkeys available, determine which turkeys on sale are the biggest while still being at or below his maximum.

Input Specification

The first line of input contains n (1 \le n \le 10^6) representing the number of turkeys available in the store.
The next n lines each contain an integer n_i (1 \le n_i \le 10^6) representing the size of each turkey available. There is an unlimited number of turkeys of size n_i and each value of n_i is guaranteed to be unique.
The next line contains q (1 \le q \le 10^6) representing the number of turkeys Bob Cratchit wants to buy.
The next q lines each contain an integer q_i (1 \le q_i \le 10^6) representing the maximum size of one of the turkeys Bob wants to buy. The turkey bought must not exceed this size.
Solutions in Java are encouraged to use fast input reading.

Output Specification

For each maximum size, output on a new line the size of turkey Bob Cratchit should buy for it. If no turkey at or below this size exists, output 0.

Subtasks

Subtask 1

For 3 points, 1 \le q \le 1000.

Subtask 2

For the remaining 4 points, no additional constraints.

Sample Input

5
1
7
4
11
12
3
5
20
1

Sample Output

4
12
1

Comments