Poker Hands
InterviewTime limit1sMemory limit128 MB
Given card counts per rank, find the fewest contiguous-rank straights whose unit cards sum to exactly those counts.
- Level
Medium7 of 10
- Topics
- Greedy, Array, Implementation, Prefix sum
- Solved
- No attempts yet
Problem
Bessie and her friends are playing a special version of poker. The deck has () distinct ranks, numbered through (an ordinary deck has ).
In this game there is exactly one kind of hand a cow may play: choose two ranks and with , then play exactly one card of every rank from to inclusive. Such a hand is called a straight.
Bessie currently holds cards of rank (). Find the minimum number of straights she must play to get rid of all of her cards.
Input
- The first line contains the integer .
- Among the next lines, the -th line contains , the number of cards of rank .
Output
Print a single integer: the minimum number of straights Bessie must play to get rid of all of her cards.
Hint
For the sample case, Bessie can play a straight from to , a straight from to , a straight from to , two straights from to , and a straight from to , for a total of 6 hands needed to discard all of her cards.