B - Garden of Giants

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

Input

Output

Sample test(s)

Input
3 2 1 1 3 3 2
Output
5