Skip to content

Instantly share code, notes, and snippets.

@pungrue26
Created October 31, 2016 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pungrue26/507bf26b45697cc91bbac5fdb30ab8d0 to your computer and use it in GitHub Desktop.
Save pungrue26/507bf26b45697cc91bbac5fdb30ab8d0 to your computer and use it in GitHub Desktop.
Mandragora Forest in HackerRank
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
long [] array = new long [n];
for (int j = 0; j < n; j++) {
array[j] = in.nextLong();
}
Arrays.sort(array);
for (int j = 1; j < n; j++) {
array[j] += array[j - 1];
}
long max = array[n - 1];
for (int j = 0; j < n; j++) {
max = Math.max(max, (array[n - 1] - array[j]) * (j + 2));
}
System.out.println(max);
}
in.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment