| 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()