#P3723. [AHOI2017/HNOI2017] 礼物

    ID: 1364 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017各省省选递归安徽湖南进制快速傅里叶变换 FFT

[AHOI2017/HNOI2017] 礼物

Description

My roommate recently fell for a cute girl. Her birthday is coming up, and he decided to buy a pair of couple bracelets: one for himself and one for her. Each bracelet has nn decorations, and each decoration has a certain brightness.

However, on the day before her birthday, my roommate suddenly realized that he might have picked up the wrong bracelet, and there is no time to replace it. He can only use a special method: increase the brightness of all decorations on one of the bracelets by the same non-negative integer cc. Since a bracelet is a circle, it can be rotated by any angle, but because the orientation of the decorations is fixed, the bracelet cannot be flipped. After applying the brightness modification and rotation, we want to minimize the difference value between the two bracelets.

After rotating both bracelets and aligning the decorations, starting from some aligned position and numbering the decorations from 11 to nn in counterclockwise order (where nn is the number of decorations on each bracelet), the brightness of the decoration at position ii on the first bracelet is xix_i, and that on the second bracelet is yiy_i. The difference value between the two bracelets is:

i=1n(xiyi)2\sum_{i=1}^{n} (x_i-y_i)^2

Please help compute the minimal possible difference value after applying the allowed brightness modification and rotation.

Input Format

The first line contains two integers n,mn, m, where each bracelet has nn decorations, and the initial brightness of each decoration is at most mm.

The next two lines each contain nn integers, representing the brightness of the decorations on the first and second bracelets, respectively, listed in counterclockwise order starting from some position.

Output Format

Output a single integer: the minimal difference value that can be achieved between the two bracelets. Note that after the modification, the brightness of decorations may exceed mm.

5 6
1 2 3 4 5
6 3 3 4 5
1

Hint

Sample explanation:

Increase the brightness of the first bracelet by 11, making it 2,3,4,5,62, 3, 4, 5, 6.

Rotate the second bracelet. In this sample, cyclically shift the second bracelet 6,3,3,4,56, 3, 3, 4, 5 left by one position, making it 3,3,4,5,63, 3, 4, 5, 6.

The difference value is 11.

Constraints:

  • For 30%30\% of the testdata, n500n \le 500, m10m \le 10.
  • For 70%70\% of the testdata, n5000n \le 5000.
  • For 100%100\% of the testdata, 1n500001 \le n \le 50000, 1xi,yim1001 \le x_i, y_i \le m \le 100.

Translated by ChatGPT 5