Eating Pi
Tony has been invited out to eat. He's quite hungry and decides to visit Ritisha's Pie Emporium, famous for its rotating pie a magnificent dessert made up of several slices of different sizes arranged in a perfect circle.
What makes this pie truly special, however, is that whenever Tony eats a slice, an identical slice instantly reappears in the same spot. This means he could, in theory, eat forever.
But Ritisha has three rules:
- You cannot eat the same slice twice in a row.
- You can only eat a slice that is directly clockwise or anticlockwise of the one you just ate.
- If you start a slice, you must finish it
each slice costs the same, no matter its size. Tony is on a budget and wants to eat just enough to exactly satisfy his hunger, represented by a target number.
Starting from the 1st slice given, determine the fewest number of slices Tony must eat to reach exactly his target hunger level, or state that it's impossible.
Input
The first line contains two integers
and
the number of different slices , starting hunger level
the next line contains
integers the sizes of each slice of pie
Output
Output the minimum number of slices Tony can eat to satisfy his hunger if there is no way to satisfy his hunger output -1
Clarifcations
- you must eat the 1st slice offered
Example
Input 1
4 8
1 2 4 3
Output 1
3
- starting hunger: 8
- eat slice 0
- hunger: 7
- turn clockwise
- eat slice 3
- hunger: 3
- turn clockwise
- eat slice 2
- hunger: 0
- we ate 3 slices output 3
Input 2
5 7
3 1 4 1 5
Output 2
3
- starting hunger: 7
- eat slice 0
- hunger: 4
- turn anticlockwise
- eat slice 1
- hunger: 3
- turn clockwise
- eat slice 0
- hunger: 0
- we ate 3 slices output 3
Input 3
4 15
2 4 6 10 12
Output 3
-1
- there is no way to reach 15
Comments