#abc252c. C - Slot Strategy

C - Slot Strategy

Score : 300300 points

问题描述

存在一个具有 NN 个转轴的老虎机。
ii 个转轴上的符号排列由长度为 1010 的字符串 SiS_i 表示,其中恰好包含每个字符 01\ldots9 各一次。

每个转轴都有一个对应的按钮。对于每个非负整数 tt,高桥可以在转轴开始旋转后tt秒时选择按下任意一个按钮(或不操作)。
如果在转轴开始旋转后tt秒时按下第 ii 个转轴的按钮,则第 ii 个转轴会停止并显示 SiS_i 中的第 ((tmod10)+1)((t\bmod{10})+1) 个字符。
这里,tmod10t\bmod{10} 表示当 tt 除以 1010 时的余数。

高桥希望让所有转轴停下并显示相同的字符。
找出转轴开始旋转后实现这一目标所需的最短秒数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a slot machine with NN reels.
The placement of symbols on the ii-th reel is represented by a string SiS_i of length 1010 containing each of 0, 1, \ldots, 9 exactly once.

Each reel has a corresponding button. For each non-negative integer tt, Takahashi can press one of the buttons of his choice (or do nothing) tt seconds after the reels start spinning.
If the button for the ii-th reel is pressed tt seconds after the start of the spin, the ii-th reel will stop to display the ((tmod10)+1)((t\bmod{10})+1)-th character of SiS_i.
Here, tmod10t\bmod{10} denotes the remainder when tt is divided by 1010.

Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.

Constraints

  • 2N1002\leq N\leq 100
  • NN is an integer.
  • SiS_i is a string of length 1010 containing each of 0, 1, \ldots, 9 exactly once.

Input

Input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.

Sample Input 1

3
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can make all reels display 8 in 66 seconds after the start of the spin by stopping them as follows.

  • 00 seconds after the start of the spin, press the button for the 22-nd reel, making it stop to display the ((0mod10)+1=1)((0\bmod{10})+1=1)-st character of S2S_2, 8.
  • 22 seconds after the start of the spin, press the button for the 33-rd reel, making it stop to display the ((2mod10)+1=3)((2\bmod{10})+1=3)-rd character of S3S_3, 8.
  • 66 seconds after the start of the spin, press the button for the 11-st reel, making it stop to display the ((6mod10)+1=7)((6\bmod{10})+1=7)-th character of S1S_1, 8.

There is no way to make all reels display the same character in five seconds or less, so the answer is 66.

Sample Input 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

Sample Output 2

40

Note that he must stop all reels to make them display the same character.

update @ 2024/3/10 10:45:26