Managing Allowance
InterviewTime limit1sMemory limit128 MB
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 days, the amount of money he will spend each day is fixed, and he decides to withdraw money from his bank account exactly times.
Each time, Hyunwoo withdraws 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 won, and then pays for that day.
Because Hyunwoo likes the number , he wants the number of withdrawals to be exactly . So even when he already has more than enough money for the day, he may choose to deposit the leftover back and withdraw won again. In other words, as long as the minimum number of withdrawals actually required is at most , he can insert extra withdrawals to make the total exactly .
To save money, Hyunwoo wants the withdrawal amount to be as small as possible. Write a program that finds the smallest that lets him get through all days while withdrawing exactly times.
Input
The first line contains and , separated by a space. (, )
Each of the next lines contains the amount of money Hyunwoo will spend on the -th day, one per line. ()
Output
Print the minimum amount that Hyunwoo must withdraw from his account.