electicode
АсосӣКурсҳоМанбаъҳоМасъалаҳо
Ҳамаи масъалаҳо
Khimio
Khayyam Finals 2025

Circle Handshakes - KhIMIO

1 сМаҳдудияти вақт
256 МБМаҳдудияти ҳофиза
Ҳамаи ҳалҳо

Тавсифи масъала

There are NNN people standing in a circle, numbered from 111 to NNN. For each person iii (1≤i≤N)(1 \le i \le N)(1≤i≤N), we are given a direction and a target number of handshakes . The process consists of turns, taken in order .

Ҳал кардан мехоҳӣ?

Ворид шав, кодро ҳамин ҷо навис ва натиҷаро дарҳол бин.

Ворид шудан

electicode© 2026. Ҳамаи ҳуқуқҳо ҳифз шудаанд.

Намуд
Si∈{L,R}S_i \in \{L, R\}
Si​∈{L,R}
AiA_iAi​
NNN
1,2,…,N1,2,\dots,N1,2,…,N

At the start, every person has made 000 handshakes. Let HjH_jHj​ denote the current total number of handshakes of person jjj at any moment. On the iii-th turn:

  • If Hi≥AiH_i \ge A_iHi​≥Ai​, the iii-th person does nothing on this turn.

  • Otherwise, the iii-th person leaves their position and starts moving around the circle in the direction given by SiS_iSi​:

    • If Si=LS_i = LSi​=L, they move anticlockwise: first to person i−1i-1i−1, then i−2i-2i−2, and so on. When they reach person 111, they continue from person , then , and so on.

Visiting people one by one in that order, each time they meet a person x≠ix \ne ix=i and still have Hi<AiH_i < A_iHi​<Ai​, they perform a handshake with xxx (this increases both HiH_iHi​ and HxH_xHx​ by 111). They continue until Hi=AiH_i = A_iHi​=Ai​, then return to their original position.

Note. The target AiA_iAi​ refers to the overall number of handshakes for person iii. Handshakes received during other people's turns count toward HiH_iHi​. If, by the start of turn iii, Hi≥AiH_i \ge A_iHi​≥Ai​, person iii skips their turn entirely.

Determine the final number of handshakes H1,H2,…,HNH_1, H_2, \ldots, H_NH1​,H2​,…,HN​ after all NNN turns are completed.

Input Format

First line contains one integer N (2≤N≤5⋅105)N \ (2 \leq N \leq 5 \cdot 10^5)N (2≤N≤5⋅105).

Second line contains one string consisting of NNN characters, S (Si∈{L,R})S \ (S_i \in \{L, R\})S (Si​∈{L,R}).

Third line contains NNN space-separated integers A1,A2,…,AN (0≤Ai≤109)A_1, A_2, \ldots, A_N \ (0 \leq A_i \leq 10^9)A1​,A2​,…,AN​ (0≤Ai​≤109).

Output Format

Print one line containing NNN space-separated integers, the answer.

Scoring

SubtasksAdditional restrictionsPointsRequired Subtasks
0Sample0-
1N=2N = 2N=210-
2Ai≤1A_i \leq 1Ai​≤110-
3N,Ai≤5⋅103N, A_i \leq 5 \cdot 10^3N,Ai​≤5⋅103220
4N≤5⋅103N \leq 5 \cdot 10^3N≤5⋅103180, 1, 3
5-400, 1, 2, 3, 4

Мисолҳо

Мисол 1
Вуруд
3 RRL 1 2 2
Баромад
1 3 2
Мисол 2
Вуруд
4 RRLL 3 3 0 2
Баромад
3 3 2 2
Мисол 3
Вуруд
5 LRLRL 6 4 4 1 2
Баромад
7 5 4 3 3
NN
N
N−1N-1N−1
  • If Si=RS_i = RSi​=R, they move clockwise: first to person i+1i+1i+1, then i+2i+2i+2, and so on. When they reach person NNN, they continue from person 111, then 222, and so on.