Problem:
Score : 500 points

Problem Statement

We have N logs of lengths A_1,A_2,\cdots A_N.

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq K \leq 10^9

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

4

First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.

Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.

Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192

Solution in Rust (1.42.0) (judge verdict: Accepted)

use crate::partition_point::RangeBoundsExt as _;
use proconio::{fastout, input};

#[fastout]
fn main() {
    input! {
        n: usize,
        k: u64,
        r#as: [u64; n],
    }

    let ans = (1..=1_000_000_000)
        .partition_point(|m| r#as.iter().map(|a| (a + m - 1) / m - 1).sum::<u64>() <= k)
        .unwrap();
    println!("{}", ans);
}

mod partition_point {
    use std::{
        convert::Infallible,
        ops::{Add, Bound, Div, RangeBounds, Sub},
    };

    pub trait RangeBoundsExt<T> {
        fn partition_point<F: FnMut(T) -> bool>(&self, f: F) -> Option<T>;
        fn partition_point_rev<F: FnMut(T) -> bool>(&self, f: F) -> Option<T>;
    }

    impl<R: RangeBounds<T>, T: PrimitiveInteger> RangeBoundsExt<T> for R {
        fn partition_point<F: FnMut(T) -> bool>(&self, mut f: F) -> Option<T> {
            return bisect::<_, BelowMax<_>, _>(self.end_bound(), self.start_bound(), &mut f);

            struct BelowMax<T>(Infallible, fn() -> T);

            impl<T: PrimitiveInteger> BisectBounds for BelowMax<T> {
                type Item = T;

                const OK_SIDE_BOUND: T = T::MAX_VALUE;
                const NG_SIDE_BOUND: T = T::MIN_VALUE;

                fn step_ok(ok: T) -> T {
                    ok - T::ONE
                }

                fn minmax(ok: T, ng: T) -> (T, T) {
                    (ng, ok)
                }
            }
        }

        fn partition_point_rev<F: FnMut(T) -> bool>(&self, mut f: F) -> Option<T> {
            return bisect::<_, MinAbove<_>, _>(self.start_bound(), self.end_bound(), &mut f);

            struct MinAbove<T>(Infallible, fn() -> T);

            impl<T: PrimitiveInteger> BisectBounds for MinAbove<T> {
                type Item = T;

                const OK_SIDE_BOUND: T = T::MIN_VALUE;
                const NG_SIDE_BOUND: T = T::MAX_VALUE;

                fn step_ok(ok: T) -> T {
                    ok + T::ONE
                }

                fn minmax(ok: T, ng: T) -> (T, T) {
                    (ok, ng)
                }
            }
        }
    }

    fn bisect<T: PrimitiveInteger, B: BisectBounds<Item = T>, F: FnMut(T) -> bool>(
        ok: Bound<&T>,
        ng: Bound<&T>,
        f: &mut F,
    ) -> Option<T> {
        let mut ok = match ok {
            Bound::Included(&x) if f(x) => x,
            Bound::Excluded(&x) if x != B::NG_SIDE_BOUND && f(B::step_ok(x)) => B::step_ok(x),
            Bound::Unbounded if f(B::OK_SIDE_BOUND) => B::OK_SIDE_BOUND,
            Bound::Included(_) | Bound::Excluded(_) | Bound::Unbounded => return None,
        };

        let mut ng = match ng {
            Bound::Included(&x) if f(x) => return Some(x),
            Bound::Unbounded if f(B::NG_SIDE_BOUND) => return Some(B::NG_SIDE_BOUND),
            Bound::Included(&x) | Bound::Excluded(&x) => x,
            Bound::Unbounded => B::NG_SIDE_BOUND,
        };

        if (ok < T::ZERO) == (ng < T::ZERO) {
            while B::max(ok, ng) - B::min(ok, ng) > T::ONE {
                let mid = B::min(ok, ng) + (B::max(ok, ng) - B::min(ok, ng)) / (T::ONE + T::ONE);
                *if f(mid) { &mut ok } else { &mut ng } = mid;
            }
            Some(ok)
        } else {
            let mut bisect =
                |ok, ng| bisect::<_, B, _>(Bound::Included(&ok), Bound::Excluded(&ng), f);
            bisect(T::ZERO, ng).or_else(|| bisect(ok, T::ZERO))
        }
    }

    trait BisectBounds {
        type Item: PrimitiveInteger;

        const OK_SIDE_BOUND: Self::Item;
        const NG_SIDE_BOUND: Self::Item;

        fn step_ok(ok: Self::Item) -> Self::Item;
        fn minmax(ok: Self::Item, ng: Self::Item) -> (Self::Item, Self::Item);

        fn min(ok: Self::Item, ng: Self::Item) -> Self::Item {
            let (min, _) = Self::minmax(ok, ng);
            min
        }

        fn max(ok: Self::Item, ng: Self::Item) -> Self::Item {
            let (_, max) = Self::minmax(ok, ng);
            max
        }
    }

    pub trait PrimitiveInteger:
        Copy + Ord + Add<Output = Self> + Sub<Output = Self> + Div<Output = Self>
    {
        const ZERO: Self;
        const ONE: Self;
        const MIN_VALUE: Self;
        const MAX_VALUE: Self;
    }

    macro_rules! impl_primitive_integer(($($ty:ty),*) => {
    $(
        impl PrimitiveInteger for $ty {
            const ZERO: Self = 0;
            const ONE: Self = 1;
            const MIN_VALUE: Self = <$ty>::min_value();
            const MAX_VALUE: Self = <$ty>::max_value();
        }
    )*
});

    impl_primitive_integer!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
}