Problem Statement
Little Fox Jiro is standing at the bottom of a long flight of stairs. The bottom of the stairs has number 0, the bottommost step has number 1, the next step has number 2, and so on. The staircase is so long that Jiro is guaranteed not to reach its top.
Jiro will now perform N consecutive actions. The actions are numbered 1 through N, in order. When performing action X, Jiro chooses between two options: either he does nothing, or he jumps exactly X steps up the stairs. In other words, if Jiro starts performing action X standing on step Y, he will end it either on step Y, or on step Y+X.
For example, if N=3, Jiro will make three consecutive choices: whether or not to jump 1 step upwards, 2 steps upwards, and then 3 steps upwards.
One of the steps is broken. The number of this step is badStep. Jiro cannot jump onto this step.
You are given the
Definition
- Class:
- JumpFurther
- Method:
- furthest
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int furthest(int N, int badStep)
- (be sure your method is public)
Constraints
- N will be between 1 and 2,000, inclusive.
- badStep will be between 1 and 4,000,000, inclusive.
Examples
2
2
Returns: 3
The optimal strategy is to jump upwards twice: from step 0 to step 1, and then from step 1 to step 3. This trajectory avoids the broken step.
2
1
Returns: 2
In this case step 1 is broken, so Jiro cannot jump upwards as his first action. The optimal strategy is to first stay on step 0, and then to jump from step 0 to step 2.
3
3
Returns: 5
1313
5858
Returns: 862641
1
757065
Returns: 1
2000
4000000
Returns: 2001000
2000
1
Returns: 2000999
1
1
Returns: 0
2000
2001000
Returns: 2000999
1
1
Returns: 0
2
2
Returns: 3
3
2
Returns: 6
4
3
Returns: 9
5
1
Returns: 14
13
4
Returns: 91
22
12
Returns: 253
31
2
Returns: 496
40
10
Returns: 819
49
15
Returns: 1224
2000
1888
Returns: 2001000
1993
219
Returns: 1987021
1986
257
Returns: 1973091
1979
65
Returns: 1959210
1972
717
Returns: 1945378
1965
1376
Returns: 1931595
17
55
Returns: 152
25
1
Returns: 324
33
28
Returns: 560
41
105
Returns: 860
49
45
Returns: 1224
2000
199396
Returns: 2000999
1987
7875
Returns: 1975077
1974
355746
Returns: 1949324
1961
305371
Returns: 1923740
1948
65341
Returns: 1898325
1935
41616
Returns: 1873079
1499
1122751
Returns: 1124249
1599
1268028
Returns: 1279199
1699
1430586
Returns: 1444149
1799
1601155
Returns: 1619099
1899
1794565
Returns: 1804049
1999
1983036
Returns: 1998999
2000
287332
Returns: 2001000
1699
1827096
Returns: 1444150
1398
2120892
Returns: 977901
1097
3205588
Returns: 602253
796
3444833
Returns: 317206
495
2230356
Returns: 122760
2
3
Returns: 2
20
21
Returns: 209
5
10
Returns: 14
3
6
Returns: 5
2000
400000
Returns: 2001000
4
6
Returns: 9
5
15
Returns: 14
7
15
Returns: 27
1989
3999999
Returns: 1979055
1
100
Returns: 1
10
55
Returns: 54
3
10
Returns: 6
14
55
Returns: 104
5
6
Returns: 14
15
15
Returns: 119
5
3
Returns: 14
2000
3545
Returns: 2001000
4
10
Returns: 9
100
7
Returns: 5050
10
15
Returns: 54
5
7
Returns: 15
1
6
Returns: 1
5
21
Returns: 15
1999
1999000
Returns: 1998999