Problem Statement
During each day of his hike, Limak either ascends or descends. (Each day he has to choose one or the other, he cannot both ascend and descend on the same day.) Additionally, there are two constraints:
- He cannot ascend too quickly, to make acclimatization to higher altitudes easier. More precisely, each day spent ascending can increase his altitude by at most A.
- He cannot descend too quickly, otherwise his knees hurt. More precisely, each day spent descending can decrease his altitude by at most B.
You are given the
Definition
- Class:
- UpDownHiking
- Method:
- maxHeight
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int maxHeight(int N, int A, int B)
- (be sure your method is public)
Constraints
- N will be between 2 and 50, inclusive.
- A and B will be between 1 and 50, inclusive.
Examples
3
7
10
Returns: 10
The highest altitude Limak can reach is 10. Here is one way to do so: On day 1, Limak will start at altitude 0 and he will ascend to altitude 7. On day 2, Limak will start at altitude 7 and he will ascend to altitude 10. On day 3, Limak will start at altitude 10 and he will descend back to altitude 0.
5
40
30
Returns: 80
In the five days of this hike Limak can change his altitude by +40, +40, -27, -27, and -26. The highest altitude reached is 80.
2
50
1
Returns: 1
Even though Limak can climb quickly, moving up even by 50 in one day, he can move down by at most 1 in the second day. So, in the first day he should climb by 1 only.
3
42
42
Returns: 42
20
7
9
Returns: 77
50
5
7
Returns: 145
34
42
2
Returns: 64
7
25
45
Returns: 100
2
10
1
Returns: 1
2
10
20
Returns: 10
3
10
45
Returns: 20
5
32
2
Returns: 8
17
5
23
Returns: 69
43
23
1
Returns: 41
48
7
16
Returns: 231
50
1
1
Returns: 25
50
3
27
Returns: 135
50
50
50
Returns: 1250
50
14
12
Returns: 322
50
49
42
Returns: 1127
50
1
50
Returns: 49
50
50
1
Returns: 49
10
1
10
Returns: 9
3
10
2
Returns: 4
49
1
50
Returns: 48
6
4
2
Returns: 8
3
15
29
Returns: 29
3
6
5
Returns: 6
2
5
3
Returns: 3
4
1
50
Returns: 3
5
2
10
Returns: 8
2
3
7
Returns: 3
3
50
2
Returns: 4
50
50
49
Returns: 1225
5
30
40
Returns: 80
5
1
1
Returns: 2