-
Notifications
You must be signed in to change notification settings - Fork 55
/
Java.java
51 lines (40 loc) · 1.3 KB
/
Java.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
49
50
51
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Add'em Up */
/* Difficulty: Easy */
/* Date solved: 22.02.2019 */
/* */
/****************************************/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
int N = Integer.parseInt(scanner.nextLine());
ArrayList<Integer> X = new ArrayList<Integer>();
for (String s : scanner.nextLine().split(" "))
{
X.add(Integer.parseInt(s));
}
int cost = 0;
while (X.size() > 1)
{
Collections.sort(X);
//Smallest possible sum.
int minSum = X.get(0) + X.get(1);
//Add costs and reduce integer list.
cost += minSum;
X.remove(0);
X.remove(0);
X.add(minSum);
}
//Print total cost.
System.out.println(cost);
}
}