Under construction

Still being built. Things may change

Cocktail Party

Time limit1sMemory limit1024 MB

Summary
For each length r from 0 to n-1, count the pairs of cups whose substrings of length r match, and find the largest product of their deliciousness values.
Level

Medium7 of 10

Topics
String, String matching, Union-find, Sorting
Solved
No attempts yet

Problem

You are given a string of length nn with the labels of nn cups of cocktail. The ii-th cup has label sis_i, and every label is one of the 26 lowercase English letters. Let Str(l,r)=slsl+1⋯sr\mathrm{Str}(l,r) = s_l s_{l+1} \cdots s_r be the string formed by the labels from cup ll to cup rr. If Str(p,p0)=Str(q,q0)\mathrm{Str}(p,p_0) = \mathrm{Str}(q,q_0) where 1≤p≤p0≤n1 \le p \le p_0 \le n, 1≤q≤q0≤n1 \le q \le q_0 \le n, p≠qp \ne q, and p0−p+1=q0−q+1=rp_0-p+1 = q_0-q+1 = r, then cup pp and cup qq are called rr-similar. If two cups are rr-similar for some r>1r > 1, they are also 11-similar, 22-similar, ..., and (r−1)(r-1)-similar. For any 1≤p≤q≤n1 \le p \le q \le n with p≠qp \ne q, cups pp and qq are 00-similar.

Freda assigns a deliciousness value aia_i to each cup. Mixing cup pp and cup qq produces a cocktail with deliciousness apaqa_p a_q. For each r=0,…,n−1r = 0, \dots, n-1, the problem asks how many pairs of cups are rr-similar, and the maximum deliciousness obtainable by mixing two rr-similar cups.

Input

The first line contains an integer nn, the number of cups. The second line contains a string SS of length nn, where the ii-th character is the label of cup ii. The third line contains nn integers separated by single spaces, where the ii-th integer is aia_i.

Output

Print nn lines. The ii-th line contains two integers: the number of pairs of (i−1)(i-1)-similar cups, and the maximum deliciousness from mixing two (i−1)(i-1)-similar cups. If no two cups are (i−1)(i-1)-similar, print 0 0 on that line.

Constraints

10≤n≤300 00010 \le n \le 300\,000

∣ai∣≤1 000 000 000|a_i| \le 1\,000\,000\,000

Examples2

  1. Example 1

    Input
    10
    ponoiiipoi
    2 1 4 7 4 8 3 6 4 7
    
    Expected output
    45 56
    10 56
    3 32
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    
  2. Example 2

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