[Algorithm] [백준] 1026 - 보물 (그리디)

myeonji·2022년 3월 9일
0

Algorithm

목록 보기
72/89

s가 최소가 되려면, a배열에서 가장 작은수와 b배열에서 가장 큰 수를 곱해야 한다.

import sys
input = sys.stdin.readline

# A 배열에서 가장 작은 수와 B 배열에서 가장 큰 수를 곱하고, 사용한 것은 pop하는 방식으로 계산

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

result = 0
for _ in range(n):
    a_min = min(a)
    b_max = max(b)
    result += (a_min*b_max)
    a.pop(a.index(a_min))
    b.pop(b.index(b_max))
print(result)

위의 코드는 max와 min 함수를 이용해서 풀었다.

혹은

b배열은 정렬하면 안되므로 a배열만 정렬해서 풀 수도 있다.

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a.sort()

result = 0
for i in range(n):
    result += a[i]*b.pop(b.index(max(b)))
print(result)

0개의 댓글