Statistics

Problem Statement for "UpDownHiking"

Problem Statement

Limak is going to spend N days in the mountains. The days will be numbered 1 through N. In the morning of day 1 Limak starts his hike in the base camp at altitude 0. In the evening of day N Limak must return back to altitude 0. Limak carries a tent and a sleeping bag, so during the hike he can sleep at any altitude.

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.
For example, suppose that Limak's altitude in the morning is 470, and suppose that A=100 and B=200. In the evening of the same day Limak can be anywhere between the altitudes 270 and 570, inclusive.

You are given the ints N, A, and B. Return the largest altitude Limak can reach.

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

  1. 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.

  2. 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.

  3. 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.

  4. 3

    42

    42

    Returns: 42

  5. 20

    7

    9

    Returns: 77

  6. 50

    5

    7

    Returns: 145

  7. 34

    42

    2

    Returns: 64

  8. 7

    25

    45

    Returns: 100

  9. 2

    10

    1

    Returns: 1

  10. 2

    10

    20

    Returns: 10

  11. 3

    10

    45

    Returns: 20

  12. 5

    32

    2

    Returns: 8

  13. 17

    5

    23

    Returns: 69

  14. 43

    23

    1

    Returns: 41

  15. 48

    7

    16

    Returns: 231

  16. 50

    1

    1

    Returns: 25

  17. 50

    3

    27

    Returns: 135

  18. 50

    50

    50

    Returns: 1250

  19. 50

    14

    12

    Returns: 322

  20. 50

    49

    42

    Returns: 1127

  21. 50

    1

    50

    Returns: 49

  22. 50

    50

    1

    Returns: 49

  23. 10

    1

    10

    Returns: 9

  24. 3

    10

    2

    Returns: 4

  25. 49

    1

    50

    Returns: 48

  26. 6

    4

    2

    Returns: 8

  27. 3

    15

    29

    Returns: 29

  28. 3

    6

    5

    Returns: 6

  29. 2

    5

    3

    Returns: 3

  30. 4

    1

    50

    Returns: 3

  31. 5

    2

    10

    Returns: 8

  32. 2

    3

    7

    Returns: 3

  33. 3

    50

    2

    Returns: 4

  34. 50

    50

    49

    Returns: 1225

  35. 5

    30

    40

    Returns: 80

  36. 5

    1

    1

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: