##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
4
3 5 2 5 2 5 2
4 1 0 1 0 10 0 1 0
3 10 100 5 100 1 100
5 1 1 1 1 1 1 1 1 100 1
15
40
16
490

It is Christmas, and Santa wants to give gifts to children standing in a line. There are children, numbered from to from left to right.
For each child , two numbers and are specified. Santa will decide how many gifts to give to each child.
Child is happy if they receive at least gifts, and also they are not too far behind any adjacent child: compared to each neighbor (if it exists), child may have at most fewer gifts.
Formally, the conditions are for all , and additionally for and for .
Find the minimum possible value of such that all children are happy.
, all children have
Each child needs gifts and tolerates neighbors having up to more. Distribution works. Total: 15.
, values
With , no child tolerates any difference, so all must receive equal gifts. Child 3 needs , forcing . Total: 40.