#P3723. Bracelet Brightness Adjustment
Bracelet Brightness Adjustment
Bracelet Brightness Adjustment
Your roommate recently fell for a cute girl. With her birthday just around the corner, he decided to buy a pair of matching bracelets – one for himself and one for her. Each bracelet has n decorative charms arranged in a circle, and every charm has a brightness value. However, a day before her birthday, he realized that he picked up the wrong bracelet and has no time to exchange it! The only solution is to adjust one of the bracelets by increasing the brightness of every charm by the same non-negative integer c (i.e. adding c to each element). Moreover, because the bracelet is circular, he can choose an arbitrary rotation (but note that flipping is not allowed, as the orientations of the charms are fixed).
After choosing a rotation and applying the brightness increase to one bracelet, the charms on the two bracelets are aligned. Starting from some alignment point and labeling the charms in counterclockwise order from 1 to n, denote the brightness values on the first bracelet by \(x_1, x_2, \dots, x_n\) and those on the second bracelet (which is adjusted) by \(y_1, y_2, \dots, y_n\). The difference between the two bracelets is defined as:
[ D = \sum_{i=1}^{n} (x_i - (y_i + c))^2. ]
Your task is to help him choose an appropriate rotation and a non-negative integer c to add (to every charm of one bracelet) so that the difference D is minimized. Output the minimum possible value of D.
Note: The operation of adding a brightness increment is applied uniformly to one entire bracelet. Rotation means that the positions of the charms in that bracelet can be shifted cyclically.
inputFormat
The input is given as follows:
The first line contains an integer n (the number of charms in each bracelet). The second line contains n integers: x1, x2, ..., xn, representing the brightness values on the first bracelet. The third line contains n integers: y1, y2, ..., yn, representing the brightness values on the second bracelet.
All brightness values are integers.
outputFormat
Output a single integer, the minimum possible difference value D after performing the adjustment (adding a non-negative integer c to one bracelet) and optimal rotation.
sample
3
1 2 3
2 2 2
2