We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4f6803b commit 4db6900Copy full SHA for 4db6900
minjeong/BinarySearch/2025-05-05-[백준]-#2512-예산.py
@@ -0,0 +1,37 @@
1
+import sys
2
+
3
+input = sys.stdin.readline
4
5
+def main():
6
+ N = int(input()) # 지방의 수
7
+ budgets = sorted(map(int, input().split())) # 각 지방의 예상요청
8
+ M = int(input()) # 총 예산
9
10
+ # 1. 모든 요청이 배정될 수 있는 경우, 요청 최대값이 정답
11
+ if sum(budgets) <= M:
12
+ return budgets[-1]
13
14
+ # 2. 모든 요청이 배정될 수 없는 경우, 상한액 탐색
15
+ left = 0
16
+ right = budgets[-1]
17
+ answer = 0
18
19
+ while left <= right:
20
+ mid = (left + right) // 2 # 상한액
21
+ total = 0
22
+ for b in budgets:
23
+ total += min(b, mid)
24
25
+ if total <= M:
26
+ # 이 상한액으로 배정해도 총액이 허용 범위 이내 → C를 더 높여 볼 수 있음
27
+ answer = mid
28
+ left = mid + 1
29
+ else:
30
+ # 총액이 초과 → 상한액을 낮춰야 함
31
+ right = mid - 1
32
33
+ return answer
34
35
36
+if __name__ == '__main__':
37
+ print(main())
0 commit comments