Timer is Running

Given an array of** N** numbers, we need to maximize the sum of selected numbers. At each step, you need to select a number A_{i}, delete one occurrence of **A _{i}-1 (if exists) and A_{i}** each from the array. Repeat these steps until the array gets empty. The problem is to maximize the sum of selected numbers.

**Input:**

The first line of the input contains **T ** denoting the number of the test cases. For each test case, 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

**Note:** Numbers need to be selected from maximum to minimum.

**Example:
Input**

2

3

1 2 3

6

1 2 2 2 3 4

4

10

**Explanation
#Testcase 1:**

At first step we select 3, so 3 and 2 are deleted from the sequence leaving us with 1. Then we select 1 from the sequence and delete it. So the sum of selected numbers is 1+3 = 4.

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.

Author: vanshi_14

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

abnandi04 | 801 |

KshamaGupta | 412 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

anshusingh20112001 | 181 |

abnandi04 | 776 |

KshamaGupta | 296 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

afreensartaj131 | 170 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4748 |

Login to report an issue on this page.