Question Name:Shop in Candy Store

#include <stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{int i,j,k,l=0,n,m,a[1000],s=0,s1=0,min=0,max=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	for(i=0;i<n;i++)
	{for(j=0;j<n-i-1;j++)
	{int tm;
	if(a[j]>a[j+1])
	{tm=a[j];
	a[j]=a[j+1];
	a[j+1]=tm;}}}
	while(s<n)
	{min=min+a[l];
	l++;
	s=s+k+1;}
	j=n-1;
		while(s1<n)
	{max=max+a[j];
	j--;
	s1=s1+k+1;}
	printf("%d ",min);
	printf("%d\n",max);}
	return 0;
}

Problem Description

In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you.
You are now provided with an attractive offer.
You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free.
Now you have to answer two questions. Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies.
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.

Input

The first line of the input contains T the number of test cases. Each test case consists of two lines.
The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies.

Output

For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent.
Remember to output the answer of each test case in a new line.

Constraints
1 <= T <= 50
1 <= N <= 1000
0 <= K <= N-1
1 <= Ai <= 100

Expected Time Complexity : O(nlogn)

  • Test Case 1

    Input (stdin)

    1
    4  2
    3 2 1 4
    

    Expected Output

    3 7
  • Test Case 2

    Input (stdin)

    2
    6 2
    5 4 2 6 1 8
    9 4
    2 7 1 6 8 3 12 10 11
    

    Expected Output

    3 14
    3 23

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro