[백준] P2798

6964 단어 algorithmalgorithm
import java.util.Scanner;

public class P2798 {

	static int sum;
	static int max;
	static int n, m;
	static int[] arr;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		max = 0;
		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++) {
					sum = 0;
					sum += arr[i] + arr[j] + arr[k];

					if (sum <= m) {

						if (sum > max) {
							max = sum;
						}

					}

				}
			}

		}

		System.out.println(max);

		sc.close();

	}

}

좋은 웹페이지 즐겨찾기