PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000025. Sở thích của Sakurako (Sakurako’s Hobby)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sở thích của Sakurako (Sakurako’s Hobby)

Sakurako’s Hobby

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation pp of 1,2,…,n1,2,\ldots,n. Every position has one of two colors specified by a binary string ss of length nn: character 0 means black and 1 means white.

From position ii, you may move to position pip_i. From the new position jj, you may move again to pjp_j, and may continue as many times as you like. The starting position is also considered reachable. Since pp is a permutation, the movement will eventually revisit positions.

Let F(i)F(i) be the number of distinct black positions reachable when starting from ii. Compute F(1),F(2),…,F(n)F(1),F(2),\ldots,F(n). A position is counted only once, even if it can be visited repeatedly.

Input

The first line contains nn, the second a permutation p1,…,pnp_1,\ldots,p_n, and the third a binary string ss of length nn.

Output

Print F(1),…,F(n)F(1),\ldots,F(n).

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; pp is a permutation of 1,…,n1,\ldots,n; ∣s∣=n|s|=n and si∈{0,1}s_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; pp is a permutation of 1,…,n1,\ldots,n; ∣s∣=n|s|=n and si∈{0,1}s_i\in\{0,1\}.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; pp is a permutation of 1,…,n1,\ldots,n; ∣s∣=n|s|=n and si∈{0,1}s_i\in\{0,1\}.

Examples

Input

6
3 5 6 1 2 4
010011

Output

3 0 3 3 0 3

Explanation

The permutation consists of the cycles (1,3,6,4)(1,3,6,4) and (2,5)(2,5). In the first cycle, positions 1,3,41,3,4 are black, so every position in that cycle can reach exactly three distinct black positions. The second cycle contains no black positions. The result is 3 0 3 3 0 3.