from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
a.sort()
for i in range(n):
tmp = max(b)
s += a[i] * tmp
b.pop(b.index(tmp))
print(s)
a
의 최솟값과 b
의 최댓값을 곱해서 더하면 s
를 최소화할 수 있다. 문제를 보고 b
는 정렬하면 안 되는 줄 알고 최댓값을 찾은 다음 제거하는 식으로 풀었다.
그러나 다시 생각해 보니 상관 없어서 b
역시 내림차순으로 정렬해 다시 풀었다.
from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
a.sort()
b.sort(reverse=True)
for i, j in zip(a, b):
s += i * j
print(s)
시간은 처음 풀이가 68ms, 두 번째 풀이가 72ms가 나왔다. 별 차이 없지만, zip
을 써보고 싶었다.