f | from itertools import chain | f | from itertools import chain |
| | | |
| def joinseq(*seq): | | def joinseq(*seq): |
| s = [] | | s = [] |
| for t in seq: | | for t in seq: |
| s.append(list(chain(t))) | | s.append(list(chain(t))) |
| while s: | | while s: |
n | min_sym = min((sym[0] for sym in s)) | n | min_ = min((w[0] for w in s)) |
| for sym in s: | | for w in s: |
| if sym[0] == min_sym: | | if w[0] == min_: |
| sym.remove(min_sym) | | w.remove(min_) |
| if not sym: | | if not w: |
| s.remove(sym) | | s.remove(w) |
| break | | break |
t | yield min_sym | t | yield min_ |
| import sys | | import sys |
| exec(sys.stdin.read(), globals()) | | exec(sys.stdin.read(), globals()) |