"Let the games begin."
Squid Game has become a blockbuster hit and the frontman is now finding it difficult to accommodate all the participants in Squid Game 2.0. So, he decided that he will allow only those participants who could solve the following problem.
There are a total of players who are competing in the Squid Game, numbered from to . When the player gets eliminated from the game, amount of money is added to the prize pool. The game is played until players get eliminated, and the only player left is declared as the winner. The winner gets all the money present in the prize pool.
You are given an array consisting of elements, where denotes the prize money added to the prize pool when the player gets eliminated from the game. Find the maximum prize that the winner can get, given that you can choose any player to be the winner.
Input Format
- The first line of input contains a single integer , denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains an integer , denoting the number of players.
- The second line of each test case contains space-separated integers , denoting the amount of money added to the prize pool when the () player dies.
Output Format
For each test case, output in a single line the maximum prize that the winner can get, given that you can choose any player to be the winner.
Constraints
- The sum of across all test cases does not exceed
Explanation
Test Case 1:
- If we choose the first player to be the winner, he will win the game when the second and third players die. Hence, the amount of money won by him will be .
- If we choose the second player to be the winner, he will win the game when the first and third players die. Hence, the amount of money won by him will be .
- If we choose the third player to be the winner, he will win the game when the first and second players die. Hence, the amount of money won by him will be .
Therefore, we can clearly see that the maximum amount of money that can be won by any player is .
Test Case 2: Irrespective of who is chosen, the winner will always win an amount of .
Test Case 3: If we choose the sixth player to be the winner, the amount won by him will be . It can be proven that if we choose any other player to be the winner, the amount is less than . Hence, the maximum amount of money that can be won by any player is .
Solution In Java
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
int y = sc.nextInt();
int [] arr = new int[y];
for(int h=0;h<y;h++){
arr[h]=sc.nextInt();
}
Arrays.sort(arr);
int sum =0;
for(int p=1;p<arr.length;p++){
sum =sum+arr[p];
}
System.out.println(sum);
}
}
}
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++)
{
cin>>a[i];
}
sort(a,a+n);
int sum=0;
for(int i=1; i<n; i++)
{
sum+=a[i];
}
cout<<sum<<endl;
}
return 0;
}
t = int(input())
for i in range(t):
sum = 0
n = int(input())
lst = list(map(int,input().split()))
lst.sort(reverse=True)
m = min(lst)
for i in lst:
sum += i
print(sum-m)