Utopia Divided

Time limit1sMemory limit128 MB

Summary
Assign 2N distinct numbers into N signed x/y pairs so the teleporter visits the given regions in order, choosing the lexicographically smallest guiding.
Level

Medium7 of 10

Topics
Greedy, Backtracking, Implementation, Brute force
Solved
No attempts yet

Problem

Long ago the land of Utopia was split by war into four regions, separated by one vertical line (a north-south longitude) and one horizontal line (an east-west latitude). Their crossing point is the origin (0,0)(0, 0). A location is described by two numbers: how far east and how far north it lies from the origin, and either number may be negative. The four regions are:

  • Utopia 1 — the northeast, where x>0x > 0 and y>0y > 0;
  • Utopia 2 — the northwest, where x<0x < 0 and y>0y > 0;
  • Utopia 3 — the southwest, where x<0x < 0 and y<0y < 0;
  • Utopia 4 — the southeast, where x>0x > 0 and y<0y < 0.

Citizens may not cross a border, so travel is done by a teleporter that starts at the origin (0,0)(0, 0). The teleporter is driven by code numbers: you are given 2N2N distinct positive code numbers, each of which may be used exactly once. Using a code pair (±u,±v)(\pm u, \pm v) at the current point (x,y)(x, y) moves the teleporter to (x±u, y±v)(x \pm u,\ y \pm v). You choose the order of the 2N2N numbers, split them into NN pairs, and give each number a ++ or −- sign; in each pair one number is the xx-displacement and the other is the yy-displacement.

You are given a sequence of NN region numbers. After the ii-th teleport the machine must land strictly inside the ii-th requested region (the signs of xx and yy must match that region); it may never come to rest on a border, i.e. never on a line x=0x = 0 or y=0y = 0. Two or more consecutive requests may name the same region.

For example, with code numbers 7 5 6 1 3 2 4 8 and region sequence 4 1 2 1, the code pairs (+1,−7),(+2,+8),(−6,+3),(+4,+5)(+1, -7), (+2, +8), (-6, +3), (+4, +5) move the teleporter (0,0)→(1,−7)→(3,1)→(−3,4)→(1,9)(0,0) \to (1,-7) \to (3,1) \to (-3,4) \to (1,9), which lie in Utopia 4,1,2,14, 1, 2, 1 respectively. Among all valid guidings for this input, that one is the lexicographically smallest.

A valid guiding always exists. When more than one exists, you must report the lexicographically smallest one, as defined in the output section.

Input

The first line contains an integer NN (1≤N≤101 \le N \le 10).

The second line contains the 2N2N distinct code numbers, integers with 1≤code number≤1000001 \le \text{code number} \le 100000, separated by single spaces.

The third line contains the sequence of NN region numbers, each equal to 11, 22, 33, or 44, separated by single spaces.

Output

Print NN lines. The ii-th line describes the ii-th code pair as sx sy, where sx is the xx-displacement and sy is the yy-displacement. Each displacement is written with a leading sign (+ or -) immediately followed by its magnitude (no space after the sign), and the two displacements are separated by a single space.

A valid guiding always exists. When several valid guidings exist, print the lexicographically smallest one. Guidings are compared by the sequence of signed displacements in order — dx1,dy1,dx2,dy2,…,dxN,dyNdx_1, dy_1, dx_2, dy_2, \dots, dx_N, dy_N (at each step the xx-displacement comes before the yy-displacement) — where the integers are compared by value, so a more negative displacement is considered smaller.

Examples5

  1. Example 1

    Input
    4
    7 5 6 1 3 2 4 8
    4 1 2 1
    
    Expected output
    +1 -7
    +2 +8
    -6 +3
    +4 +5
    
  2. Example 2

    Input
    4
    2 5 4 1 7 8 6 3
    4 2 2 1
    
    Expected output
    +1 -7
    -6 +8
    +2 +3
    +4 +5
    
  3. Example 3

    Input
    1
    3 8
    1
    
    Expected output
    +3 +8
    
  4. Example 4

    Input
    2
    1 2 3 4
    1 3
    
    Expected output
    +1 +2
    -4 -3
    
  5. Example 5

    Input
    4
    10 20 30 40 50 60 70 80
    1 2 3 4
    
    Expected output
    +10 +20
    -80 +30
    +40 -70
    +50 -60