Golf
InterviewTime limit1sMemory limit1024 MB
Given a distance D and C club distances, find the fewest strokes that sum to exactly D, or 0 if impossible.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Like everyone else, the cows love the relaxation and challenge of a good game of golf. Bessie plays extraordinarily well and can aim her shot perfectly toward the hole, every time. Each club drives the ball exactly the distance specified for that club, every time.
Bessie is a smart cow. She knows she can overshoot and then hit back toward the hole, and she uses this to her advantage.
For those who do not play golf: every time the ball is hit, a stroke is counted. The object of golf is to hit the ball from the tee into the hole in as few strokes as possible. The lowest score wins.
Bessie needs a program to select the best combination of clubs to reach a given hole in the fewest strokes. If that is impossible, she wants to know, and she graciously accepts the loss. Cows are sturdier than humans, so Bessie can carry clubs (), and the total distance from the tee to the hole does not exceed 10,000 yards ().
Input
The first line has two space-separated integers, and . Each of the next lines contains a single integer (), the number of yards that club drives the ball. No two clubs share the same distance.
Output
Print a single line with the minimum number of strokes needed to move the ball from the tee to the hole. Print 0 if it is impossible to reach the hole with the given clubs.