ABA12C - Buying Apples!

Harish went to a supermarket to buy exactly ‘k’ kilograms apples for his ‘n’ friends. The supermarket was really weird. The pricing of items was very different. He went to the Apples section and enquired about the prices. The salesman gave him a card in which he found that the prices of apples were not per kg. The apples were packed into covers, each containing ‘x’ kg of apples, x > 0 and ‘x’ is an integer. An ‘x’ kg packet would be valued at ‘y’ rupees. So, the placard contained a table with an entry ‘y’ denoting the price of an ‘x’ kg packet. If ‘y’ is -1 it means that the corresponding packet is not available. Now as apples are available only in packets, he decides to buy atmost ‘n’ packets for his ‘n’ friends i.e he will not buy more than n packets of apples.

Harish likes his friends a lot and so he does not want to disappoint his friends. So now, he will tell you how many friends he has and you have to tell him the minimum amount of money he has to spend for his friends.


The first line of input will contain the number of test cases, C.

Each test case will contain two lines.

The first line containing N and K, the number of friends he has and the amount of Apples in kilograms which he should buy.

The second line contains K space separated integers in which the ith integer specifies the price of a ‘i’kg apple packet. A value of -1 denotes that the corresponding packet is unavailable.

  • 0 < N <= 100
  • 0 < K <= 100
  • 0 < price <= 1000


The output for each test case should be a single line containing the minimum amount of money he has to spend for his friends. Print -1 if it is not possible for him to satisfy his friends.

Sample I/O

3 5
-1 -1 4 5 -1
5 5
1 2 3 4 5


Explanation of test cases:

1) As there are only 3 and 4kg packets in the shop, he will not be able to satisfy his friends as he would not be able to buy exactly 5kg of apples.

2) He can buy five 1kg packet as he has to buy 5 kg. So the min money he should spend is 5.

hide comments
jkks1234: 2017-07-01 22:27:46

N makes no sense here .you can solve without considering the statement " he decides to buy atmost ‘n’ packets for his ‘n’ friends i.e he will not buy more than n packets of apples".

Adarsh K: 2017-06-23 12:03:07

Considering n give me the wrong answer. After removing n from the terminating if condition it accepted

sandeep_4141: 2017-06-12 16:00:39

n means non-sense :)
just solve unbound knapscak !!

ekjot_kaur281: 2017-06-12 10:23:33

Can he buy a packet of x kg more than 1 time or not ?

mishra10: 2017-06-01 15:59:43

Don't solve this very wrongly framed question..Ignore and move to other question..n plays no part

suraj_: 2017-05-18 17:35:07

it is similar to unbounded knapsack

akshayvenkat: 2017-05-10 02:12:03

Its a simple 1D problem , @big_o_logn !
Given a number k , find out the cheapest way to produce an exact sum of k , using k different values which can be duplicated if needed.

big_o_logn: 2017-05-08 00:46:54

Can anyone tell me what is wrong with my algorithm?

I created a 2D memo table storing the index (starts at 0, so the current # of bag being bought minus 1) and number of kilograms of apples bought: int[][] memo = new int[n+1][K+1];

For each state, I loop through all available bag sizes and choose the solution with the minimum cost such that the required kilograms are bought with the required number of bags.

Base cases:
if(index == n) // nth bag has just been bought
return 0 if kilograms == K or Integer.MAX_VALUE otherwise.

if(kilograms > K) return Integer.MAX_VALUE; //invalid solution

if (memo table has a solution), return that stored solution; //Dynamic programming

My code worked for both sample test cases. Are there any weird edge cases that I am leaving out?

mishmetall: 2017-05-06 23:47:29

Terrible problem. As other says, solution for AC does not consider N and is classic cut rope problem, however it should, as for problem
3 5
100 40 1 1 2
it should return 180, not 2 (my solution that was AC).

micro14: 2017-05-06 15:21:44

AC in 1 go!!!!

Added by:Kashyap Krishnakumar
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own problem