
정렬하고 삼각형 성질인 a+b>c만 만족하면 sum 값을 구하면 되는 문제이다.
시간복잡도:O(logN), 공간복잡도:O(N)
- [ x ] 1회
- 2회
- 3회
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int [] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
int sum = 0;
boolean check = false;
for(int i=n-1;i>=2;i--){
int a = arr[i-2];
int b = arr[i-1];
int c = arr[i];
if(a+b>c){
sum=a+b+c;
check = true;
break;
}
}
if(check){
System.out.println(sum);
}else{
System.out.println(-1);
}
}
}
