#abc264g. G - String Fair

G - String Fair

Score : 600600 points

问题描述

在一场字符串展览中,他们确定一个非空的、由小写英文字母组成的字符串 SS 的“美感”。

字符串 SS 的美感等于由 NN 个标准决定的 NN 个评分之和。对于 i=1,2,,Ni = 1, 2, \ldots, N,第 ii 个标准决定的评分是:“作为连续子序列在 SS 中出现的次数”的 TiT_i(输入中给出的长度 最多为3 的字符串)乘以 PiP_i

打印由小写英文字母组成的 非空 字符串 SS 可能达到的最大美感。如果可以获得无限大的美感,则打印 Infinity

这里,字符串 VV 作为连续子序列在字符串 U=U1U2UUU = U_1U_2\ldots U_{|U|} 中出现的次数定义为满足条件 1iUV+11 \leq i \leq |U|-|V|+1UiUi+1Ui+V1=VU_iU_{i+1}\ldots U_{i+|V|-1} = V 的整数 ii 的数量。

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

Problem Statement

In a string fair, they determine the beauty of a non-empty string SS consisting of lowercase English letters.

The beauty of string SS equals the sum of NN scores determined by NN criteria. For i=1,2,,Ni = 1, 2, \ldots, N, the score determined by the ii-th criterion is "the number of occurrences of a string TiT_i (of length at most 33 given in the Input) in SS as a consecutive subsequence" multiplied by PiP_i.

Print the maximum possible beauty of a non-empty string SS consisting of lowercase English letters. If it is possible to obtain infinitely large beauty, print Infinity instead.

Here, the number of occurrences of a string VV in a string U=U1U2UUU = U_1U_2\ldots U_{|U|} as a consecutive subsequence is defined to be the number of integers ii such that 1iUV+11 \leq i \leq |U|-|V|+1 and UiUi+1Ui+V1=VU_iU_{i+1}\ldots U_{i+|V|-1} = V.

Constraints

  • 1N182781 \leq N \leq 18278
  • NN is an integer.
  • TiT_i is a string of length between 11 and 33 consisting of lowercase English letters.
  • ijTiTji \neq j \Rightarrow T_i \neq T_j
  • 109Pi109-10^9 \leq P_i \leq 10^9
  • PiP_i is an integer.

Input

Input is given from Standard Input in the following format:

NN

T1T_1 P1P_1

T2T_2 P2P_2

\vdots

TNT_N PNP_N

Output

Print the maximum possible beauty of a non-empty string SS consisting of lowercase English letters. If it is possible to obtain infinitely large beauty, print Infinity instead.

Sample Input 1

3
a -5
ab 10
ba -20

Sample Output 1

Infinity

For example, if S=S = abzabz:

  • The score determined by the 11-st criterion is 2×(5)=102 \times (-5) = -10 points, because a occurs twice in SS as a consecutive subsequence.
  • The score determined by the 22-nd criterion is 2×10=202 \times 10 = 20 points, because ab occurs twice in SS as a consecutive subsequence.
  • The score determined by the 33-rd criterion is 0×(20)=00 \times (-20) = 0 points, because ba occurs 00 times in SS as a consecutive subsequence.

Thus, the beauty of SS is (10)+20+0=10(-10) + 20 + 0 = 10.

As another example, if S=S = abzabzabz:

  • The score determined by the 11-st criterion is 3×(5)=153 \times (-5) = -15 points, because a occurs 33 times in SS as a consecutive subsequence.
  • The score determined by the 22-nd criterion is 3×10=303 \times 10 = 30 points, because ab occurs 33 times in SS as a consecutive subsequence.
  • The score determined by the 33-rd criterion is 0×(20)=00 \times (-20) = 0 points, because ba occurs 00 times in SS as a consecutive subsequence.

Thus, the beauty of SS is (15)+30+0=15(-15) + 30 + 0 = 15.

In general, for a positive integer XX, if SS is a concatenation of XX copies of abz, then the beauty of SS is 5X5X. Since you can obtain as large beauty as you want, Infinity should be printed.

Sample Input 2

28
a -5
ab 10
ba -20
bb -20
bc -20
bd -20
be -20
bf -20
bg -20
bh -20
bi -20
bj -20
bk -20
bl -20
bm -20
bn -20
bo -20
bp -20
bq -20
br -20
bs -20
bt -20
bu -20
bv -20
bw -20
bx -20
by -20
bz -20

Sample Output 2

5

S=S = ab achieves the maximum beauty possible.

Sample Input 3

26
a -1
b -1
c -1
d -1
e -1
f -1
g -1
h -1
i -1
j -1
k -1
l -1
m -1
n -1
o -1
p -1
q -1
r -1
s -1
t -1
u -1
v -1
w -1
x -1
y -1
z -1

Sample Output 3

-1

Note that SS should be a non-empty string.

update @ 2024/3/10 11:10:31