baekjoon: 2798(블랙잭)

강지안·2023년 5월 12일
0

baekjoon

목록 보기
21/186

문제

코드

import java.util.ArrayList;
import java.util.Scanner;

public class q2798 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        ArrayList<Integer> cards = new ArrayList<>();

        for(int i=0; i<N; i++) {
            cards.add(sc.nextInt());
        }

        int max = 0;

        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                for(int k=0; k<N; k++) {
                    if(i != j && j != k && i != k) {
                        int sum = cards.get(i) + cards.get(j) + cards.get(k);
                        if(sum > max && sum <= M) max = sum;
                    }
                }
            }
        }
        System.out.println(max);
    }
}

0개의 댓글