Under construction

Still being built. Things may change

Text Editor

Time limit4sMemory limit1024 MB

Summary
Given line lengths, find the fewest arrow key presses to move the cursor from a start position to a target, where up and down keep the column clamped to each line's end.
Level

Hard9 of 10

Topics
Shortest path, Greedy, Array
Solved
No attempts yet

Problem

Robert is competing in CEOI 2024. He has almost finished his solution to the hardest problem of the day and is pretty sure it will score 100 points. There is one small problem: he made a typo. His favorite mouse, which he has used since 2008, has finally broken and does not respond at all. He has to move the cursor to the typo with the arrow keys on his keyboard.

Robert's program has NN lines with lengths l1,l2,…,lNl_1, l_2, \ldots, l_N. Robert always ends his programs with an empty line, so lN=0l_N = 0. The cursor can be placed between two characters, or at the start or end of a line. Line ii therefore has li+1l_i + 1 possible cursor positions (called columns), numbered from 1 to li+1l_i + 1. For example, a cursor on line 2 in column 6 sits after the fifth character of that line.

Robert wants to move the cursor from line sls_l, column scs_c to line ele_l, column ece_c. Find the minimum number of key presses needed to do so.

The horizontal arrow keys are simple. Pressing left moves the cursor to the previous column, or to the end of the previous line if the cursor is at the start of a line. Pressing right moves the cursor to the next column, or to the start of the next line if the cursor is at the end of a line. Pressing left at the very start of the file or right at the very end of the file has no effect.

The vertical arrow keys are slightly more complicated. Pressing up moves the cursor to the previous line, and pressing down moves it to the next line, without changing the column number. If that column is past the end of the new line, the cursor moves to the end of that line instead. If up or down would move the cursor to a line that does not exist, the cursor does not move.

Input

The first line contains the integer NN, the number of lines in Robert's solution.

The second line contains two integers sls_l and scs_c, the initial cursor position.

The third line contains two integers ele_l and ece_c, the target cursor position.

The fourth line contains NN space-separated integers l1,l2,…,lNl_1, l_2, \ldots, l_N, the length of each line.

Output

Print a single line containing one integer, the minimum number of key presses needed to move the cursor from (sl,sc)(s_l, s_c) to (el,ec)(e_l, e_c).

Constraints

1≤N≤1061 \leq N \leq 10^6

0≤li≤1090 \leq l_i \leq 10^9 for each ii such that 1≤i≤N1 \leq i \leq N

lN=0l_N = 0

1≤sl,el≤N1 \leq s_l, e_l \leq N

1≤sc≤lsl+11 \leq s_c \leq l_{s_l} + 1

1≤ec≤lel+11 \leq e_c \leq l_{e_l} + 1

Examples2

  1. Example 1

    Input
    5
    3 1
    2 8
    7 10 9 9 0
    
    Expected output
    3
    
  2. Example 2

    Input
    5
    1 20
    3 25
    25 10 40 35 0
    
    Expected output
    16