Daily Challenge #80

There are M items in the store.
James wants to buy the most expensive items as possible.
He has N dinars (can be any currency, really doesn’t matter).
He can buy the same item multiple times.


The user should input two integers, N (how much money he has) & M (how many items are in the store) as well as a space-separated string (prices of items).
The output should be how many dinars he has left after buying as much and as expensive things as he can.


Example:

INPUT:                                   OUTPUT: 
1250                                     10
5
1010 357 725 1125 115	      

Explanation:
He has 1250 RSD, he buys one item at the price of 1125 RSD, he now has 125RSD left.
He can’t buy the items for 1010, 725, nor 357, so he buys the one at the price of 115 RSD. He now has 10 RSD left.
He can’t buy anything anymore so he exits the store.

Another example:

INPUT:                                   OUTPUT:
10000                                    0
6
3010 3005 5725 1265 2075 385

So James begins always with the most expensive and looks what is the next expensive?

Really tricky challenge though :+1: :+1: :+1:

Yes!

Is it? I always solve it yellow before posting. It took me around 20 minutes.

EDIT: How the heck did ‘yellow’ show up? I typed ‘myself’, probably autocorrect or Smith :unamused:

EDIT 2: NO, not ‘Smith’, something! :unamused:

1 Like

No, I mean the idea is nice and challenging for thinking of a solution :wink:

1 Like

Hey, another question: can the string contain unlimited items?

The string contains M items.

1 Like

Hmmm. This kind of questions are very special.
So what she be input?
A string with multiple value?

exactly.

OP’s question

1 Like

N, M & the prices.

What happens if there is a price which is more expensive than he has money? Do we have to include this in our kode too?

He doesn’t buy it?

1 Like

Then James won’t buy it…

1 Like

Is he super rich or something? :joy:

2 Likes

Here is one of my solution, I think I will post another one later (cuz this is not the best solution)

My solution


The initialize itemInStore will sort the list of the item in the store by descending.

Total = 64 blocks
Proof of functionality:

2 Likes

Olalala. Okay okay. I have a much easier solution but this works too! Can I get proof of functionality?

2 Likes

wait a moment :slightly_smiling_face:

1 Like

My Solution:

Solution

I can’t open it, it is a spoiler on clickable text. Remove the spoiler and it will work.

1 Like

Check now!

Nice! Try making your own method for sorting a list :wink: