-
Notifications
You must be signed in to change notification settings - Fork 858
/
MarcsCakewalk.java
48 lines (43 loc) · 1.01 KB
/
MarcsCakewalk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
*
* Problem Statement-
* [Marc's Cakewalk](https://www.hackerrank.com/challenges/marcs-cakewalk)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.sorting;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class MarcsCakewalk {
public static int[] sort(int a[]) {
for (int i = 1; i < a.length; i++) {
for (int j = i; j > 0; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
} else
break;
}
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] calories = new int[n];
for (int calories_i = 0; calories_i < n; calories_i++) {
calories[calories_i] = in.nextInt();
}
int sortedArray[] = sort(calories);
int j = 0;
long sum = 0;
for (int i = sortedArray.length - 1; i >= 0; i--) {
sum = (sum + sortedArray[i] * (long) Math.pow(2, j++));
}
System.out.println(sum);
in.close();
}
}