Skip to content
BytePatterns

Pie Slices With Two Friends

HardDynamic Programming#circular-dp#exact-count-dp~45m

Problem

A round pie is cut into 3n slices of different sizes, arranged in a circle. You take any remaining slice, then one friend takes the remaining slice just before it on the circle and another friend takes the remaining slice just after it. This repeats until the pie is gone. Return the largest total size you can collect.

Examples

Input:  slices = [4, 1, 2, 8, 3, 5]
Output: 13
Why:    take 8 (friends take 2 and 3), then take 5 (friends take 4 and 1)
Input:  slices = [8, 1, 1, 1, 1, 8]
Output: 9
Why:    the two 8s touch across the join of the circle, so taking
        one always hands the other to a friend
Input:  slices = [5, 2, 7]
Output: 7
Why:    edge case, one round: take the biggest slice

Hints

0 / 3

Stuck on the idea rather than the code? House Robber in a Circle covers it.