j33vansh

Eren and Armin Java Solution

Apr 30th, 2022 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         int t = sc.nextInt();
  8.         while(t-->0){
  9.            
  10.  
  11.         int numBooks = sc.nextInt();
  12.  
  13.         long[] books = new long[numBooks];
  14.         for (int i = 0; i < numBooks; i++) {
  15.             books[i] = sc.nextLong();
  16.         }
  17.  
  18.         Arrays.sort(books);
  19.  
  20.         long sum = 0;
  21.  
  22.         for (int j = 0; j < books.length - 1; j++) {
  23.             sum += books[j];
  24.  
  25.         }
  26.         if (sum < books[numBooks - 1]) {
  27.             System.out.println(books[numBooks - 1] * 2);
  28.         } else {
  29.             System.out.println(sum + books[numBooks - 1]);
  30.         }
  31.         }
  32.        
  33.         sc.close();
  34.  
  35.     }
  36. }
Add Comment
Please, Sign In to add comment