TopcoderArchive
Archive
Algorithm problem

CheapestGifts

Problem statement, definition, constraints, and public examples.

Problem Statement

You have k friends and you would like to buy each of them a gift. Since you are short of money, you would like to minimize the total cost.

You go to a store that has n different products. There are exactly m of each product available in stock. Given a int[] prices containing the price of each product, return the minimum possible total cost to buy k gifts. If there are not enough gifts in the store, return -1.

Definition

Class:
CheapestGifts
Method:
getPrice
Parameters:
int[], int, int
Returns:
int
Method signature:
int getPrice(int[] prices, int m, int k)
(be sure your method is public)

Constraints

  • prices will contain between 1 and 50 elements, inclusive.
  • Each element of prices will be between 1 and 1000, inclusive.
  • m will be between 1 and 1000, inclusive.
  • k will be between 1 and 100,000, inclusive.

Examples

  1. {1,3,2}
    2
    3
    Returns: 4
    You can buy two gifts with a price of 1 each and one gift with a price of 2 for a total cost of 4.
  2. {1}
    2
    3
    Returns: -1
    There is only one product and the store only has two of it in stock. You need three gifts, so you return -1.
  3. {1, 2, 3, 4, 5, 6, 7}
    1
    7
    Returns: 28
    In this case you must buy all the gifts in the store.
Back to all problems