Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 717 Bytes

2512.md

File metadata and controls

41 lines (33 loc) · 717 Bytes

예산

문제

https://www.acmicpc.net/problem/2512

정답

let [n, arr, m] = require("fs")
  .readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
  .toString()
  .split("\n");

arr = arr
  .split(" ")
  .map(Number)
  .sort((a, b) => a - b);

// 이진 탐색을 위한 시작과 끝점 설정
let start = 1;
let end = arr[arr.length - 1];
let result = 0;

while (start <= end) {
  // 이진 탐색 수행
  let mid = Math.floor((start + end) / 2); // 현재의 중간점 (상한액)
  let total = 0;
  for (x of arr) {
    total += Math.min(mid, x);
  }
  if (total <= m) {
    result = mid;
    start = mid + 1;
  } else {
    end = mid - 1;
  }
}

console.log(result);