본문 바로가기
알고리즘 & 자료구조/백준

백준 2798

by 신재권 2021. 11. 5.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main2798 {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int N = Integer.parseInt(st.nextToken()); // 카드의 개수
		int M = Integer.parseInt(st.nextToken()); // 카드의 합 
		
		st = new StringTokenizer(br.readLine());  // 카드 숫자 입력
		int[] card = new int[N];
		
		for(int i=0; i<N; i++){
			card[i] = Integer.parseInt(st.nextToken());
		}
		int result = 0;
		first:for(int i=0; i<N-2; i++){
			for(int j=i+1; j<N-1; j++){
				for(int k =j+1; k<N; k++){
					int temp = card[i] + card[j] + card[k];
					if(temp == M){
						result = temp;
						break first;
					}
					if(result < temp && temp < M){
						result = temp;
					}
				}
			}
		}
		System.out.println(result);

	}

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1268  (0) 2021.11.13
백준 1260  (0) 2021.11.07
백준 1931  (0) 2021.11.03
백준 11047  (0) 2021.11.02
백준 5585  (0) 2021.11.01