본문 바로가기
Problem Solving/Baekjoon

[백준] 15008 Falling Apart - Greedy / Java

by graycode 2024. 3. 16.

 문제 링크

 

15008번: Falling Apart

After acquiring a new integer and showing it off to other couples at a cocktail party, Alice and Bob headed home for a good night of sleep. As their integer was quite large, they were forced to carry it together. Then, on the Boole Boulevard, right by the

www.acmicpc.net

 

 풀이 코드

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 = read();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) arr[i] = read();

        Arrays.sort(arr);

        int e = 0, o = 0;
        while (n-- > 0) {
            if ((n & 1) == 0) e += arr[n];
            else o += arr[n];
        }

        bw.write(e > o ? e + " " + o : o + " " + e);
        bw.flush();
    }

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

        return n;
    }

}

댓글