## [f]izzbuzzer

### Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

# Marc’s Cakewalk challenge

Problem statement
Marc loves cupcakes, but he also likes to stay fit. He eats cupcakes in one sitting, and each cupcake has a calorie count, . After eating a cupcake with calories, he must walk at least (where is the number cupcakes he has already eaten) miles to maintain his weight.

Given the individual calorie counts for each of the cupcakes, find and print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order.

Input Format
The first line contains an integer, , denoting the number of cupcakes.
The second line contains space-separated integers describing the respective calorie counts of each cupcake, .

Constraints

Output Format
Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight.

Sample Input 0

3
1 3 2


Sample Output 0

11


Explanation 0
Let’s say the number of miles Marc must walk to maintain his weight is . He can minimize by eating the cupcakes in the following order:

1. Eat the cupcake with calories, so .
2. Eat the cupcake with calories, so .
3. Eat the cupcake with calories, so .

We then print the final value of , which is , as our answer.

Solution

public class MarcsCakewalk {

public static void main(String[] args) throws FileNotFoundException {
System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt"));
Scanner in = new Scanner(System.in);

StringBuilder sb = new StringBuilder();
int n = in.nextInt();

int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = in.nextInt();
}

Arrays.sort(c);

long miles = 0;
for (int i = 0; i < n; i++) {
miles += Math.pow(2, i) * c[n - i - 1];
}

System.out.println(miles);
}

}