#B4163. Minimal Absolute Difference Sequence Selection

    ID: 11820 Type: Default 1000ms 256MiB

Minimal Absolute Difference Sequence Selection

Minimal Absolute Difference Sequence Selection

You are given two sequences of length \( n \), \( a \) and \( b \). Your task is to construct a sequence \( c \) of length \( n \) such that for every \( i=1,2,\dots,n \), \( c_i \) is chosen either as \( a_i \) or \( b_i \). The goal is to minimize the total absolute difference between consecutive elements of \( c \), i.e., minimize

[ \sum_{i=2}^{n} \left| c_i - c_{i-1} \right| ]

You only need to output the minimum possible sum.

inputFormat

The first line contains a single integer \( n \) representing the length of the sequences. The second line contains \( n \) integers representing the sequence \( a \). The third line contains \( n \) integers representing the sequence \( b \).

outputFormat

Output a single integer which is the minimum possible sum of \( \sum_{i=2}^{n} |c_i-c_{i-1}| \) that can be obtained by choosing each \( c_i \) from \( a_i \) or \( b_i \).

sample

3
1 2 3
3 2 1
2