#P6874. Brick Pile Construction
Brick Pile Construction
Brick Pile Construction
Mirko and Slavko are playing with bricks. Each of them has a set of brick columns. In total there are an odd number N of columns. In Mirko's pile, the i-th column contains mi bricks; in Slavko's pile, the i-th column contains si bricks.
They want to form two identical brick piles (one from Mirko's bricks and one from Slavko's) such that the column heights first strictly decrease and then strictly increase, with the adjacent columns differing by exactly 1. More precisely, if we denote the final height of column i as hi and let mid = (N+1)/2 (the center column), then the following must hold:
- hi = x + |i - mid| for some integer x \ge 0
- For i = 1,2,...,mid, hi strictly decreases, and for i = mid,...,N, hi strictly increases.
- The two columns adjacent to the lowest column (at i = mid) have the same number of bricks (i.e. symmetry is maintained).
You are allowed to perform the following operations on any column:
- Remove one brick from the top of a column.
- Add one brick on top of a column.
Determine the minimum number of operations required so that both piles can be transformed into the desired pattern.
Note: The desired pattern is completely determined by the choice of the integer x (which represents the height of the middle column). For each column i, the target height is Ti = x + |i - mid|.
The total cost is the sum over all columns and both piles of |mi - Ti|
and |si - Ti|
. Since the function is convex in x, an optimal x can be found by considering the median of the adjusted brick counts.
inputFormat
The first line contains an odd integer N (the number of columns).
The second line contains N integers: m1, m2, ..., mN, representing the number of bricks in each column in Mirko's pile.
The third line contains N integers: s1, s2, ..., sN, representing the number of bricks in each column in Slavko's pile.
outputFormat
Output a single integer: the minimum number of operations required.
sample
3
2 1 2
3 2 3
3