#GD0000144. Các tổng tiền tố âm (Negative Prefixes)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 of length . 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 . Define as the largest index such that , or if no negative prefix exists. Output a valid rearrangement minimizing .
Input
The first line contains . The second contains integers . The third contains integers , where means locked and means unlocked.
Output
Print 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): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
4
2 -3 4 -1
1 0 0 1
Output
2 4 -3 -1
Explanation
Positions and are locked. Placing at position and at position gives . Its prefix sums are , so no prefix is negative and the minimum is .