code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class baekjoon25707 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N,result=0;
static ArrayList<Integer> list =new ArrayList<>();
public static void main(String args[]) throws IOException{
N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
while(st.hasMoreTokens()){
list.add(Integer.parseInt(st.nextToken()));
}
Collections.sort(list,Collections.reverseOrder());
for(int i=0;i<N;i++){
if(i==N-1){
result+=list.get(0) - list.get(i);
}else{
result+=list.get(i)-list.get(i+1);
}
}
System.out.print(result);
}
}
그냥 정렬해서 뺄셈해주면 된다.
팔찌니까 맨마지막구슬은 맨처음구슬과 꿰면 된다.