15,306,676 members

See more:

While attending Geeks Classes, Shivani went through a question in which an array was given and she was asked to maximize the sum by picking some elements from the array so that after selecting an element A[i] from array, delete one occurrence of A[i]-1 (If array size becomes 1, you can include that single element in sum). But, she got stuck and asked you for help. Help Shivani in solving this question.

Note : Elements to be selected are from maximum to minimum.

Input :

The first line of the input contains T denoting the number of the test cases. For each test case T, the first line contains an integer N denoting the size of the array. Next line contains N space separated integers denoting the elements of the array.

Output :

For each test case, the output is an integer displaying the maximum sum of selected numbers.

Constraints :

1 <= T <= 100

1 <= n <= 50

1 <= A[i] <= 20

Example :

Input

2

3

1 2 3

6

1 2 2 2 3 4

Output

4

10

**What I have tried:**

Please Help Me Out..!!

Just Give me the explanation.

Note : Elements to be selected are from maximum to minimum.

Input :

The first line of the input contains T denoting the number of the test cases. For each test case T, the first line contains an integer N denoting the size of the array. Next line contains N space separated integers denoting the elements of the array.

Output :

For each test case, the output is an integer displaying the maximum sum of selected numbers.

Constraints :

1 <= T <= 100

1 <= n <= 50

1 <= A[i] <= 20

Example :

Input

2

3

1 2 3

6

1 2 2 2 3 4

Output

4

10

Please Help Me Out..!!

Just Give me the explanation.

We see this sort of thing so often that @originalgriff wrote an article about how to start ... start here How to Write Code to Solve a Problem, A Beginner's Guide[^]

Comments

Snap! :laugh:

+5

+5

I went for the terse version :laugh:

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

And part of the homework is getting you to work out how to analyze a problem and come up with solutions - so "explaining it" to you really wouldn't help you in the long term!

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!

Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and think about how you would do it manually, given a set of numbers on a sheet of paper.

And part of the homework is getting you to work out how to analyze a problem and come up with solutions - so "explaining it" to you really wouldn't help you in the long term!

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!

Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and think about how you would do it manually, given a set of numbers on a sheet of paper.

v2

Comments

I'm not asking you the code. I just need a better explanation. This is the only reason, why I uploaded the question here.

A better explanation of what?

The homework assignment is pretty clear - try it on paper and you'll see what it means. If you still can't understand it, you need to talk to your teacher and get him to clarify things for you.

The homework assignment is pretty clear - try it on paper and you'll see what it means. If you still can't understand it, you need to talk to your teacher and get him to clarify things for you.

Quote:While attending Geeks Classes, Shivan ...

First of all, this comes from a challenge site, it is a good idea to give link to original page, because in those problems, exact wording matters.

Quote:How can I solve this coding problem.

The first step is to solve the problem by hand with a pencil and sheet of paper.

When you are familiar with the procedure you use to solve, it is basically your algorithm,you should be able to start write code.

Quote:Just Give me the explanation.

Have a look at Maximize the sum of selected numbers from an array to make it empty | Practice | GeeksforGeeks[^]

Comments

I must be very stupid, as the explanation does not make sense to me.

My understanding is

until array is empty : Take one maximum N and remove one N-1 if any.

until array is empty : Take one maximum N and remove one N-1 if any.

I understand that part. What I don't understand is how they get the sum at the end.

Sum of all taken values.

Quote: We select 4, so 4 and 3 are deleted leaving us with {1,2,2,2}.

Then we select 2, so 2 & 1 are deleted. We are left with{2,2}.

We select 2 in next two steps, thus the sum is 4+2+2+2=10.

Doesn't make sense to me.

Then we select 2, so 2 & 1 are deleted. We are left with{2,2}.

We select 2 in next two steps, thus the sum is 4+2+2+2=10.

Doesn't make sense to me.

Quote: At each step, you need to select a number Ai, delete one occurrence of Ai-1 (if exists), and Ai each from the array.

add 4 to sum and remove the 4 and a 3 from array.

it is the word "select"

add 4 to sum and remove the 4 and a 3 from array.

it is the word "select"

Thanks Patrice. I must learn to read (and understand) English. :(

Sometimes I am tired too :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900