白駿2798 JAVA

1040 ワード

acmicpc.net/status?user_id=sunnyleewin&problem_id=2798&from_mine=1
ブラックジャックの質問に答えた.
package BlackJack;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		
		int N = input.nextInt();
		int M = input.nextInt();
		int result = 0;
		
		int[] ar = new int[N];
		
		for(int i = 0; i< N; i++)
			ar[i] = input.nextInt();
		
		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 = ar[i] + ar[j] + ar[k];
					
					if(temp <= M) {
						result = temp;
					}
					
					else if(result < M && temp < M) {
						result = temp;
						
					}
					
				}
			}
		}
		
		System.out.println(result);
		
		
	
		
	}