Tap here to switch tabs
Problem
Submissions

Problem: Maximum Value of an Alternating Sequence

Try to solve the Maximum Value of an Alternating Sequence problem.

Statement

You are given three integers n, s, and m.

Construct a sequence of length n starting with s, where the elements a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} are defined as follows:

  • $a_0 = $ s

  • For each ii from 11 to n −1- 1:

    • If ii is odd, $a_i = a_{i - 1} + $ m

    • If ii is even, ai=ai−1−1a_i = a_{i - 1} - 1

Return the maximum value present in the sequence.

Constraints:

  • 1≤1 \leq n, s ≤109\leq 10^9

  • 1≤1 \leq m ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Value of an Alternating Sequence

Try to solve the Maximum Value of an Alternating Sequence problem.

Statement

You are given three integers n, s, and m.

Construct a sequence of length n starting with s, where the elements a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} are defined as follows:

  • $a_0 = $ s

  • For each ii from 11 to n −1- 1:

    • If ii is odd, $a_i = a_{i - 1} + $ m

    • If ii is even, ai=ai−1−1a_i = a_{i - 1} - 1

Return the maximum value present in the sequence.

Constraints:

  • 1≤1 \leq n, s ≤109\leq 10^9

  • 1≤1 \leq m ≤105\leq 10^5