L - Distance Graph

Languages: C, C++, Java, Python, Kotlin
Time & Memory limits: (details)

Input

Output

Sample test(s)

Input
6 2 1 2
Output
9
Input
10 4 1 2 3 4
Output
-1
Input
11 4 1 2 3 4
Output
21
Input
15 4 1 2 3 4
Output
46

Hints