슈콩

[BOJ] 백준 1026 보물 본문

Algorithms/Baekjoon

[BOJ] 백준 1026 보물

shukong 2025. 8. 27. 15:23
재배열 부등식: 큰 값에 큰 값을 곱하면 결과가 최대, 작은 값을 곱하면 결과가 최소

 

 

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st;
    	int n = Integer.parseInt(br.readLine());
    	int[] a = new int[n];
    	int[] b = new int[n];
    	st = new StringTokenizer(br.readLine());
    	for(int i=0;i<n;i++) {
    		a[i] = Integer.parseInt(st.nextToken());
    	}
    	st = new StringTokenizer(br.readLine());
    	for(int i=0;i<n;i++) {
    		b[i] = Integer.parseInt(st.nextToken());
    	}
    	Arrays.sort(a);
    	Arrays.sort(b);
    	int result = 0;
    	for(int i=0;i<n;i++) {
    		result += a[i]*b[n-1-i];
    	}
    	System.out.println(result);
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 11399 ATM  (0) 2025.08.27
[BOJ] 백준 12865 평범한 배낭  (0) 2025.08.27
[BOJ] 백준 1931 회의실 배정  (2) 2025.08.27
[BOJ] 백준 11047 동전 0  (2) 2025.08.27
[BOJ] 백준 9657 돌 게임 3  (4) 2025.08.27