CodeLabs

[백준 / C#] 2805 : 나무 자르기 본문

백준/1000 ~

[백준 / C#] 2805 : 나무 자르기

무오_ 2023. 8. 9. 22:59
using System;

namespace _2805
{
    class Program
    {
        static long[] N_M, arr;
        static long maxValue;
        static bool Chack(long mid, long[] arr)
        {
            long cnt = 0;
            for (long i = 0; i < N_M[0]; i++)
                cnt += Math.Max(arr[i] - mid, 0);
            return cnt >= N_M[1];
        }
        static void Solution(long[] arr, long wood)
        {
            long left = 1;
            long result = 0;
            while(left<=maxValue)
            {
                long mid = (left + maxValue) / 2;
                if (Chack(mid, arr))
                {
                    result = mid > result ? mid : result;
                    left = mid + 1;
                }
                else maxValue = mid - 1;
            }
            Console.WriteLine(result);
        }
        static void Input()
        {
            N_M = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
            arr = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
            Array.Sort(arr);
            maxValue = arr[N_M[0] - 1];
        }
        static void Solve()
        {
            Input();
            Solution(arr, N_M[1]);
        }
        static void Main(string[] args)
        {
            Solve();
        }
    }
}