Under construction

Still being built. Things may change

Broken Piano

Interview

Time limit0.5sMemory limit1024 MB

Summary
Given broken piano keys and a MIDI piece, count the notes that cannot be played and find the smallest transposition that makes the whole piece playable.
Level

Easy3 of 10

Topics
Brute force, Array, Implementation
Solved
No attempts yet

Problem

MIDI (Musical Instrument Digital Interface) is a technical standard for describing musical pieces. Each note is an integer in the range 0…1 0230 \ldots 1\,023. You have a piano with 1 0241\,024 keys and want to play a piece given in MIDI format. The leftmost key is MIDI code 00, the second key from the left is code 11, and so on.

Unfortunately the piano is old, and some keys are broken. You know which keys work and which do not. No working key breaks while the piece is played, and no broken key starts working.

Write a program that finds:

  • the number of notes in the piece that cannot be played on the piano, and
  • the minimal transposition needed to play the piece on the piano.

Transposition shifts every note in the piece up or down by the same amount. When the piece is transposed up by one half-tone, each note is played on the key to its right instead. When it is transposed down by one half-tone, the key to its left is played. Two half-tones up means the key two positions to the right, and so on. When transposed up by one half-tone, the note with MIDI code 1 0231\,023 cannot be played, because no key corresponds to it.

Input

The first line contains NN, the number of broken keys (1≤N≤1 0241 \le N \le 1\,024). The second line contains NN distinct integers AiA_i (0≤Ai≤1 0230 \le A_i \le 1\,023), the MIDI codes of the broken keys. The third line contains MM, the number of notes in the piece (1≤M≤1061 \le M \le 10^6). The fourth line contains MM integers BiB_i (0≤Bi≤1 0230 \le B_i \le 1\,023), the MIDI codes of the notes.

Output

Output two lines. The first line is the number of notes in the piece that cannot be played on the piano. The second line is the number of half-tones the piece must be transposed by so that it can be played. If several transpositions work, output the one with the smallest absolute value. If no transposition works, output X on the second line. If your solution cannot determine the transposition, output E on the second line.

Examples2

  1. Example 1

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

    Input
    2
    0 1023
    4
    0 1023 1023 1023
    
    Expected output
    4
    X