TreeSet에 3개의 합을 저장하자.
TreeSet<Integer> Tset = new TreeSet<>(Collections.reverseOrder);
3중 for문을 이용해서 저장하도록 하자.
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int l=0;l<n;l++){
Tset.add(arr[i]+arr[j]+arr[l]);
}
}
}
int cnt = 0;
for(int x: Tset){
cnt++;
if(cnt == k)
return x;
}
package algolecture;
import java.util.Collections;
import java.util.Scanner;
import java.util.TreeSet;
public class Main35 {
public int solution(int[] arr, int n, int k) {
int answer=-1;
TreeSet<Integer> Tset = new TreeSet<>(Collections.reverseOrder());
for(int i=0;i<n;i++) {
for (int j = i + 1; j < n; j++) {
for (int l = j + 1; l < n; l++) {
Tset.add(arr[i] + arr[j] + arr[l]);
}
}
}
int cnt = 0;
for(int x : Tset){
cnt++;
if(cnt == k )
return x;
}
return answer;
}
public static void main(String[] args) {
Main35 T = new Main35();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = kb.nextInt();
}
System.out.print(T.solution(arr,n, k));
}
}