B. Bulldozer
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
Examples
Input
5
1 1 2 1 1
Output
2
Input
5
1 4 3 1 1
Output
7
Input
9
1 0 0 0 6 0 0 0 1
Output
5
Input
10
1 3 0 0 1 9 1 1 1 1
Output
13