#D4024. Dimension travel
Dimension travel
Dimension travel
problem
Dimension exists from dimension to dimension. AOR Ika can move between dimensions. Specifically, when AOR Ika is in the dimension, she can move freely to the dimension (), but in the dimension (). You need to use magic to move to.
AOR Ika can use types of magic and is numbered . The th magic allows you to move from the dimension to the dimension. This can only be used when you are in the dimension.
When you go to the dimension, you will receive a dimension-specific "curse of dimensionality". AOR Ika takes damage due to the "curse of dimensionality" of the dimension. If you receive the "curse of dimensionality" of the dimension, you will not receive the "curse of dimensionality" of the dimension () forever.
Find the minimum total damage inflicted when AOR Ika moves from the dimension to the dimension. However, AOR Ika-chan has already received the "curse of dimensionality" of the dimension, and we will not consider the damage. Also, AOR Ika is guaranteed to be able to go to the dimension.
output
Output the minimum value of the total damage received by AOR Ika-chan. Also, output a line break at the end.
Example
Input
3 1 2 3 1 2 3 1 3
Output
3
inputFormat
outputFormat
output
Output the minimum value of the total damage received by AOR Ika-chan. Also, output a line break at the end.
Example
Input
3 1 2 3 1 2 3 1 3
Output
3
样例
3 1 2 3
1 2 3
1 3
3