This page is still under construction.

Parts of this page are still being built. What you see may change.

Managing Allowance

Interview

Time limit1sMemory limit128 MB

Summary
Find the smallest fixed withdrawal amount K so that the N daily costs can be covered using exactly M withdrawals, counting forced top-ups and optional extra ones.
Level

Medium5 of 10

Topics
Binary search, Greedy, Simulation, Array
Solved
No attempts yet

Problem

Hyunwoo wants to plan how to spend his allowance efficiently. For the next NN days, the amount of money he will spend each day is fixed, and he decides to withdraw money from his bank account exactly MM times.

Each time, Hyunwoo withdraws KK won from the account. He spends each day using the money he currently holds: if it is enough to cover that day's amount, he pays it and carries the rest over to the next day. If it is not enough, he first puts the leftover money back into the account, withdraws a fresh KK won, and then pays for that day.

Because Hyunwoo likes the number MM, he wants the number of withdrawals to be exactly MM. So even when he already has more than enough money for the day, he may choose to deposit the leftover back and withdraw KK won again. In other words, as long as the minimum number of withdrawals actually required is at most MM, he can insert extra withdrawals to make the total exactly MM.

To save money, Hyunwoo wants the withdrawal amount KK to be as small as possible. Write a program that finds the smallest KK that lets him get through all NN days while withdrawing exactly MM times.

Input

The first line contains NN and MM, separated by a space. (1≤N≤100,0001 \le N \le 100{,}000, 1≤M≤N1 \le M \le N)

Each of the next NN lines contains the amount of money Hyunwoo will spend on the ii-th day, one per line. (1≤amount≤10,0001 \le \text{amount} \le 10{,}000)

Output

Print the minimum amount KK that Hyunwoo must withdraw from his account.

Examples1

  1. Example 1

    Input
    7 5
    100
    400
    300
    100
    500
    101
    400
    
    Expected output
    500