목차

Cakes

ps
링크acmicpc.net/…
출처BOJ
문제 번호7744
문제명Cakes
레벨플래티넘 3
분류

그리디

시간복잡도O(nlogn)
인풋사이즈n<=1,000,000
사용한 언어Python 3.11
제출기록88752KB / 2696ms
최고기록2696ms
해결날짜2023/07/28

풀이

코드

"""Solution code for "BOJ 7744. Cakes".

- Problem link: https://www.acmicpc.net/problem/7744
- Solution link: http://www.teferi.net/ps/problems/boj/7744

Tags: [greedy]
"""

import sys
import functools


def main():
    N = int(sys.stdin.readline())
    a_and_b = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]

    a_and_b.sort(
        key=functools.cmp_to_key(
            lambda x, y: (
                (x[0] - x[1]) - (y[0] - y[1])
                if (comp := min(x[0], y[1]) - min(x[1], y[0])) == 0
                else comp
            )
        )
    )

    prepare_time = bake_time = 0
    for a, b in a_and_b:
        prepare_time += a
        bake_time = max(bake_time, prepare_time) + b

    print(bake_time)


if __name__ == '__main__':
    main()