winetasting

Wine Tasting Conference

The annual "Mirage Pavillion Summer Wine Tasting Conference" has just witnessed its grand opening. The event consisted of a Tasting Challenge and a Fun Challenge, with two prizes awarded to the winners, "Top Sommelier" and "Top Hunter", attracting many tasters to participate.

At the conference dinner, the bartender Rainbow made n cocktails. The cocktails were arranged in a row, with the ith glass (1in) labelled one of the 26 lowercase letters of the alphabet si. Let Str(l,r) denote the string consisting of the sequentially concatenated rl+1 labels of the the lth glass to the rth glass. If Str(p,p0)=Str(q,q0), where 1p0pn, 1q0qn, pq, and p0p+1=q0q+1=r, then the pth glass is said to be "r-alike" to the qth glass. Of course, two glasses of wine that are "r-alike" (r>1) are also "1-alike", "2-alike", and "(r1)-alike". In particular, for any 1p, qn, pq, the pth glass is "0-alike" to the qth glass.

During the tasting session, Sommelier Freda assessed the tastiness of each drink, and with her professionalism and experience easily won the title of "Top Sommelier". The ith drink (1in) was hence rated ai. Rainbow then announced the question for the Fun Challenge: If the pth cocktail were to be mixed with the qth cocktail, you would get a drink with tastiness apaq. He asks the sommeliers: For each r=0,1,2,,n1, count the number of ways to select two "r-alike" cocktails, and also find the maximum tastiness that can be obtained by blending two "r-alike" cocktails.

Limits

1n3×105, 1|ai|109.

Subtask # Score Constraints
1 15 n500
|ai|10000
2 25 n2000
3 10 n99991
No pairs of 10-alike cocktails exist.
4 20 All values of ai are equal.
5 30 No additional constraints

Input format

The first line of input consists of a single integer n, the number of glasses of cocktails.
The second line of input consists of a length n string of lowercase letters S, the ith character of which represents glass i's label.
The third line of input contains n, the ith of which represents the tastiness ai of cocktail i.

Output format

Output n lines each containing two integers. On the ith line, the first integer shall be the number of ways to choose two (i1)-alike cocktail glasses, and the second integer shall be the greatest tastiness achievable by mixing two (i1)-alike cocktails. If no pairs of (i1)-alike cocktails exist, output 0 for both integers.

Samples

Sample Input 1 Sample Output 1
10
ponoiiipoi
2 1 4 7 4 8 3 6 4 7
45 56
10 56
3 32
0 0
0 0
0 0
0 0
0 0
0 0
0 0

  • 0-alike: All 45 pairs of cocktails are 0-alike. The tastiest mix has tastiness 8×7=56.
  • 1-alike: The 1-alike pairs of cocktails are (1,8),(2,4),(2,9),(4,9),(5,6),(5,7),(5,10),(6,7),(6,10), and (7,10). The tastiest mix remains 8×7=56.
  • 2-alike: The 2-alike pairs of cocktails are (1,8),(4,9), and (5,6). The tastiest of them has tastiness 4×8=32.
  • No pairs of cocktails are 3-alike, 4-alike, , or 9-alike.

Sample Input 2 Sample Output 2
12
abaabaabaaba
1 -2 3 -4 5 -6 7 -8 9 -10 11 -12
66 120
34 120
15 55
12 40
9 27
7 16
5 7
3 -4
2 -4
1 -4
0 0
0 0


Submitting .cpp to 'winetasting'


You're not logged in! Click here to login

Time Limit: 1 Seconds
Memory Limit: 512MB
Your best score: 0
Source: LOJ #2133

Subtask Score
1 15
2 25
3 10
4 20
5 30