Auto-Coin-o-Matic

시간 제한2초메모리 제한2048 MB

요약
서로 다른 액면가의 동전이 시간에 따라 하나씩 제거될 때, 각 질의마다 값 v를 정확히 만드는 최소 동전 개수를 구하고 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

It's finally here! The day you unveil your new invention, the Auto-Coin-o-Matic! You watch with glee and anxiety as people insert their card into the machine, type in the amount they want, and get exact change out with the fewest number of coins.

But was it actually the fewest number of coins? That's how it was programmed, but what if you had a bug? It's okay, you're watching. You decide to randomly pick some transactions and double check that what the machine gave out is indeed the fewest number of coins possible. But, oh no, the machine is running out of certain types of coins! Will it still work correctly?

입력

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, d_1,d_2,…,d_nd\_1, d\_2, \ldots, d\_n (1≤d_i≤1051 \le d\_i \le 10^5) representing the denominations of coins available initially. It is guaranteed that all denominations are unique.

The next mm lines contain a character cc (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 the value of the event.

  • If cc is the character Q, this is a query and the output should be the minimum number of coins needed to give out exactly vv. It is guaranteed that there will be at least one query.

    If it is not possible to make vv exactly with the available denominations, output −1-1 instead.

  • If cc is the character X, this means the machine is out of coins of denomination vv. All queries after this point cannot use this denomination. It is guaranteed that each X event corresponds to a denomination vv which the machine currently has in stock.

출력

Output kk lines, where kk is the number of query events (c=c = Q). On the iith line, output the fewest number of coins needed to give change for the iith query, or −1-1 if this is impossible.

예제1

  1. 예제 1

    입력
    4 7
    1 2 5 10
    Q 23
    X 1
    Q 23
    X 5
    Q 23
    X 10
    Q 22
    
    예상 출력
    4
    6
    -1
    11