TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Let's define a function, , on a string, , of length as follows:

where denotes the ASCII value of the character in string , , and .

Nikita has a string, , consisting of lowercase letters that she wants to perform queries on. Each query consists of an integer, , and you have to find the value of where is the alphabetically smallest palindromic substring of . If doesn't exist, print instead.

Input Format

The first line contains space-separated integers describing the respective values of (the length of string ) and (the number of queries).
The second line contains a single string denoting .
Each of the subsequent lines contains a single integer denoting the value of for a query.

  • It is guaranteed that string consists of lowercase English alphabetic letters only (i.e., to ).
  • .

Output Format

For each query, print the value of function where is the alphabetically smallest palindromic substring of ; if doesn't exist, print instead.

Sample Input 1

5 7
abcba
1
2
3
4
6
7
8  

Sample Output 1

97
97
696207567
98
29493435
99
-1

Hints

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 262144 131072
1 1000 262144 131072
2 1000 262144 131072
3 1000 262144 131072
4 1000 262144 131072
5 1000 262144 131072
6 1000 262144 131072
7 1000 262144 131072
8 1000 262144 131072
9 1000 262144 131072
10 1000 262144 131072
11 1000 262144 131072
12 1000 262144 131072
13 1000 262144 131072
14 1000 262144 131072
15 1000 262144 131072
16 1000 262144 131072
17 1000 262144 131072
18 1000 262144 131072
19 1000 262144 131072
20 1000 262144 131072