본문 바로가기
Problem Solving/Baekjoon

[백준] 13472 Sticky Situation - Greedy / Java

by graycode 2024. 8. 12.

 문제 링크

https://www.acmicpc.net/problem/13472

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = (int) read();
        long[] arr = new long[n];
        while (n-- > 0) arr[n] = read();

        Arrays.sort(arr);

        bw.write(isPossible(arr));
        bw.flush();
    }

    private static String isPossible(long[] arr) {
        for (int i = 2; i < arr.length; i++) if (arr[i - 2] + arr[i - 1] > arr[i]) return "possible";

        return "impossible";
    }

    private static long read() throws IOException {
        long c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글