More Garden Troubles
InterviewTime limit2sMemory limit128 MB
Find the fewest flamethrower shots that clear every plot when each shot halves a plot and its two neighbors, rounding down.
Problem
Mr. Wincenty owns a garden and is famously the sort of person who cannot stand yard work. Some time after his earlier troubles with fallen leaves, spring arrived and brought a new headache: weeds began sprouting all over the garden. One day, after long hours in his workshop, Mr. Wincenty walked into the garden holding a portable flamethrower.
The garden consists of plots numbered from to . Plot holds a whole number of weeds . Firing the flamethrower once at plot halves the number of weeds in plot and in the neighboring plots and .
Here "halving" means integer division by (rounding down): weeds become , and weeds become . You may also aim the flamethrower at the non-existent plots or ; in that case only plot or only plot is reduced, respectively.
Determine the minimum number of times Mr. Wincenty must fire the flamethrower so that every plot ends up with weeds.
Input
The first line contains an integer (), the number of test cases. The test cases follow.
Each test case begins with a line containing one integer (), the number of plots in the garden. The next line contains space-separated integers (), the number of weeds in each plot.
Output
For each test case, print on its own line the minimum number of times the flamethrower must be used.