Party Games

Time limit1sMemory limit128 MB

Summary
Given n distinct uppercase names, find the shortest string S such that exactly half the names are <= S and half are > S, breaking ties by the alphabetically smallest S.
Level

Medium6 of 10

Topics
String, Sorting, Greedy, Implementation
Solved
No attempts yet

Problem

You've been invited to a party. The host wants to divide the guests into two teams for party games, with exactly the same number of guests on each team. As she greets each guest on arrival, she wants to tell which team the guest is on, as easily as possible, without having to look up each name on a list.

Being a good computer scientist, you have an idea: give her a single string, and all she has to do is compare a guest's name alphabetically to that string. To make this even easier, the string should be as short as possible.

Given the distinct names of nn party guests (where nn is even), find the shortest possible string SS such that exactly half of the names are less than or equal to SS and exactly half are greater than SS. If several strings share the same shortest length, choose the alphabetically smallest one among them.

(All string comparisons are alphabetical.)

Input

The input may contain multiple test cases.

Each test case begins with an even integer nn (2≤n≤10002 \le n \le 1000) on its own line.

The next nn lines each contain one name. Each name is a single word consisting only of capital letters and is at most 3030 letters long. Within a test case, the names are distinct.

The input ends with a line containing 00.

Output

For each test case, print on its own line the shortest possible string the host could use to separate her guests, with ties broken in favor of the alphabetically smallest string. The printed strings consist entirely of capital letters.

Examples3

  1. Example 1

    Input
    4
    FRED
    SAM
    JOE
    MARGARET
    2
    FRED
    FREDDIE
    2
    JOSEPHINE
    JERRY
    2
    LARHONDA
    LARSEN
    0
    
    Expected output
    K
    FRED
    JF
    LARI
    
  2. Example 2

    Input
    2
    APPLE
    BANANA
    0
    
    Expected output
    B
    
  3. Example 3

    Input
    2
    AB
    ABC
    0
    
    Expected output
    AB