Programming contests

ECN programozó csapatverseny, 2023. november 25.

November 25, 2023 10:30 AM – November 25, 2023 3:30 PM

Colors in Store

A new shipment of n tablecloths has arrived at a store. Each tablecloth is described by three attributes: price (pi), front color (ai), and back color (bi), where pi is an integer and represents the cost of the ith tablecloth, and ai and bi are integers ranging from 1 to 3, indicating the colors of the front and back of the tablecloth, respectively. It is guaranteed that all pi values are unique.

The store expects m customers to visit, and each customer intends to purchase exactly one tablecloth. For each customer, we know their favorite color (cj). A customer will buy a tablecloth if either the front or back side matches their favorite color. In such cases, the customer selects the cheapest available option among the tablecloths that match their preferred color. If there are no tablecloths with an acceptable color for the customer, they won't make a purchase. The customers arrive sequentially, and each customer is served only after the previous one has been attended to.

Your task is to calculate the price each customer will pay for the tablecloth they choose.

Input Specification

The first line contains an integer n (1 ≤ n ≤ 200 000), representing the number of tablecloths. The following line provides a sequence of n integers: p1p2, …, pn (1 ≤ pi ≤ 1 000 000 000), indicating the prices of the tablecloths. The next line contains a sequence of n integers: a1a2, …, an (1 ≤ ai ≤ 3), representing the front colors of the tablecloths. The subsequent line contains a sequence of n integers: b1b2, …, bn (1 ≤ bi ≤ 3), indicating the back colors of the tablecloths. Then, there's a single integer m (1 ≤ m ≤ 200 000), denoting the number of customers. The next line provides a sequence of m integers: c1c2, …, cm (1 ≤ cj ≤ 3), signifying the favorite color of each customer. The customers arrive in the order they are listed in the input, and each customer is served only after the previous one has been assisted.

Output Specification

Print one line to the standard output with m space-separated integers, where the jth integer should represent the price of the tablecloth that the jth customer will purchase. If the jth customer decides not to buy anything, print –1 for that customer.

Sample Input

  1. 5
  2. 300 200 400 500 911
  3. 1 2 1 2 3
  4. 2 1 3 2 1
  5. 6
  6. 2 3 1 2 1 1
download as text file

Output for Sample Input

  1. 200 400 300 500 911 -1
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019