The Squid Game - Codechef Solution

"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 N players who are competing in the Squid Game, numbered from 1 to N. When the ith player gets eliminated from the game, Ai amount of money is added to the prize pool. The game is played until N1 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 A consisting of N elements, where Ai denotes the prize money added to the prize pool when the ith 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 T, denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains an integer N, denoting the number of players.
  • The second line of each test case contains N space-separated integers A1,A2,,AN, denoting the amount of money added to the prize pool when the ith (1iN) 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

  • 1T1000
  • 2N105
  • 0Ai104
  • The sum of N across all test cases does not exceed 
Sample Input

3
3
3 1 2
5
1 1 1 1 1
6
3 6 4 2 5 1

Sample Output

4
20


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 1+2=3.
  • 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 3+2=5.
  • 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 3+1=4.

Therefore, we can clearly see that the maximum amount of money that can be won by any player is 5.

Test Case 2: Irrespective of who is chosen, the winner will always win an amount of 4.

Test Case 3: If we choose the sixth player to be the winner, the amount won by him will be 3+6+4+2+5=20. It can be proven that if we choose any other player to be the winner, the amount is less than 20. Hence, the maximum amount of money that can be won by any player is 20.


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);
       
		        }
		        }
             } 
        	

Solution in C++

 #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;
}
              

Solution in Python
 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)

Admin

Hi This is the Admin of CodingSoln. Currently Pursuing B. Tech Computer Science and Engineering form KIIT University India

Post a Comment

Previous Post Next Post