#P4749. [CERC2017] Kitchen Knobs

[CERC2017] Kitchen Knobs

题目描述

You are cooking on a gigantic stove at a large fast-food restaurant. The stove contains nn heating elements arranged in a line and numbered with integers 11 through nn left to right. Each element is operated by its control knob. The knobs are a bit unusual: each knob is marked with seven non-zero digits evenly distributed around a circle. The power of the heating element is equal to the positive integer obtained by reading the digits on its control knob clockwise starting from the top of the knob.

PujzQA.md.png

In a single step, you can rotate one or more consecutive knobs by any number of positions in any direction. However, all knobs rotated in one step need to be rotated by the same number of positions in the same direction.

Find the smallest number of steps needed to set all the heating elements to maximal possible power.

输入格式

The first line contains an integer n(1n501)n(1 \le n \le 501) — the number of heating elements. The jthj-th of the following nn lines contains an integer xjx_j — the initial power of the jthj-th heating element. Each xjx_j consists of exactly seven non-zero digits.

输出格式

Output a single integer — the minimal number of steps needed.

题目大意

题目描述

你正一家大型的快餐餐厅的巨型灶台上烹饪。这个灶台上有n个炉子排成一排,并按序标为1-n号。每个炉子都会被它的控制手柄所控制。这些手柄可不一般:每个手柄上面都有1-7的号码围成一圈。灶台的火力就是从它的控制手柄顶端开始顺时针读取数字而得到的。

【图片】:样例数据1输入的初始手柄位置如图所示。

每一步你都可转动一个或多个相邻手柄——向任意方向转动任意几下。但是,同一步转动的手柄只能向同一方向转动同样的次数。

找到最少的步数来吧所有的炉子都设定到可能的最大火力。

输入输出格式

输入格式

第一行包含一个整数(1<=n<=501)表示炉子的数量。接下去第j行包含7个非零整数,其中第一个是初始火力大小。

输出格式

输出一个整数,表示需要的步数。

6
9689331
1758824
3546327
5682494
9128291
9443696

3

7
5941186
3871463
8156346
9925977
8836125
9999999
5987743

2

提示

In the first example, one of the ways to achieve maximal possible power is: rotate knobs 22 through 33 by 33 positions in the counterclockwise direction, rotate knob 33 by 33 positions in the counterclockwise direction, and rotate knobs 44 through 66 by 22 positions in the clockwise direction.