Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x_1, x_2, ..., x_{k} (k > 1) is such maximum element x_{j}, that the following inequality holds: $x_{j} \neq \operatorname{max}_{i = 1}^{k} x_{i}$.

The lucky number of the sequence of distinct positive integers x_1, x_2, ..., x_{k} (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.

You've got a sequence of distinct positive integers s_1, s_2, ..., s_{n} (n > 1). Let's denote sequence s_{l}, s_{l} + 1, ..., s_{r} as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].

Note that as all numbers in sequence s are distinct, all the given definitions make sence.


-----Input-----

The first line contains integer n (1 < n ≤ 10^5). The second line contains n distinct integers s_1, s_2, ..., s_{n} (1 ≤ s_{i} ≤ 10^9).


-----Output-----

Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].


-----Examples-----
Input
5
5 2 1 4 3

Output
7

Input
5
9 8 3 5 7

Output
15



-----Note-----

For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].

For the second sample you must choose s[2..5] = {8, 3, 5, 7}.

Difficulty: MEDIUM_HARD
Tags: Amortized analysis

Data structures

Implementation
Skill types: Data structures

Amortized analysis

Starter code:

Solutions:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ans = 0
d = []
for i in a:
	while len(d) and d[-1] < i:
		ans = max(ans, d[-1] ^ i)
		d.pop()
	if len(d):
		ans = max(ans, d[-1] ^ i)
	d.append(i)
d = []
print(ans)


n = int(input())
l = [int(i) for i in input().split()]
ans = 0
stack = []
for i in range(n):
	while len(stack) != 0:
		if l[i] > stack[len(stack) - 1]:
			ans = max(ans, l[i] ^ stack[len(stack) - 1])
			stack.pop()
		else:
			ans = max(ans, l[i] ^ stack[len(stack) - 1])
			break
	stack.append(l[i])
print(ans)


from collections import deque

def ngr(a, n):
	s = deque()
	l = []
	for j in range(n - 1, -1, -1):
		if len(s) == 0:
			s.append(a[j])
			l.append(-1)
		else:
			while len(s) != 0 and s[len(s) - 1] <= a[j]:
				s.pop()
			if len(s) == 0:
				l.append(-1)
			else:
				l.append(s[len(s) - 1] ^ a[j])
			s.append(a[j])
	l.reverse()
	return l

def ngl(a, n):
	s = deque()
	l = []
	for j in range(n):
		if len(s) == 0:
			s.append(a[j])
			l.append(-1)
		else:
			while len(s) != 0 and s[len(s) - 1] <= a[j]:
				s.pop()
			if len(s) == 0:
				l.append(-1)
			else:
				l.append(s[len(s) - 1] ^ a[j])
			s.append(a[j])
	return l
n = int(input())
a = list(map(int, input().split()))
c = ngr(a, n)
d = ngl(a, n)
c.extend(d)
print(max(c))


n = int(input())
a = list(map(int, input().strip().split()))
stack = []
max_xor = 0
for k in range(n):
	if not stack:
		stack.append(a[k])
	elif a[k] < stack[-1]:
		max_xor = max(max_xor, a[k] ^ stack[-1])
		stack.append(a[k])
	elif a[k] > stack[-1]:
		while stack and a[k] > stack[-1]:
			popped = stack.pop()
			max_xor = max(max_xor, a[k] ^ popped)
		if stack:
			max_xor = max(max_xor, a[k] ^ stack[-1])
		stack.append(a[k])
print(max_xor)


n = int(input())
nl = [int(x) for x in input().split()]
st = []
ans = 0
for x in nl:
	if st and st[-1] > x:
		ans = max(ans, st[-1] ^ x)
	elif st:
		while st and st[-1] < x:
			ans = max(ans, st.pop() ^ x)
		if st:
			ans = max(ans, st[-1] ^ x)
	st.append(x)
print(ans)


import sys
inp = sys.stdin.readlines()
n = int(inp[0])
arr = list(map(int, inp[1].split()))
maxglobal = -1
top = -1
stack = [0] * n
for i in range(n):
	while top != -1:
		maxglobal = max(maxglobal, stack[top] ^ arr[i])
		if stack[top] < arr[i]:
			top -= 1
		else:
			break
	stack[top + 1] = arr[i]
	top += 1
