This page is still under construction.

Parts of this page are still being built. What you see may change.

N-dimensional travel

Interview

Time limit2sMemory limit512 MB

Summary
Given a walk on an N-dimensional integer grid as a list of coordinate indices and signs, decide whether all visited points, including start and end, are distinct.
Level

Medium5 of 10

Topics
Hash map, Implementation, Math, Simulation
Solved
No attempts yet

Problem

Subin likes to travel. With nowhere left to go on Earth, Subin sets off into an N-dimensional universe. A point of this universe is written as N coordinates, and the coordinates are indexed from 1 to N.

Subin starts at the origin, the point whose every coordinate is 0, and each move takes two steps.

  • Choose the index of the coordinate to move, one of 1 to N.
  • Move to the point where that coordinate is 1 larger or 1 smaller. Every other coordinate stays as it was before the move.

Before leaving, Subin wrote down a travel plan. The plan lists, in order, the coordinate index chosen for each move and whether the move increases or decreases that coordinate.

Write a program that follows the plan and prints 1 if every point, including the starting point and the last point, is visited exactly once, and 0 if some point is visited two or more times.

Input

The first line contains the number of dimensions NN. (1≤N≤1091 \le N \le 10^9)

The second line contains the length MM of the travel plan. (1≤M≤501 \le M \le 50)

The third line contains the MM coordinate indices chosen in the plan, in move order, separated by spaces. Each index is between 11 and NN.

The fourth line contains a string of length MM. If its ii-th character is +, the ii-th move increases the coordinate by 1, and if it is -, the move decreases the coordinate by 1.

Output

Print 1 if every point is visited exactly once, and 0 otherwise.

Hint

In two dimensions, choosing indices 1, 2, 1, 2 with directions +, +, -, - moves along (0,0)→(1,0)→(1,1)→(0,1)→(0,0)(0,0) \to (1,0) \to (1,1) \to (0,1) \to (0,0), so (0,0)(0,0) is visited twice.

In three dimensions, choosing indices 1, 2, 3, 1, 2 with directions +, +, +, -, - moves along (0,0,0)→(1,0,0)→(1,1,0)→(1,1,1)→(0,1,1)→(0,0,1)(0,0,0) \to (1,0,0) \to (1,1,0) \to (1,1,1) \to (0,1,1) \to (0,0,1), and no point is visited twice.

Examples3

  1. Example 1

    Input
    1
    1
    1
    +
    
    Expected output
    1
    
  2. Example 2

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

    Input
    3
    5
    1 2 3 1 2
    +++--
    
    Expected output
    1