import sys
input = sys.stdin.readline

# 3
# 1 8   # (개수, 우유양)
# 2 5
# 1 2


N = int(input())
cows = []

for _ in range(N):
    x, y = map(int, input().split())
    cows.append([y, x])   # (우유양, 개수)

# 우유양 기준 정렬
cows.sort()

left = 0
right = N - 1
answer = 0

while left <= right:
    y1, c1 = cows[left]
    y2, c2 = cows[right]

    # 현재 쌍의 시간
    answer = max(answer, y1 + y2)

    # 만들 수 있는 최소 쌍 수
    use = min(c1, c2)

    cows[left][1] -= use
    cows[right][1] -= use

    if cows[left][1] == 0:
        left += 1
    if cows[right][1] == 0:
        right -= 1

print(answer)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: