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

#GD0000144. Các tổng tiền tố âm (Negative Prefixes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Các tổng tiền tố âm (Negative Prefixes)

Negative Prefixes

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an integer array aa of length nn. Each position is either locked or unlocked. Values at locked positions must stay unchanged, while all values at unlocked positions may be rearranged among those positions.

Let pj=a1+⋯+ajp_j=a_1+\cdots+a_j. Define kk as the largest index jj such that pj<0p_j<0, or 00 if no negative prefix exists. Output a valid rearrangement minimizing kk.

Input

The first line contains nn. The second contains nn integers aia_i. The third contains nn integers lil_i, where 11 means locked and 00 means unlocked.

Output

Print nn integers representing an array that preserves locked values and the multiset of unlocked values and minimizes the final negative prefix index.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; −109≤ai≤109-10^9\le a_i\le10^9; li∈{0,1}l_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤n≤5001\le n\le500; −109≤ai≤109-10^9\le a_i\le10^9; li∈{0,1}l_i\in\{0,1\}.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le1000; −109≤ai≤109-10^9\le a_i\le10^9; li∈{0,1}l_i\in\{0,1\}.

Examples

Input

4
2 -3 4 -1
1 0 0 1

Output

2 4 -3 -1

Explanation

Positions 11 and 44 are locked. Placing 44 at position 22 and −3-3 at position 33 gives [2,4,−3,−1][2,4,-3,-1]. Its prefix sums are [2,6,3,2][2,6,3,2], so no prefix is negative and the minimum kk is 00.