๐ŸŽฒ ๋ฐฑ์ค€ 2512๋ฒˆ ์˜ˆ์‚ฐ

Jeongeunยท2023๋…„ 12์›” 5์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
142/186

๋ฐฑ์ค€2512๋ฒˆ

๐Ÿงธ ๊ทธ๋ž˜๋„ ์ด์ œ ์ด๋ถ„ ํƒ์ƒ‰์— ๋Œ€ํ•ด ๊ฐ์ด ์žกํžŒ ๊ฒƒ ๊ฐ™๋‹ค!

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const request = input
  .shift()
  .split(" ")
  .map(Number)
  .sort((a, b) => a - b);
const possible = +input[0];

if (request.reduce((a, b) => a + b, 0) <= possible)
  return console.log(Math.max(...request));

let left = 0;
let right = possible;

while (left <= right) {
  //์ƒํ•œ์•ก
  const mid = Math.floor((left + right) / 2);

  let total = 0;

  for (el of request) {
    if (el <= mid) total += el;
    else total += mid;
  }

  if (total > possible) right = mid - 1;
  else left = mid + 1;
}

console.log(right);

0๊ฐœ์˜ ๋Œ“๊ธ€