You are given a permutation of length and two arrays and . The permutation contains each integer from to exactly once.
Initially, the sequence contains in this order. You want to delete every element.
In one operation, choose either the minimum or the maximum value currently present in the sequence. You may delete the chosen value only if it is at the front or the back of the sequence.
When only one element remains, it is both the front and the back. You may choose either method to delete it. The relative order of the remaining elements does not change.
Find the minimum total cost to delete every element. If this is impossible, print -1.
The input is given from standard input in the following format:
Print the minimum total cost to delete all elements. If this is impossible, print -1.
The costs and are indexed by the value , not by its position.