print(maxglobal)


m = int(input())
l = list(map(int, input().split()))
max_lucky = 0
stack = []
for i in range(m):
	while stack:
		if stack[-1] < l[i]:
			max_lucky = max(stack[-1] ^ l[i], max_lucky)
			stack.pop()
		else:
			max_lucky = max(stack[-1] ^ l[i], max_lucky)
			break
	stack.append(l[i])
print(max_lucky)


n = int(input())
arr = list(map(int, input().split()))
s = []
ans = 0
for i in arr:
	while s:
		ans = max(ans, s[-1] ^ i)
		if s[-1] > i:
			break
		else:
			s.pop()
	s.append(i)
print(ans)


def xor(a, b):
	return a ^ b

def solve(l):
	ans = xor(l[0], l[1])
	st = [l[0], l[1]]
	for i in range(2, n):
		while len(st) > 0 and st[-1] < l[i]:
			st.pop()
		st.append(l[i])
		if len(st) >= 2:
			ans = max(ans, xor(st[-1], st[-2]))
	return ans
n = int(input())
l = input()
l = [int(i) for i in l.split()]
ans = solve(l)
l.reverse()
ans = max(ans, solve(l))
print(ans)


from math import *
import functools
input()
numbs = [int(s) for s in input().split()]

def getMaxPot2(x):
	res = 1
	while 2 * res <= x:
		res *= 2
	return res
a = functools.reduce(lambda x, y: x & y, numbs)
numbs = [a ^ s for s in numbs]
maxPot2 = getMaxPot2(max(numbs))
res = 0
for (i, val) in enumerate(numbs):
	if val & maxPot2 > 0:
		(max2, j) = (-1, i - 1)
		while j >= 0 and numbs[j] & maxPot2 == 0:
			if numbs[j] > max2:
				max2 = numbs[j]
				res = max(res, val ^ max2)
			j -= 1
		(max2, j) = (-1, i + 1)
		while j < len(numbs) and numbs[j] & maxPot2 == 0:
			if numbs[j] > max2:
				max2 = numbs[j]
				res = max(res, val ^ max2)
			j += 1
print(res)


n = input()
nos = [int(x) for x in input().split()]
(stack, ans) = ([], 0)
for n in nos:
	while stack:
		ans = max(ans, stack[-1] ^ n)
		if stack[-1] > n:
			break
		else:
			stack.pop()
	stack.append(n)
print(ans)


def f(a):
	stack = []
	ans = -1
	for i in range(0, len(a)):
		while stack and a[i] >= stack[-1]:
			ans = max(ans, a[i] ^ stack[-1])
			stack.pop(-1)
		stack.append(a[i])
	return ans
a = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
an = -1
an = max(f(l), f(l[::-1]))
print(an)


def maximum_xor_secondary(sequence):
	(stack, answer) = ([], 0)
	for x in sequence:
		while stack:
			answer = max(answer, stack[-1] ^ x)
			if stack[-1] > x:
				break
			else:
				stack.pop()
		stack.append(x)
	return answer
(size, num) = (input(), [int(x) for x in input().split()])
print(maximum_xor_secondary(num))


n = int(input())
arr = list(map(int, input().strip().split(' ')))
res = 0
st = []
for i in range(n):
	while st:
		res = max(res, arr[st[-1]] ^ arr[i])
		if arr[i] > arr[st[-1]]:
			st.pop(-1)
		else:
			break
	st.append(i)
print(res)


n = int(input())
nlist = list(map(int, input().split()))
stack = list()
result = 0
for i in range(n):
	while stack != []:
		result = max(result, stack[-1] ^ nlist[i])
		if stack[-1] > nlist[i]:
			break
		else:
			stack.pop()
	stack.append(nlist[i])
print(result)


n = int(input())
l = [int(e) for e in input().split()]
s = [l[0]]
m = 0
for i in range(1, n):
	if l[i] < s[-1]:
		m = max(m, l[i] ^ s[-1])
	else:
		while len(s) != 0 and l[i] > s[-1]:
			x = s.pop()
			m = max(m, l[i] ^ x)
		if len(s) != 0:
			m = max(m, l[i] ^ s[-1])
	s.append(l[i])
print(m)


import sys
n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
m = 0
for i in range(1, n):
	if len(b) > 0:
		m = max(m, b[-1] ^ a[i])
	while len(b) > 0 and a[i] > b[-1]:
		m = max(m, b[-1] ^ a[i])
		b.pop()
	if len(b) > 0:
		m = max(m, b[-1] ^ a[i])
	b.append(a[i])
print(m)


import sys

def input():
	return sys.stdin.readline().strip()
Stack = []
N = int(input())
A = list(map(int, input().split(' ')))
Ans = 0
for i in A:
	while len(Stack) != 0:
		if i > Stack[-1]:
			Ans = max(Ans, i ^ Stack[-1])
			Stack.pop()
		else:
			Ans = max(Ans, Stack[-1] ^ i)
			break
	Stack.append(i)
print(Ans)


n = int(input())
a = list(map(int, input().split()))
st = a[:2]
mx = a[0] ^ a[1]
for i in a[2:]:
	while st and st[-1] < i:
		st.pop()
	st.append(i)
	l = st.pop()
	if st:
		mx = max(mx, st[-1] ^ l)
	st.append(l)
st = a[:n - 3:-1]
mx = max(mx, a[n - 1] ^ a[n - 2])
for i in a[n - 3::-1]:
	while st and st[-1] < i:
		st.pop()
	st.append(i)
	l = st.pop()
	if st:
		mx = max(mx, st[-1] ^ l)
	st.append(l)
print(mx)


from sys import stdin
ip = stdin.readline
n = int(ip())
a = list(map(int, ip().split()))
stk = [a[0]]
ans = 0
for i in range(1, n):
	val = a[i]
	ans = max(ans, val ^ a[i - 1])
	while stk and stk[-1] < val:
		ans = max(ans, val ^ stk.pop())
	if stk:
		ans = max(ans, stk[-1] ^ val)
	stk.append(val)
print(ans)


n = int(input())
li_t = list(map(int, input().split()))
stk1 = []
ans = 0
for i in li_t:
	while stk1 != []:
		if stk1[-1] < i:
			ans = max(ans, stk1[-1] ^ i)
			stk1.pop()
		else:
			ans = max(ans, stk1[-1] ^ i)
			break
	stk1.append(i)
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
st = []
ans = 0
for i in a:
	while st:
		if st[-1] < i:
			ans = max(ans, st[-1] ^ i)
			st.pop()
		else:
			ans = max(ans, st[-1] ^ i)
			break
	st.append(i)
print(ans)


n = int(input())
sn = list(map(int, input().split()))
ans = 0
stack = []
for i in range(n):
	while len(stack) and sn[i] > sn[stack[-1]]:
		l = stack.pop()
		ans = max(ans, sn[l] ^ sn[i])
	if len(stack):
		ans = max(ans, sn[i] ^ sn[stack[-1]])
	stack.append(i)
print(ans)


from sys import stdin
ip = stdin.readline
ip()
a = list(map(int, ip().split()))
stk = []
ans = 0
for i in a:
	while stk:
		ans = max(ans, stk[-1] ^ i)
		if stk[-1] > i:
			break
		stk.pop()
	stk.append(i)
print(ans)


x = input()
l = list(map(int, input().split()))
ans = 0
d = []
for i in l:
	while d != []:
		if d[-1] < i:
			ans = max(ans, d[-1] ^ i)
			d.pop()
		else:
			ans = max(ans, d[-1] ^ i)
			break
	d.append(i)
print(ans)


def get_max_xor(num_list):
	stack = []
	max_xor = num_list[0] ^ num_list[1]
	for i in range(len(num_list)):
		while stack and stack[-1] < num_list[i]:
			stack.pop()
		stack.append(num_list[i])
		if len(stack) > 1:
			max_xor = max(max_xor, stack[-1] ^ stack[-2])
	return max_xor
list_len = int(input())
num_list = list(map(int, input().split()))
print(max(get_max_xor(num_list), get_max_xor(num_list[::-1])))


def empty(l):
	return len(l) == 0

def lucky_number(arr):
	st = []
	l = 0
	for d in arr:
		while not empty(st) and st[-1] < d:
			l = max(l, st.pop() ^ d)
		if not empty(st):
			l = max(l, st[-1] ^ d)
		st.append(d)
	d = 0
	if not empty(st):
		d = st.pop()
	while not empty(st):
		l = max(l, d ^ st[-1])
		d = st.pop()
	return l
N = int(input())
arr = list(map(int, input().split()))
print(lucky_number(arr))


n = int(input())
s = list(map(int, input().split()))
stack = []
ans = 0
for i in s:
	while stack and stack[-1] < i:
		t = stack.pop()
		ans = max(ans, t ^ i)
	stack.append(i)
	if stack[0] != stack[-1]:
		ans = max(ans, stack[-1] ^ stack[-2])
print(ans)


n = int(input())
[first_value, *sn] = list(map(int, input().strip().split()))
ans = 0
stack = [None] * n
top = 0
stack[top] = first_value
for s in sn:
	if s > stack[top]:
		while top > -1 and s > stack[top]:
			ans = max(ans, s ^ stack[top])
			top -= 1
		if top > -1:
			ans = max(ans, s ^ stack[top])
		top += 1
		stack[top] = s
	else:
		ans = max(ans, s ^ stack[top])
		top += 1
		stack[top] = s
print(ans)


def fun(numbers):
	le = len(numbers)
	stack = []
	ans = 0
	i = 1
	for i in numbers:
		while stack:
			ans = max(ans, stack[-1] ^ i)
			if stack[-1] > i:
				break
			else:
				stack.pop()
		stack.append(i)
	return ans
n = input()
user_input = input().split(' ')
numbers = [int(x.strip()) for x in user_input]
print(fun(numbers))


n = int(input())
li = [int(x) for x in input().split()]
stack = []
large = 0
for i in range(0, n):
	while len(stack) != 0:
		if stack[-1] < li[i]:
			large = max(large, stack[-1] ^ li[i])
			stack.pop()
		else:
			large = max(large, stack[-1] ^ li[i])
			break
	stack.append(li[i])
print(large)


num = int(input())
arr = [int(i) for i in input().split()]
ans = 0
stack = [arr[0]]
for i in arr:
	while stack:
		ans = max(ans, stack[-1] ^ i)
		if stack[-1] > i:
			break
		else:
			stack.pop()
	stack.append(i)
print(ans)


n = int(input())
li = [int(x) for x in input().split()]
monD = []
ans = 0
for (i, j) in zip(li, range(0, len(li))):
	if j == 0:
		monD.append(i)
	else:
		fl = 1
		if monD[-1] > i:
			ans = max(ans, i ^ monD[-1])
			monD.append(i)
		else:
			while monD[-1] < i:
				ans = max(ans, i ^ monD[-1])
				_ = monD.pop()
				if len(monD) == 0:
					fl = 0
					break
			if fl != 0:
				ans = max(ans, i ^ monD[-1])
			monD.append(i)
print(ans)


size = int(input())
array = list(map(int, input().split()))
mystack = []
score = None
maxim = 0
index = 0
while index < size:
	if not mystack or array[mystack[-1]] > array[index]:
		mystack.append(index)
		index += 1
	else:
		while mystack and array[mystack[-1]] < array[index]:
			top = mystack.pop()
			score = int(array[top] ^ array[index])
			maxim = max(score, maxim)
		mystack.append(index)
		index += 1
mystack.clear()
index = size - 1
while index >= 0:
	if not mystack or array[mystack[-1]] > array[index]:
		mystack.append(index)
		index -= 1
	else:
		while mystack and array[mystack[-1]] < array[index]:
			top = mystack.pop()
			score = int(array[top] ^ array[index])
			maxim = max(score, maxim)
		mystack.append(index)
		index -= 1
print(maxim)


n = int(input())
a = list(map(int, input().split()))
s = list()
ans = 0
s.append(a[0])
for i in range(1, n):
	while s and a[i] > s[-1]:
		ans = max(ans, a[i] ^ s.pop())
	if s:
		ans = max(ans, a[i] ^ s[-1])
	s.append(a[i])
print(ans)


n = int(input())
a = list(map(int, input().split()))
stack = []
xor = 0
for i in range(n):
	while stack != [] and stack[-1] < a[i]:
		xor = max(xor, a[i] ^ stack[-1])
		stack.pop()
	if stack != []:
		xor = max(xor, a[i] ^ stack[-1])
	stack.append(a[i])
print(xor)


n = int(input().strip())
sequence = [int(x) for x in input().strip().split()]
stack = []
max_xor = 0
for bar in sequence:
	while stack:
		max_xor = max(max_xor, stack[-1] ^ bar)
		if stack[-1] < bar:
			stack.pop()
		else:
			break
	stack.append(bar)
print(max_xor)


n = int(input())
a = list(map(int, input().split()))
m = 0
stack = []
for i in a:
	while stack:
		m = max(m, stack[-1] ^ i)
		if stack[-1] > i:
			break
		else:
			stack.pop()
	stack.append(i)
print(m)


from collections import deque
stack = deque()
n = int(input())
a = list(map(int, input().split()))
res1 = 0
l = 0
for i in a:
	while stack and stack[-1] < i:
		stack.pop()
		l -= 1
	stack.append(i)
	l += 1
	if l > 1:
		res1 = max(res1, stack[-1] ^ stack[-2])
stack.clear()
l = 0
res2 = 0
a.reverse()
for i in a:
	while stack and stack[-1] < i:
		stack.pop()
		l -= 1
	stack.append(i)
	l += 1
	if l > 1:
		res2 = max(res2, stack[-1] ^ stack[-2])
print(max(res1, res2))


n = int(input())
a = list(map(int, input().split()))
ans = -1
stack = []
for i in range(n):
	while stack != []:
		ans = max(ans, stack[-1] ^ a[i])
		if a[i] > stack[-1]:
			stack.pop()
		else:
			break
	stack.append(a[i])
print(ans)


RI = lambda : [int(x) for x in input().split()]
rw = lambda : [input().strip()]
n = int(input())
l = RI()
stack = []
index_stack = -1
maxo = 0
i = 0
while i < n:
	if index_stack == -1 or stack[index_stack] > l[i]:
		stack.append(l[i])
		index_stack += 1
		i += 1
	else:
		maxo = max(stack[index_stack] ^ l[i], maxo)
		temp = stack[index_stack]
		stack.pop()
		index_stack -= 1
		if len(stack) != 0:
			maxo = max(temp ^ stack[index_stack], maxo)
while len(stack) != 1:
	temp = stack[index_stack]
	stack.pop()
	index_stack -= 1
	maxo = max(temp ^ stack[index_stack], maxo)
print(maxo)


l = int(input())
arr = [int(i) for i in input().split()]
stack = [arr[0]]
maxa = 0
x = 0
for i in range(1, len(arr)):
	while len(stack) != 0 and arr[i] > stack[-1]:
		x = arr[i] ^ stack[-1]
		maxa = max(maxa, x)
		stack.pop()
	if len(stack) != 0:
		x = arr[i] ^ stack[-1]
		maxa = max(maxa, x)
	stack.append(arr[i])
print(maxa)


t = input()
a = list(map(int, input().split()))
m = 0
l = []
for i in range(0, len(a)):
	b = a[i]
	if len(l) == 0:
		l.append(b)
	elif l[-1] > b:
		if l[-1] ^ b > m:
			m = l[-1] ^ b
		l.append(b)
	else:
		while len(l) > 0 and l[-1] < b:
			if l[-1] ^ b > m:
				m = l[-1] ^ b
			del l[-1]
		l.append(b)
		if len(l) > 1:
			if l[-1] ^ l[-2] > m:
				m = l[-1] ^ l[-2]
print(m)


N = int(input())
W = list(map(int, input().split()))

def func(W):
	(st, ans) = ([W[0]], 0)
	for i in W[1:]:
		while len(st) and st[-1] < i:
			st.pop()
		ans = max(ans, st[-1] ^ i) if len(st) else ans
		st.append(i)
	return ans
print(max(func(W), func(W[::-1])))


from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return list(map(int, input().split()))

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def solve():
	n = ii()
	a = li()
	b = []
	for j in range(31, -1, -1):
		for i in range(n):
			if a[i] >> j & 1:
				b.append(i)
		if len(b) < n and len(b) > 0:
			break
		b = []
	if len(b) == n or len(b) == 0:
		print(0)
		return
	ans = 0
	x = 0
	for j in range(len(b) - 1):
		mx = 0
		x1 = a[b[j]]
		for i in range(b[j] - 1, x - 1, -1):
			mx = max(mx, a[i])
			ans = max(ans, x1 ^ mx)
		mx = 0
		for i in range(b[j] + 1, b[j + 1]):
			mx = max(mx, a[i])
			ans = max(ans, x1 ^ mx)
		x = b[j] + 1
	mx = 0
	x1 = a[b[len(b) - 1]]
	for i in range(b[len(b) - 1] - 1, x - 1, -1):
		mx = max(mx, a[i])
		ans = max(ans, x1 ^ mx)
	mx = 0
	for i in range(b[len(b) - 1] + 1, n):
		mx = max(mx, a[i])
		ans = max(ans, x1 ^ mx)
	print(ans)

def __starting_point():
	solve()
__starting_point()


n = int(input())
a = [int(k) for k in input().split()]
if len(a) == 1:
	print(a[0])
else:
	d = [a[0], a[1]]
	c = a[0] ^ a[1]
	for i in range(n):
		while len(d) > 0 and a[i] > d[-1]:
			d.pop()
		d.append(a[i])
		if len(d) > 1:
			c = max(c, d[-1] ^ d[-2])
	d = [a[-1], a[-2]]
	e = max(c, a[-1] ^ a[-2])
	for i in range(n - 3, -1, -1):
		while len(d) > 0 and a[i] > d[-1]:
			d.pop()
		d.append(a[i])
		if len(d) > 1:
			e = max(e, d[-1] ^ d[-2])
	print(e)


n = int(input())
a = list(map(int, input().split()))
l = []
s = 0
for i in range(n):
	while l != []:
		if a[i] >= l[-1]:
			s = max(s, a[i] ^ l[-1])
			l.pop()
		else:
			s = max(s, a[i] ^ l[-1])
			break
	l.append(a[i])
print(s)


n = int(input())
ls = list(map(int, input().split()))
st = list()
Max = 0
for i in range(n):
	while len(st) > 0:
		temp = st[len(st) - 1]
		Max = max(Max, temp ^ ls[i])
		if temp > ls[i]:
			break
		st.pop()
	st.append(ls[i])
print(Max)


n = int(input())
a = list(map(int, input().split()))
ans = 0
d = []
for i in a:
	while len(d) and d[-1] < i:
		ans = max(ans, d[-1] ^ i)
		d.pop()
	if len(d):
		ans = max(ans, d[-1] ^ i)
	d.append(i)
d = []
print(ans)


import sys, os
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
tc = 1
for test in range(1, tc + 1):
	n = int(input())
	a = list(map(int, input().split()))
	Prev = [i for i in range(n)]
	Next = [i for i in range(n)]
	stack = []
	for i in range(n):
		while stack and a[stack[-1]] <= a[i]:
			stack.pop()
		if stack:
			Prev[i] = stack[-1]
		stack.append(i)
	stack = []
	for i in range(n - 1, -1, -1):
		while stack and a[stack[-1]] <= a[i]:
			stack.pop()
		if stack:
			Next[i] = stack[-1]
		stack.append(i)
	ans = 0
	for i in range(n):
		ans = max(ans, a[i] ^ a[Prev[i]])
		ans = max(ans, a[i] ^ a[Next[i]])
	print(ans)


from sys import stdin
(lines, line_index) = (stdin.readlines(), -1)

def get_line():
	global lines, line_index
	line_index += 1
	return lines[line_index]

def main():
	n = int(get_line())
	seq = [int(x) for x in get_line().split()]
	stack = []
	ans = 0
	for val in seq:
		while stack:
			if stack[-1] < val:
				ans = max(ans, stack[-1] ^ val)
				stack.pop()
			else:
				ans = max(ans, stack[-1] ^ val)
				break
		stack.append(val)
	print(ans)
main()


