## A Coin Problem

View as PDF

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

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Angie is going shopping!

She has different types of coins in her pocket, with the type of coin being worth dollars, and she's going to stores. From the store, she wants to buy dollars worth of stuff from store , but store only accepts the first 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.

#### Input Specification

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

The next line contains space separated integers containing the values ().

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

#### Output Specification

There are lines of output, with each line containing the minimum amount of coins needed to satisfy the payment in the transaction.

If the 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: coins with , coin with , and coin worth .

Transaction 2: coin worth , and coin worth .

Transaction 3: coin worth , and coins worth .

All that needs to be noted here is that for the third transaction, even though she has a coin worth 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: coins worth , and coin worth .

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.

• commented on July 13, 2020, 8:26 p.m.

Any explanation as to why I'm getting Java Out of Memory error for my code? Source: https://dmoj.ca/src/2202142.

• commented on July 14, 2020, 12:01 p.m. edited

You use too much memory

• commented on Feb. 5, 2020, 2:20 a.m. edited

My submission got "failed initialising" on c++17, but my code runs fine in visual studio. Does anyone know what I can do about this?

• commented on Feb. 5, 2020, 8:00 a.m.

Your int dpTable[2 * 1000][10001] uses 2000 x 10001 x sizeof(int) = ~80MB, which by itself already exceeds the 32MB memory limit.

• 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.

• 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.

• 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.

• 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() algorithm for each item would be insufficient.

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

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

• 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

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

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

• commented on Oct. 24, 2019, 7:08 a.m. edited

tle with pypy3 but ac (in 23sec) with pypy2, with the very same code... i suppose that the time limit is not for pypy3, and/or you expected to do some magic...

• commented on Oct. 24, 2019, 8:51 a.m. edited

No, you just got unlucky with the judges. Resubmit a couple of times and it'll AC.

Same thing happens with PyPy 2, might I add.

• commented on Oct. 24, 2019, 8:01 a.m. edited

My code gets worst case ~1.3s with PyPy3