102646: [AtCoder]ABC264 G - String Fair

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

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

The beauty of string $S$ equals the sum of $N$ scores determined by $N$ criteria. For $i = 1, 2, \ldots, N$, the score determined by the $i$-th criterion is "the number of occurrences of a string $T_i$ (of length at most $3$ given in the Input) in $S$ as a consecutive subsequence" multiplied by $P_i$.

Print the maximum possible beauty of a non-empty string $S$ 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 $V$ in a string $U = U_1U_2\ldots U_{|U|}$ as a consecutive subsequence is defined to be the number of integers $i$ such that $1 \leq i \leq |U|-|V|+1$ and $U_iU_{i+1}\ldots U_{i+|V|-1} = V$.

Constraints

  • $1 \leq N \leq 18278$
  • $N$ is an integer.
  • $T_i$ is a string of length between $1$ and $3$ consisting of lowercase English letters.
  • $i \neq j \Rightarrow T_i \neq T_j$
  • $-10^9 \leq P_i \leq 10^9$
  • $P_i$ is an integer.

Input

Input is given from Standard Input in the following format:

$N$
$T_1$ $P_1$
$T_2$ $P_2$
$\vdots$
$T_N$ $P_N$

Output

Print the maximum possible beauty of a non-empty string $S$ 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 =$ abzabz:

  • The score determined by the $1$-st criterion is $2 \times (-5) = -10$ points, because a occurs twice in $S$ as a consecutive subsequence.
  • The score determined by the $2$-nd criterion is $2 \times 10 = 20$ points, because ab occurs twice in $S$ as a consecutive subsequence.
  • The score determined by the $3$-rd criterion is $0 \times (-20) = 0$ points, because ba occurs $0$ times in $S$ as a consecutive subsequence.

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

As another example, if $S =$ abzabzabz:

  • The score determined by the $1$-st criterion is $3 \times (-5) = -15$ points, because a occurs $3$ times in $S$ as a consecutive subsequence.
  • The score determined by the $2$-nd criterion is $3 \times 10 = 30$ points, because ab occurs $3$ times in $S$ as a consecutive subsequence.
  • The score determined by the $3$-rd criterion is $0 \times (-20) = 0$ points, because ba occurs $0$ times in $S$ as a consecutive subsequence.

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

In general, for a positive integer $X$, if $S$ is a concatenation of $X$ copies of abz, then the beauty of $S$ is $5X$. 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 = $ 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 $S$ should be a non-empty string.

Input

题意翻译

在“字符串评鉴大会”上,由小写字母构成的非空字符串 $S$ 的由 $N$ 条标准决定: 每条标准是一个字符串 $T_i$ ($T_i$ 的长度不超过 $3$)和对应的得分 $P_i$,$S$ 的“美丽度”定义为: $$\sum_{i=1}^N P_i\times C_i$$ 其中 $C_i$ 为 $T_i$ 在 $S$ 中出现的次数。 字符串 $V$ 在字符串 $U=U_1U_2 \cdots U_{|U|}$ 中出现的次数定义为:满足 $1\le i\le \vert U\vert -\vert V\vert +1$ 且 $U_iU_{i+1}\cdots U_{i+\vert V\vert -1}=V$ 的整数 $i$ 的数量,其中 $\vert U\vert$ 表示字符串 $U$ 的长度。 现在给出 $N$ 条标准,求出这 $N$ 条标准下“美丽度”最大的**非空**字符串 $S$ 的“美丽度”。如果这个答案是无限大,输出 `Infinity`。 数据范围: + $1\le N\le 18278$,$N$ 为整数。 + $1\le \vert T_i\vert \le 3$,$T_i$ 只包含小写字母。 + 若 $i\neq j$,则 $T_i\neq T_j$。 + $-10^9\le P_i\le 10^9$。 + $P_i$ 均为整数。 样例解释: 样例 $1$:$S$ 为 $X$ 个 $\texttt{abz}$ 相连时,它的“美丽度”是 $5X$,所以 $S$ 的最大“美丽度”无限大。 样例 $2$:$S=\texttt{ab}$ 时“美丽度”最大。 样例 $3$:请注意 $S$ 不能为空。

Output

分数:600分

问题描述

在一个字符串集市上,他们确定了一个非空字符串$S$由小写英文字母组成的美丽

字符串$S$的美丽等于由$N$个标准确定的$N$个分数之和。 对于$i = 1, 2, \ldots, N$,由第$i$个标准确定的分数是 "字符串$T_i$(在输入中给出的长度最多为$3$)在$S$中作为连续子序列出现的次数"乘以$P_i$。

打印由小写英文字母组成的非空字符串$S$的最大可能美丽。 如果有可能获得无限大的美丽,请打印Infinity

这里,字符串$V$作为字符串$U = U_1U_2\ldots U_{|U|}$的连续子序列出现的次数定义为 满足$1 \leq i \leq |U|-|V|+1$且$U_iU_{i+1}\ldots U_{i+|V|-1} = V$的整数$i$的数量。

约束

  • $1 \leq N \leq 18278$
  • $N$是一个整数。
  • $T_i$是一个由小写英文字母组成的长度在$1$和$3$之间的字符串。
  • $i \neq j \Rightarrow T_i \neq T_j$
  • $-10^9 \leq P_i \leq 10^9$
  • $P_i$是一个整数。

输入

输入从标准输入以以下格式给出:

$N$
$T_1$ $P_1$
$T_2$ $P_2$
$\vdots$
$T_N$ $P_N$

输出

打印由小写英文字母组成的非空字符串$S$的最大可能美丽。 如果有可能获得无限大的美丽,请打印Infinity


样例输入1

3
a -5
ab 10
ba -20

样例输出1

Infinity

例如,如果$S =$ abzabz

  • 由第$1$个标准确定的分数是$2 \times (-5) = -10$分,因为a在$S$中作为连续子序列出现两次。
  • 由第$2$个标准确定的分数是$2 \times 10 = 20$分,因为ab在$S$中作为连续子序列出现两次。
  • 由第$3$个标准确定的分数是$0 \times (-20) = 0$分,因为ba在$S$中作为连续子序列出现$0$次。

因此,$S$的美丽是$(-10) + 20 + 0 = 10$。

作为另一个例子,如果$S =$ abzabzabz

  • 由第$1$个标准确定的分数是$3 \times (-5) = -15$分,因为a在$S$中作为连续子序列出现$3$次。
  • 由第$2$个标准确定的分数是$3 \times 10 = 30$分,因为ab在$S$中作为连续子序列出现$3$次。
  • 由第$3$个标准确定的分数是$0 \times (-20) = 0$分,因为ba在$S$中作为连续子序列出现$0$次。

因此,$S$的美丽是$(-15) + 30 + 0 = 15$。

一般来说,对于正整数$X$,如果$S$是$abz$的$X$份副本的连接,则$S$的美丽是$5X$。 由于您可以获得您想要的任何大的美丽,因此应打印Infinity


样例输入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

样例输出2

5

$S = $ ab实现了可能的最大美丽。


样例输入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

样例输出3

-1

注意$S$应该是一个非空字符串。

加入题单

上一题 下一题 算法标签: