PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000081. Ký tự nhỏ nhất lớn hơn mục tiêu (Find Smallest Letter Greater Than Target)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ký tự nhỏ nhất lớn hơn mục tiêu (Find Smallest Letter Greater Than Target)

Find Smallest Letter Greater Than Target

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a sorted array of lowercase English letters containing at least two distinct letters and a target letter tt, return the smallest letter strictly greater than tt; wrap around to the first letter if none exists.

Input

The first line contains nn. The second line contains nn space-separated lowercase letters, and the third line contains tt.

Output

Print the requested lowercase letter.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le 12; all letters and tt are lowercase a–z; the array is non-decreasing and has at least two distinct letters.
  • Subtask 2 (30 points): 2≤n≤10002\le n\le 1000; all letters and tt are lowercase a–z; the array is non-decreasing and has at least two distinct letters.
  • Subtask 3 (50 points): 2≤n≤100002\le n\le 10000; all letters and tt are lowercase a–z; the array is non-decreasing and has at least two distinct letters.

Examples

Input

3
c f j
c

Output

f

Explanation

The letters are c, f, j and the target is c. Equal is not allowed. Both f and j qualify; the smaller is f.