A Coin Problem

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 2.0s
Memory limit: 32M
PyPy 2 128M
PyPy 3 128M

Author:
Problem type

Angie is going shopping!

She has N different types of coins in her pocket, with the i^{th} type of coin being worth d_i dollars, and she's going to V stores. From the j^{th} store, she wants to buy c_j dollars worth of stuff from store j, but store j only accepts the first l_j types of coins in her pocket!

Can you help her figure out the minimum number of coins to pay for each transaction in exact change?

Note that for the purpose of this problem, Angie has an infinite number of each type of coin.

Constraints

1 \le l_j \le N \le 2 \times 10^3

1 \le V \le 10^5

1 \le d_i, c_j \le 10^4

Input Specification

The first line of input consists of N and V, separated by a space.

The next line contains N space separated integers containing the values d_i (d_1, d_2, d_3, ... d_N).

The next V lines of input each contain the space separated integers c_j and l_j.

Output Specification

There are V lines of output, with each line containing the minimum amount of coins needed to satisfy the payment in the j^{th} transaction.

If the j^{th} transaction cannot be satisfied however, print -1.

Note for Python users: To pass this question using Python you must select the PyPy interpreter instead of the normal one.

Sample Input 1

6 3
7 10 15 2 3 24
107 3
12 4
24 2

Sample Output 1

8
2
3

Explanation

Transaction 1: 6 coins with $15, 1 coin with $10, and 1 coin worth $7.

Transaction 2: 1 coin worth $10, and 1 coin worth $2.

Transaction 3: 1 coin worth $10, and 2 coins worth $7.

All that needs to be noted here is that for the third transaction, even though she has a coin worth $24 the third store won't accept it.


Sample Input 2

4 3
11 15 3 1
6 2
15 1
7 4

Sample Output 2

-1
-1
3

Explanation

Transactions 1 and 2: Not possible.

Transaction 3: 2 coins worth $3, and 1 coin worth $1.

Note that the first transaction cannot be completed without the third type of coin and the second one cannot be completed without the second type of coin.


Comments


  • 0
    AryanG  commented on Aug. 3, 2019, 7:11 p.m. edited

    I'm getting a TLE in c after problem 2. Are there any ways to improve the algorithm.


    • 0
      4fecta  commented on Aug. 4, 2019, 10:19 a.m.

      There are indeed many ways to improve your algorithm. Try to work out the time complexity to see why you TLE, and how you should fix it.


      • 0
        AryanG  commented on Aug. 4, 2019, 2:30 p.m.

        My current runtime is O(mV) where m is the number of coins used and V is the target to achieve.


        • 0
          4fecta  commented on Aug. 4, 2019, 4:43 p.m. edit 2

          What do you mean by "target to achieve"? If you mean the cost of an item, please note that there are multiple queries for this question. Running an O(n^2) algorithm for each item would be insufficient.


          • 0
            AryanG  commented on Aug. 5, 2019, 7:38 p.m.

            The algorithm described is for each query. Each query takes O(mV) time.


  • 0
    explosion  commented on April 9, 2019, 8:50 p.m.

    why am i getting invalid return on the first case?

    idk my code works for the two sample inputs for some reason aaa


  • 4
    Blazebot99  commented on Feb. 12, 2019, 12:03 p.m.

    Still TLEing in PyPy3, any way to improve my algorithm? Thanks in advance!