Under construction

Still being built. Things may change

Auto-Coin-o-Matic

Time limit2sMemory limit2048 MB

Summary
Coins are removed over time, and for each query you must find the fewest coins that make amount v, or -1 if no combination works.
Level

Medium6 of 10

Topics
Dynamic programming, Implementation
Solved
No attempts yet

Problem

It is finally here! The day you unveil your new invention, the Auto-Coin-o-Matic, has arrived. People insert their card, type in the amount they want, and get exact change back with the fewest number of coins. You watch with glee and anxiety.

Was it actually the fewest number of coins? That is how the machine was programmed, but what if there is a bug? It is okay, you are watching. You decide to randomly pick some transactions and check whether the machine gave out the fewest coins possible. But the machine is running out of certain types of coins. Will it still work correctly?

Input

The input starts with two integers nn and mm (1≤n≤20001 \le n \le 2000, 1≤m≤1051 \le m \le 10^5).

The next line contains nn integers, d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤di≤1051 \le d_i \le 10^5), the denominations of coins available initially. All denominations are unique.

The next mm lines each contain a character cc (c∈{Q,X}c \in \{Q, X\}) and an integer vv (1≤v≤1051 \le v \le 10^5), where cc is the type of event and vv is its value.

  • If cc is Q, this is a query. Output the minimum number of coins needed to give out exactly vv. If it is not possible to make vv exactly with the available denominations, output −1-1.
  • If cc is X, the machine is out of coins of denomination vv. All later queries cannot use that denomination. Each X event refers to a denomination vv that the machine currently has in stock.

Output

Output kk lines, where kk is the number of query events. On the iith line, output the fewest number of coins needed for the iith query, or −1-1 if it is impossible.

Examples1

  1. Example 1

    Input
    4 7
    1 2 5 10
    Q 23
    X 1
    Q 23
    X 5
    Q 23
    X 10
    Q 22
    
    Expected output
    4
    6
    -1
    11