electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Back and forth

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

Otabek prefers to drink only pure water, specifically he likes one particular brand of bottled water. But it turned out that the factory producing this drink has closed down, and now it cannot be found in any store. However, Otabek still has not come to terms with this fact, so he decided to visit every store in his city in search of this water.

The city where Otabek lives can be represented as a number line ranging from −A-A−A to +A+A+A. It is known that there are a total of nnn stores in the city. The order in which Otabek will visit the stores is given by a list of nnn integers, where the element aia_iai​ (−A≤ai≤+A-A \le a_i \le +A−A≤ai​≤+A) corresponds to the position of the iii-th store.

Otabek will visit the stores as follows:

  • Otabek starts at point 000;
  • He walks from point 000 to point a1a_1a1​, increasing the total distance by ∣a1−0∣|a_1 - 0|∣a1​−0∣;
  • He walks from point to point , increasing the total distance by ;

Your task is to calculate the total distance that Otabek will walk. The distance traveled from point xxx to point yyy is equal to ∣y−x∣|y-x|∣y−x∣.

Input

In the first line, you are given the number of stores nnn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^51≤n≤2⋅105) and the range of the city AAA (1≤A≤1051 \le A \le 10^51≤).

In the second line, nnn integers aia_iai​ (−A≤ai≤+A-A \le a_i \le +A−A≤ai​≤+A) are given — the positions and order of the stores, all stores are distinct, note that a store can also be located at point .

Output

Output a single number — the total distance that Otabek will walk.

Scoring System

GroupAdditional ConstraintsPointsRequired Subgroups
0Tests from the statement0—
1n≤10,A≤100n \le 10, A \le 100n≤10,A≤10050
2n≤100,A≤

Examples

Example 1
Input
1 10
5
Output
10
Example 2
Input
5 10
2 3 -6 -2 10
Output
38

© 2026 Electicode. All rights reserved.

a1a_1a1​
a2a_2a2​
∣a2−a1∣|a_2 - a_1|∣a2​−a1​∣
  • …\ldots…
  • He continues the process until he reaches the last point ana_nan​;
  • From point ana_nan​ back to point 000, increasing the total distance by ∣0−an∣|0 - a_n|∣0−an​∣;
  • A
    ≤
    105
    00
    0
    100n \le 100, A \le 100
    n≤100,A≤100
    15
    0, 1
    3No additional constraints800, 1, 2