#K8561. Minimum Cost to Buy K Different Priced Candies

    ID: 36680 Type: Default 1000ms 256MiB

Minimum Cost to Buy K Different Priced Candies

Minimum Cost to Buy K Different Priced Candies

You are given an integer n and an integer k along with a list of n candy prices. Your task is to determine the minimum total cost required to purchase k candies with distinct prices.

If it is not possible to purchase k candies with different prices, output -1.

Formally, let the list of candy prices be \(P\), and let \(S\) be the set of distinct prices (i.e. \(S \subseteq P\)). If \(|S| \geq k\), you need to select the smallest k elements from \(S\) such that their sum is minimized. Otherwise, return \(-1\).

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains two integers n and k where n is the number of candies and k is the number of candies to purchase.
  • The second line contains n space-separated integers representing the prices of the candies.

outputFormat

Output a single integer to standard output (stdout): the minimum total cost to buy k candies with different prices, or -1 if it is impossible.

## sample
7 3
5 1 3 3 2 4 1
6