n = int(input())
a = [int(n) for n in input().split()]
stack = []
maxx = 0
i = 0
while i < n:
	if not stack or a[i] < stack[-1]:
		stack.append(a[i])
		i += 1
	else:
		maxx = max(stack[-1] ^ a[i], maxx)
		temp = stack.pop()
		if stack:
			maxx = max(stack[-1] ^ temp, maxx)
while len(stack) != 1:
	temp = stack.pop()
	maxx = max(temp ^ stack[-1], maxx)
print(maxx)


n = int(input().strip())
l = list(map(int, input().strip().split(' ')))
stk = []
maxVal = 0
stk.append(l[0])
stk.append(l[1])
maxVal = stk[-1] ^ stk[-2]
for i in l[2:]:
	while len(stk) and stk[-1] < i:
		stk.pop()
	stk.append(i)
	if len(stk) >= 2:
		maxVal = max(maxVal, stk[-1] ^ stk[-2])
l = l[::-1]
stk = []
stk.append(l[0])
stk.append(l[1])
maxVal = max(maxVal, stk[-1] ^ stk[-2])
for i in l[2:]:
	while len(stk) and stk[-1] < i:
		stk.pop()
	stk.append(i)
	if len(stk) >= 2:
		maxVal = max(maxVal, stk[-1] ^ stk[-2])
print(maxVal)


from sys import stdin
n = int(stdin.readline())
s = list(map(int, stdin.readline().split()))
lucky = 0
stack = []
for i in s:
	while stack:
		lucky = max(lucky, stack[-1] ^ i)
		if i > stack[-1]:
			stack.pop()
		else:
			break
	stack.append(i)
print(lucky)


def maxxor(a, b, ans):
	t = a ^ b
	if t > ans:
		return t
	else:
		return ans
n = int(input().strip())
arr = list(map(int, input().strip().split()))
s = list()
s.append(arr[0])
s.append(arr[1])
ans = maxxor(arr[0], arr[1], 0)
index = 2
while index < n:
	if arr[index] > s[-1]:
		while arr[index] > s[-1]:
			t = s.pop()
			ans = maxxor(arr[index], t, ans)
			if len(s) == 0:
				break
	if len(s) != 0:
		ans = maxxor(arr[index], s[-1], ans)
	s.append(arr[index])
	index += 1
print(ans)


def solve(array):
	st = []
	res = array[0] ^ array[1]
	for i in range(n):
		while len(st) > 0 and array[i] >= st[-1]:
			st.pop()
		st.append(array[i])
		if len(st) >= 2:
			res = max(res, st[-1] ^ st[-2])
	return res
n = int(input())
array = list(map(int, input().split()))
res = solve(array)
res = max(res, solve(array[::-1]))
print(res)


n = int(input())
arr = [int(s) for s in input().split()]
stck = []
ans = 0
for i in arr:
	if not stck:
		stck.append(i)
	else:
		while stck and i > stck[len(stck) - 1]:
			ans = max(ans, i ^ stck[len(stck) - 1])
			stck.pop(len(stck) - 1)
		if stck:
			ans = max(ans, i ^ stck[len(stck) - 1])
		stck.append(i)
print(ans)


def max_ex(nums):
	maximum = float('-inf')
	stack = []
	for i in range(len(nums)):
		if not stack:
			stack.append(nums[i])
		else:
			while stack and stack[-1] < nums[i]:
				maximum = max(maximum, stack[-1] ^ nums[i])
				stack.pop()
			if stack:
				maximum = max(maximum, stack[-1] ^ nums[i])
			stack.append(nums[i])
	if stack:
		temp = stack.pop()
		while stack:
			maximum = max(maximum, temp ^ stack[-1])
			temp = stack.pop()
	return maximum
n = int(input())
arr = [int(i) for i in input().split()]
print(max_ex(arr))


import sys
n = int(input())
a = [int(x) for x in input().strip().split(' ')]
st = [a[0]]
max1 = -sys.maxsize
for i in range(1, n):
	while len(st) > 0:
		if a[i] < st[len(st) - 1]:
			max1 = max(max1, a[i] ^ st[len(st) - 1])
			break
		else:
			max1 = max(max1, a[i] ^ st[len(st) - 1])
			st.pop()
	st.append(a[i])
print(max1)