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

#FT0000018. Mancala 2 – Phân phối bóng theo vòng tròn (Mancala 2)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mancala 2 – Phân phối bóng theo vòng tròn (Mancala 2)

Mancala 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn boxes arranged in a circle and numbered 00 through n−1n-1. Initially, box ii contains AiA_i balls. Takahashi performs mm redistribution operations using box indices B1,B2,…,BmB_1,B_2,\ldots,B_m.

In operation jj, he takes every ball out of box BjB_j, leaving that box empty. He then places one ball into each successive box in increasing cyclic order: first (Bj+1) mod n(B_j+1)\bmod n, then (Bj+2) mod n(B_j+2)\bmod n, and so on until he has no balls left. After box n−1n-1, he wraps around to box 00, so a box can receive multiple balls during one operation.

Each operation uses the box counts resulting from preceding operations. After all mm operations, output the numbers of balls in boxes 00 through n−1n-1 in index order. No intermediate answers are requested.

Input

  • Line 1: n m gives number of boxes and redistribution operations.
  • Line 2: A_0 A_1 ... A_(n-1) gives the initial values in index order.
  • Line 3: B_1 B_2 ... B_m lists the indices of the boxes chosen in the successive operations; each index is zero-based.

Output

Print exactly nn space-separated integers on one line. The (i+1)(i+1)-st number is the number of balls in box ii after mm operations.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤241\le n\le 24.
  • 1≤m≤301\le m\le 30.
  • 0≤ai≤1090\le a_i\le 10^9 for all 0≤i<n0\le i<n.
  • 0≤bj<n0\le b_j<n for all 1≤j≤m1\le j\le m.

Subtask 2 (30 points):

  • 1≤n≤12001\le n\le 1200.
  • 1≤m≤16001\le m\le 1600.
  • 0≤ai≤1090\le a_i\le 10^9 for all 0≤i<n0\le i<n.
  • 0≤bj<n0\le b_j<n for all 1≤j≤m1\le j\le m.

Subtask 3 (50 points):

  • 1≤n≤2000001\le n\le 200000.
  • 1≤m≤2000001\le m\le 200000.
  • 0≤ai≤1090\le a_i\le 10^9 for all 0≤i<n0\le i<n.
  • 0≤bj<n0\le b_j<n for all 1≤j≤m1\le j\le m.

Examples

Input

5 3
1 2 3 4 5
2 4 0

Output

0 4 2 7 2

Explanation

Initially the five boxes contain [1, 2, 3, 4, 5] balls. The three chosen box indices are 2, 4, and 0.

  1. Operation 1, box 2: take its 33 balls and set its count to 00. Give one ball to boxes 3, 4, and 0 in that order, wrapping around after index 4. The counts become [2, 2, 0, 5, 6].

  2. Operation 2, box 4: take its 66 balls and distribute them to boxes 0, 1, 2, 3, 4, 0. Box 0 receives two balls, and every other box receives one. The new counts are [4, 3, 1, 6, 1].

  3. Operation 3, box 0: take its 44 balls, then place one each in boxes 1, 2, 3, 4. The final counts are [0, 4, 2, 7, 2].

The answer is printed in box-index order on a single line: 0 4 2 7 2. The total of 1515 balls is conserved.