๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”‘ PS/BOJ

boj-1744 ์ˆ˜ ๋ฌถ๊ธฐ

by bdd 2022. 7. 15.
 

1744๋ฒˆ: ์ˆ˜ ๋ฌถ๊ธฐ

๊ธธ์ด๊ฐ€ N์ธ ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ์ˆ˜์—ด์˜ ํ•ฉ์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ๊ทธ๋ƒฅ ๊ทธ ์ˆ˜์—ด์˜ ํ•ฉ์„ ๋ชจ๋‘ ๋”ํ•ด์„œ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ˆ˜์—ด์˜ ๋‘ ์ˆ˜๋ฅผ ๋ฌถ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์–ด๋–ค ์ˆ˜๋ฅผ ๋ฌถ์œผ๋ ค๊ณ  ํ•  ๋•Œ, ์œ„์น˜์—

www.acmicpc.net

 

 

 

 

 

 

ํ’€์ด


๋ฐ˜๋ก€ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๊ณ ์ƒํ–ˆ๋˜ ๋ฌธ์ œ์ธ๋ฐ์š”, ์ผ€์ด์Šค๋ณ„๋กœ ๋ฌถ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ธ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๊ธฐ์ค€์„ 1๋กœ ์‚ผ์•„์•ผ ํ•˜๋Š”๋ฐ์š”, 1์€ ๋‹ค๋ฅธ ์ˆ˜์™€ ๊ณฑํ•˜๋ฉด ์ž๊ธฐ ์ž์‹ ์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์กฐ๊ฑด ๋”ํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1 ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ, 1์ผ ๊ฒฝ์šฐ, 1์ดˆ๊ณผ์ผ ๊ฒฝ์šฐ๋กœ ์ด๋ฅผ ๊ฐ๊ฐ ๋ถ„๋ฅ˜ํ•ด์ค€ ๋’ค ๋‘ ๊ฐœ์”ฉ ๋ฌถ์–ด์„œ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜/ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ ๋‘๊ฐœ์”ฉ ๋ฌถ๊ณ ๋‚˜์„œ ๋งˆ์ง€๋ง‰ ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

public class Main {
    public static void main(String[] args) throws Exception {
        int n = input.integer();
        List<Integer> plus = new ArrayList<>();
        List<Integer> others = new ArrayList<>();
        List<Integer> minus = new ArrayList<>();

        for (int index = 0; index < n; index++) {
            int number = input.integer();
            if (number > 1) {
                plus.add(number);
                ;
            } else if (number == 1) {
                others.add(number);
            } else {
                minus.add(number);
            }
        }
        
        Collections.sort(plus, Collections.reverseOrder());
        Collections.sort(others);
        Collections.sort(minus);

        int sum = 0;

        if (plus.size() != 0) {
            if (plus.size() % 2 != 0) {
                for (int index = 0; index < plus.size() - 1; index += 2) {
                    sum += plus.get(index) * plus.get(index + 1);
                }
                sum += plus.get(plus.size() - 1);
            } else {
                for (int index = 0; index < plus.size(); index += 2) {
                    sum += plus.get(index) * plus.get(index + 1);
                }
            }
        }
        
        if (others.size() != 0) {
            for (int index = 0; index < others.size(); index++) {
                sum += others.get(index);
            }
        }
        
        if (minus.size() != 0) {
            if (minus.size() % 2 != 0) {
                for (int index = 0; index < minus.size() - 1; index += 2) {
                    sum += minus.get(index) * minus.get(index + 1);
                }
                sum += minus.get(minus.size() - 1);
            } else {
                for (int index = 0; index < minus.size(); index += 2) {
                    sum += minus.get(index) * minus.get(index + 1);
                }
            }
        }
        System.out.println(sum);


    }

    static Input input = new Input();

    static class Input {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        public int integer() throws Exception {
            if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
            return Integer.parseInt(st.nextToken());
        }

        public String next() throws Exception {
            if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public char[] nToCharArray() throws Exception {
            if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
            return st.nextToken().toCharArray();
        }
    }
}

'๐Ÿ”‘ PS > BOJ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

boj-1916  (0) 2022.08.24
boj-2252  (0) 2022.08.22
boj-1850  (0) 2022.07.23
boj-1922 ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ  (0) 2022.07.19
boj-1300 K๋ฒˆ์งธ ์ˆ˜  (0) 2022.07.14

๋Œ“๊ธ€