boj - 1026 보물(자바)

SlowAnd·2023년 12월 29일
0
package boj;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class boj_1026 {
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        r.readLine(); // 첫 번째 줄은 수의 개수이므로 읽고 무시

        List<Integer> read1 = Arrays.stream(r.readLine().split(" "))
                                    .map(Integer::parseInt)
                                    .sorted()
                                    .collect(Collectors.toList());
        List<Integer> read2 = Arrays.stream(r.readLine().split(" "))
                                    .map(Integer::parseInt)
                                    .sorted(Comparator.reverseOrder())
                                    .collect(Collectors.toList());

        int sum = IntStream.range(0, read1.size())
                           .map(i -> read1.get(i) * read2.get(i))
                           .sum();

        w.write(String.valueOf(sum));
        w.flush();
    }
}

0개의 댓글