The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).

You are given a sequence $a$ consisting of $n$ integers.

You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).

For example, for the sequence $[1, 2, 4, 3, 2]$ the answer is $4$ (you take $1$ and the sequence becomes $[2, 4, 3, 2]$, then you take the rightmost element $2$ and the sequence becomes $[2, 4, 3]$, then you take $3$ and the sequence becomes $[2, 4]$ and then you take $4$ and the sequence becomes $[2]$, the obtained increasing sequence is $[1, 2, 3, 4]$).


-----Input-----

The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$.


-----Output-----

In the first line of the output print $k$ — the maximum number of elements in a strictly increasing sequence you can obtain.

In the second line print a string $s$ of length $k$, where the $j$-th character of this string $s_j$ should be 'L' if you take the leftmost element during the $j$-th move and 'R' otherwise. If there are multiple answers, you can print any.


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

Output
4
LRRR

Input
7
1 3 5 6 5 4 2

Output
6
LRLRRR

Input
3
2 2 2

Output
1
R

Input
4
1 2 4 3

Output
4
LLRR



-----Note-----

The first example is described in the problem statement.

Difficulty: MEDIUM_HARD

Solutions:
n = int(input())
a = [int(i) for i in input().split()]
(i, j, b, z, s, s1, s2, b1) = (0, n - 1, 0, 10 ** 6, '', '', '', 0)
while i <= j:
	if a[i] <= b and a[j] <= b:
		break
	if a[i] == z and a[j] == z:
		break
	if a[i] == a[j]:
		b1 = b
		while i <= n - 1:
			if a[i] > b1:
				b1 = a[i]
				s1 += 'L'
				i += 1
			else:
				break
		b1 = b
		while j >= 0:
			if a[j] > b1:
				b1 = a[j]
				s2 += 'R'
				j -= 1
			else:
				break
		s += s1 if len(s1) > len(s2) else s2
		break
	if a[i] > a[j]:
		if a[j] > b:
			b = a[j]
			j -= 1
			s += 'R'
		else:
			a[j] = z
	if a[j] > a[i]:
		if a[i] > b:
			b = a[i]
			i += 1
			s += 'L'
		else:
			a[i] = z
print(len(s))
print(s)


__version__ = '1.0'
__date__ = '2019-04-26'
import sys

def rec_solve(a, l, r, last):
	choices = []
	while l <= r:
		if a[l] == a[r] and a[l] > last:
			sub_ans_1 = rec_solve(a, l + 1, r, a[l])
			sub_ans_2 = rec_solve(a, l, r - 1, a[l])
			if len(sub_ans_1) > len(sub_ans_2):
				choices.append('L')
				choices += sub_ans_1
				return choices
			else:
				choices.append('R')
				choices += sub_ans_2
				return choices
		elif a[l] < a[r] and a[l] > last:
			last = a[l]
			choices.append('L')
			l += 1
		elif a[r] > last:
			last = a[r]
			choices.append('R')
			r -= 1
		elif a[l] > last:
			last = a[l]
			choices.append('L')
			l += 1
		else:
			return choices
	return choices

def solve(n, a):
	return rec_solve(a, 0, n - 1, 0)

def main(argv=None):
	n = int(input())
	a = list(map(int, input().split()))
	choice = solve(n, a)
	print(len(choice))
	print(''.join(choice))
	return 0

def __starting_point():
	STATUS = main()
	return STATUS
__starting_point()


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
LIST = [1] * n
LIST_INV = [1] * n
for i in range(n - 2, -1, -1):
	if A[i] < A[i + 1]:
		LIST[i] = LIST[i + 1] + 1
for i in range(1, n):
	if A[i] < A[i - 1]:
		LIST_INV[i] = LIST_INV[i - 1] + 1
ANS = []
SCORE = 0
i = 0
j = n - 1
while True:
	if i == j and A[i] > SCORE:
		ANS.append('R')
		SCORE = A[i]
		break
	if A[i] > A[j] and A[j] > SCORE:
		ANS.append('R')
		SCORE = A[j]
		j -= 1
	elif A[i] < A[j] and A[i] > SCORE:
		ANS.append('L')
		SCORE = A[i]
		i += 1
	elif A[i] > A[j] and A[j] <= SCORE and (A[i] > SCORE):
		ANS.append('L')
		SCORE = A[i]
		i += 1
	elif A[i] < A[j] and A[i] <= SCORE and (A[j] > SCORE):
		ANS.append('R')
		SCORE = A[j]
		j -= 1
	elif A[i] == A[j] and A[i] > SCORE:
		if LIST[i] > LIST_INV[j]:
			ANS.append('L')
			SCORE = A[i]
			i += 1
		else:
			ANS.append('R')
			SCORE = A[j]
			j -= 1
	else:
		break
print(len(ANS))
print(''.join(ANS))


def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) / 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
n = int(input().strip())
arr = list(map(int, input().split()))
seq = [0]
moves = []
i = 0
j = n - 1
while i != j:
	if arr[i] >= arr[j]:
		if arr[j] > seq[-1]:
			seq.append(arr[j])
			j -= 1
			moves.append('R')
		elif arr[i] > seq[-1]:
			seq.append(arr[i])
			i += 1
			moves.append('L')
		else:
			break
	elif arr[i] > seq[-1]:
		seq.append(arr[i])
		i += 1
		moves.append('L')
	elif arr[j] > seq[-1]:
		seq.append(arr[j])
		j -= 1
		moves.append('R')
	else:
		break
if i == j:
	if arr[i] > seq[-1]:
		seq.append(arr[i])
		moves.append('R')
seq2 = [0]
moves2 = []
i = 0
j = n - 1
while i != j:
	if arr[i] > arr[j]:
		if arr[j] > seq2[-1]:
			seq2.append(arr[j])
			j -= 1
			moves2.append('R')
		elif arr[i] > seq2[-1]:
			seq2.append(arr[i])
			i += 1
			moves2.append('L')
		else:
			break
	elif arr[i] > seq2[-1]:
		seq2.append(arr[i])
		i += 1
		moves2.append('L')
	elif arr[j] > seq2[-1]:
		seq2.append(arr[j])
		j -= 1
		moves2.append('R')
	else:
		break
if i == j:
	if arr[i] > seq2[-1]:
		seq2.append(arr[i])
		moves2.append('R')
if len(seq2) > len(seq):
	print(len(seq2) - 1)
	for i in moves2:
		print(i, end='')
else:
	print(len(seq) - 1)
	for i in moves:
		print(i, end='')


n = int(input())
arr = list(map(int, input().split()))
lb = 0
rb = n - 1
last = -1
ans = []

def prev(i):
	if i > 0:
		return arr[i - 1]
	else:
		return arr[0] - 1

def next(i):
	if i < n - 1:
		return arr[i + 1]
	else:
		return arr[n - 1] - 1
while lb <= rb:
	if arr[lb] < arr[rb]:
		last = arr[lb]
		ans.append('L')
		lb += 1
		if prev(lb) >= arr[lb]:
			while lb < rb and arr[rb] > next(rb):
				last = arr[rb]
				ans.append('R')
				rb -= 1
			break
	elif arr[rb] < arr[lb]:
		last = arr[rb]
		ans.append('R')
		rb -= 1
		if next(rb) >= arr[rb]:
			while lb < rb and arr[lb] > prev(lb):
				last = arr[lb]
				ans.append('L')
				lb += 1
			break
	else:
		break
if arr[lb] > last:
	if lb == rb:
		ans.append('R')
	if arr[lb] == arr[rb] and rb > lb:
		szleft = 0
		while arr[lb + szleft] < arr[lb + szleft + 1]:
			szleft += 1
		szright = 0
		while arr[rb - szright] < arr[rb - szright - 1]:
			szright += 1
		if szleft > szright:
			ans.extend(['L'] * (szleft + 1))
		else:
			ans.extend(['R'] * (szright + 1))
print(len(ans))
print(''.join(ans))


def count(b, dir, g):
	n = 0
	if dir == 'L':
		while g >= l and a[g] > b:
			b = a[g]
			g -= 1
			n += 1
	else:
		while g <= r and a[g] > b:
			b = a[g]
			g += 1
			n += 1
	return n
n = int(input())
a = [int(i) for i in input().split()]
b = 0
s = ''
l = 0
r = n - 1
n1 = 0
while l <= r and (a[l] > b or a[r] > b):
	if a[l] < a[r]:
		if a[l] > b:
			b = a[l]
			n1 += 1
			l += 1
			s += 'L'
		else:
			b = a[r]
			n1 += 1
			r -= 1
			s += 'R'
	elif a[r] < a[l]:
		if a[r] > b:
			b = a[r]
			n1 += 1
			r -= 1
			s += 'R'
		else:
			b = a[l]
			n1 += 1
			l += 1
			s += 'L'
	else:
		n2 = count(b, 'L', r)
		n3 = count(b, 'R', l)
		if n3 > n2:
			n1 += n3
			s += n3 * 'L'
			break
		else:
			n1 += n2
			s += n2 * 'R'
			break
print(n1)
print(s)


n = int(input())
a = list(map(int, input().split()))
s = ''
x = 0
l = 0
r = n - 1
while l <= r and (a[l] > x or a[r] > x):
	if a[l] != a[r]:
		if a[l] > x and a[l] < a[r]:
			x = a[l]
			l += 1
			s += 'L'
		if a[r] > x and a[r] < a[l]:
			x = a[r]
			r -= 1
			s += 'R'
		if a[r] > x and a[l] <= x:
			x = a[r]
			r -= 1
			s += 'R'
		if a[r] <= x and a[l] > x:
			x = a[l]
			l += 1
			s += 'L'
	else:
		i = l
		j = r
		y = 1
		z = 1
		if l != r:
			while a[i] < a[i + 1]:
				y += 1
				i += 1
			while a[j] < a[j - 1]:
				z += 1
				j -= 1
			if y > z:
				s += 'L' * y
				break
			else:
				s += 'R' * z
				break
		elif a[r] > x:
			s += 'R'
			r -= 1
print(len(s))
print(s)


n = int(input())
l = list(map(int, input().split()))
import sys
lewy = 0
prawy = n - 1
wynik = 0
seq = 0
le = 0
pra = 0
wynile = 0
wynipra = 0
kol = []
mini = -1000000000000
while True:
	if lewy > prawy:
		break
	if l[lewy] != l[prawy]:
		if l[lewy] > l[prawy]:
			if l[prawy] > mini:
				mini = l[prawy]
				prawy -= 1
				wynik += 1
				kol.append('R')
			elif l[lewy] > mini:
				mini = l[lewy]
				lewy += 1
				wynik += 1
				kol.append('L')
			else:
				print(wynik)
				for i in kol:
					print(i, end='')
				sys.exit(0)
		elif l[lewy] > mini:
			mini = l[lewy]
			lewy += 1
			wynik += 1
			kol.append('L')
		elif l[prawy] > mini:
			mini = l[prawy]
			prawy -= 1
			wynik += 1
			kol.append('R')
		else:
			print(wynik)
			for i in kol:
				print(i, end='')
			sys.exit(0)
	elif l[lewy] < mini:
		print(wynik)
		for i in kol:
			print(i, end='')
		sys.exit(0)
	else:
		miniclone = mini
		seq1 = 0
		le = 0
		while True:
			if lewy + le <= prawy and l[lewy + le] > mini:
				mini = l[lewy + le]
				seq1 += 1
				le += 1
			else:
				break
		wynile = wynik + seq1
		seq2 = 0
		le = 0
		mini = miniclone
		while True:
			if prawy - le >= lewy and l[prawy - le] > mini:
				mini = l[prawy - le]
				seq2 += 1
				le += 1
			else:
				break
		wynipra = wynik + seq2
		if seq1 > seq2:
			kol = kol + ['L'] * seq1
		else:
			kol = kol + ['R'] * seq2
		print(max(wynile, wynipra))
		for i in kol:
			print(i, end='')
		sys.exit(0)


n = int(input())
a = [int(item) for item in input().split()]
s = []
i = 0
j = n - 1
ans = []
while i <= j:
	if a[i] < a[j]:
		if not s or s[-1] < a[i]:
			s.append(a[i])
			ans.append('L')
			i += 1
		elif not s or s[-1] < a[j]:
			s.append(a[j])
			ans.append('R')
			j -= 1
		else:
			break
	elif a[i] > a[j]:
		if not s or s[-1] < a[j]:
			ans.append('R')
			s.append(a[j])
			j -= 1
		elif not s or s[-1] < a[i]:
			s.append(a[i])
			ans.append('L')
			i += 1
		else:
			break
	else:
		p1 = 0
		p2 = 0
		cur_last = s[-1] if s else None
		or_i = i
		while i <= j and (cur_last is None or a[i] > cur_last):
			cur_last = a[i]
			i += 1
			p1 += 1
		cur_last = s[-1] if s else None
		while or_i <= j and (cur_last is None or a[j] > cur_last):
			cur_last = a[j]
			j -= 1
			p2 += 1
		if p1 > p2:
			ans += list('L' * p1)
		else:
			ans += list('R' * p2)
		break
print(len(ans))
print(''.join((str(x) for x in ans)))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from collections import deque
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
else:
	_str = str
	str = lambda x=b'': x if type(x) is bytes else _str(x).encode()

def main():
	n = int(readline())
	a = deque(readlist())
	(res, seq) = ([], [0])
	while a:
		if len(a) == 1 and a[0] > seq[-1]:
			seq.append(a.pop())
			res.append('R')
		elif a[0] < a[-1] and a[0] > seq[-1]:
			seq.append(a.popleft())
			res.append('L')
		elif a[-1] < a[0] and a[-1] > seq[-1]:
			seq.append(a.pop())
			res.append('R')
		elif a[0] == a[-1] and a[0] > seq[-1]:
			(case_1, case_2) = (1, 1)
			start = a[0]
			i = 1
			while i < len(a) and a[i] > start:
				start = a[i]
				i += 1
				case_1 += 1
			start = a[-1]
			i = len(a) - 2
			while i >= 0 and a[i] > start:
				start = a[i]
				i -= 1
				case_2 += 1
			res += ['L'] * case_1 if case_1 > case_2 else ['R'] * case_2
			break
		elif a[0] > seq[-1]:
			seq.append(a.popleft())
			res.append('L')
		elif a[-1] > seq[-1]:
			seq.append(a.pop())
			res.append('R')
		else:
			break
	print(len(res))
	print(''.join(res))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._buffer = BytesIO()
		self._fd = file.fileno()
		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):
		if self._buffer.tell():
			return self._buffer.read()
		return os.read(self._fd, os.fstat(self._fd).st_size)

	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 ostream:

	def __lshift__(self, a):
		if a is endl:
			sys.stdout.write(b'\n')
			sys.stdout.flush()
		else:
			sys.stdout.write(str(a))
		return self

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', b' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', b'\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
(cout, endl) = (ostream(), object())
readline = sys.stdin.readline
readlist = lambda var=int: [var(n) for n in readline().split()]
input = lambda : readline().rstrip(b'\r\n')
main()


n = int(input())
nums = list(map(int, input().split()))

def foo(l, r, prev=0):
	ans = ''
	while l <= r:
		if nums[l] <= prev and nums[r] <= prev:
			break
		if prev < nums[l] < nums[r]:
			ans += 'L'
			prev = nums[l]
			l += 1
		elif prev < nums[r] < nums[l]:
			ans += 'R'
			prev = nums[r]
			r -= 1
		elif nums[r] > nums[l]:
			ans += 'R'
			prev = nums[r]
			r -= 1
		elif nums[r] < nums[l]:
			ans += 'L'
			prev = nums[l]
			l += 1
		elif l == r:
			ans += 'R'
			break
		else:
			a1 = 'L' + foo(l + 1, r, nums[l])
			a2 = 'R' + foo(l, r - 1, nums[l])
			ans += a1 if len(a1) > len(a2) else a2
			break
	return ans
ans = foo(0, n - 1)
print(len(ans))
print(ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 10 ** 9 + 7
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
ans = []
l = 0
r = n - 1
last = -1
while l <= r:
	if a[l] > last and a[r] > last:
		if a[l] < a[r]:
			ans.append('L')
			last = a[l]
			l += 1
		elif a[r] < a[l]:
			ans.append('R')
			last = a[r]
			r -= 1
		else:
			break
	elif a[l] > last:
		last = a[l]
		ans.append('L')
		l += 1
	elif a[r] > last:
		last = a[r]
		ans.append('R')
		r -= 1
	else:
		break
ol = last
Llen = 0
i = l
while i <= r and a[i] > last:
	Llen += 1
	last = a[i]
	i += 1
Rlen = 0
last = ol
i = r
while i >= l and a[i] > last:
	Rlen += 1
	last = a[i]
	i -= 1
if Rlen >= Llen:
	temp = ['R'] * Rlen
else:
	temp = ['L'] * Llen
print(len(ans) + len(temp))
print(*ans, *temp, sep='')


n = int(input())
a = [int(x) for x in input().split()]
start = 0
end = n - 1
pos = 0
s = []
while start <= end:
	if a[start] == a[end]:
		if a[start] > pos:
			counter1 = counter2 = 1
			for i in range(start + 1, end + 1):
				if a[i] > a[i - 1]:
					counter1 += 1
				else:
					break
				i += 1
			for i in range(end - 1, start - 1, -1):
				if a[i] > a[i + 1]:
					counter2 += 1
				else:
					break
				i -= 1
			if counter1 > counter2:
				for i in range(counter1):
					s.append('L')
			else:
				for i in range(counter2):
					s.append('R')
		break
	elif a[start] < a[end] and a[start] > pos:
		s.append('L')
		pos = a[start]
		start += 1
	elif a[end] > pos:
		s.append('R')
		pos = a[end]
		end -= 1
	elif a[start] > pos:
		s.append('L')
		pos = a[start]
		start += 1
	else:
		break
print(len(s))
print(*s, sep='')


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def __next__(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(next(self))

	def solve(self):
		n = self.next_int()
		x = [self.next_int() for _ in range(0, n)]
		d = 0
		l = 0
		r = n - 1
		rs = []
		while l <= r:
			if x[l] <= d and x[r] <= d:
				break
			if x[l] <= d:
				rs.append('R')
				d = x[r]
				r -= 1
			elif x[r] <= d:
				rs.append('L')
				d = x[l]
				l += 1
			elif x[r] < x[l] or l == r:
				rs.append('R')
				d = x[r]
				r -= 1
			elif x[l] < x[r]:
				rs.append('L')
				d = x[l]
				l += 1
			else:
				ll = l + 1
				while x[ll] > x[ll - 1]:
					ll += 1
				rr = r - 1
				while x[rr] > x[rr + 1]:
					rr -= 1
				if ll - l > r - rr:
					rs.append('L')
					d = x[l]
					l += 1
				else:
					rs.append('R')
					d = x[r]
					r -= 1
		print(len(rs))
		print(''.join(rs))

def __starting_point():
	Main().solve()
__starting_point()


n = int(input())
ai = list(map(int, input().split()))
ans = [0]
p = 0
i = 0
j = n - 1
anss = []
flag = 0
while i <= j:
	if ai[i] < ai[j]:
		if ai[i] > ans[-1]:
			ans.append(ai[i])
			anss.append('L')
			i += 1
		elif ai[j] > ans[-1]:
			ans.append(ai[j])
			anss.append('R')
			j -= 1
		else:
			break
	elif ai[i] > ai[j]:
		if ai[j] > ans[-1]:
			ans.append(ai[j])
			anss.append('R')
			j -= 1
		elif ai[i] > ans[-1]:
			ans.append(ai[i])
			anss.append('L')
			i += 1
		else:
			break
	else:
		p = i
		flag = 1
		break
if flag == 1:
	left = [ans[-1]]
	leftt = []
	right = [ans[-1]]
	rightt = []
	while i <= j:
		if ai[i] > left[-1]:
			left.append(ai[i])
			leftt.append('L')
			i += 1
		else:
			break
	i = p
	while j >= i:
		if ai[j] > right[-1]:
			right.append(ai[j])
			rightt.append('R')
			j -= 1
		else:
			break
	if len(left) > len(right):
		anss += leftt
	else:
		anss += rightt
print(len(anss))
print(''.join(anss))


n = int(input())
a = list(map(int, input().split()))
ans = ''
l = 0
r = n - 1
last = -1
while l <= r:
	if min(a[l], a[r]) <= last:
		break
	if a[l] < a[r]:
		ans += 'L'
		last = a[l]
		l += 1
	elif a[l] > a[r]:
		ans += 'R'
		last = a[r]
		r -= 1
	else:
		break
last1 = last
ans1 = ans
l1 = l
while l1 <= r and a[l1] > last1:
	ans1 += 'L'
	last1 = a[l1]
	l1 += 1
last2 = last
ans2 = ans
r2 = r
while r2 >= l and a[r2] > last2:
	ans2 = ans2 + 'R'
	last2 = a[r2]
	r2 -= 1
if len(ans1) > len(ans2):
	print(len(ans1))
	print(ans1)
else:
	print(len(ans2))
	print(ans2)


l = 'L'
r = 'R'
n = int(input())
a = list(map(int, input().split()))
i = 0
j = n - 1
ln = 0
curr = -1
st = []
while i <= j:
	if a[i] < a[j]:
		if curr < a[i]:
			st.append(l)
			curr = a[i]
			i += 1
		elif curr < a[j]:
			st.append(r)
			curr = a[j]
			j -= 1
		else:
			break
	elif a[j] < a[i]:
		if curr < a[j]:
			st.append(r)
			curr = a[j]
			j -= 1
		elif curr < a[i]:
			st.append(l)
			curr = a[i]
			i += 1
		else:
			break
	else:
		if curr > a[j]:
			break
		ll = 1
		i_ = i + 1
		while i_ < j and a[i_] > a[i_ - 1]:
			ll += 1
			i_ += 1
		lr = 1
		j_ = j - 1
		while i < j_ and a[j_ + 1] < a[j_]:
			lr += 1
			j_ -= 1
		if ll > lr:
			for ii in range(ll):
				st.append(l)
		else:
			for ii in range(lr):
				st.append(r)
		break
print(len(st))
print(''.join(st))


from collections import deque
n = int(input())
a = deque([int(x) for x in input().split()])
cur = -1
ans = ''
while cur < a[0] or cur < a[-1]:
	if a[0] < a[-1]:
		if cur < a[0]:
			ans += 'L'
			cur = a[0]
			a.popleft()
		else:
			ans += 'R'
			cur = a[-1]
			a.pop()
	elif a[0] > a[-1]:
		if cur < a[-1]:
			ans += 'R'
			cur = a[-1]
			a.pop()
		else:
			ans += 'L'
			cur = a[0]
			a.popleft()
	elif len(a) == 1:
		ans += 'R'
		cur = a[-1]
		a.pop()
	elif a[0] == a[-1]:
		cur1 = cur
		cur2 = cur
		b = a.copy()
		c = a.copy()
		count1 = 0
		count2 = 0
		while cur1 < b[0]:
			count1 += 1
			cur1 = b[0]
			b.popleft()
		while cur2 < c[-1]:
			count2 += 1
			cur2 = c[-1]
			c.pop()
		if count1 > count2:
			for i in range(count1):
				ans += 'L'
			break
		else:
			for i in range(count2):
				ans += 'R'
			break
	if len(a) == 0:
		break
print(len(ans))
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
last = -1
i = 0
j = n - 1
ans = ''
while i <= j:
	if max(a[i], a[j]) <= last:
		break
	if a[i] == a[j]:
		start = i
		k = 0
		last = a[i]
		i += 1
		while i <= j:
			if a[i] <= last:
				break
			else:
				last = a[i]
				k += 1
				i += 1
		last = a[j]
		k2 = 0
		i = start
		j -= 1
		while i <= j:
			if a[j] <= last:
				break
			else:
				last = a[j]
				k2 += 1
				j -= 1
		if k > k2:
			ans += 'L' * (k + 1)
		else:
			ans += 'R' * (k2 + 1)
		break
	if a[i] < a[j]:
		if a[i] > last:
			ans += 'L'
			last = a[i]
			i += 1
		else:
			ans += 'R'
			last = a[j]
			j -= 1
	elif a[j] > last:
		ans += 'R'
		last = a[j]
		j -= 1
	else:
		ans += 'L'
		last = a[i]
		i += 1
print(len(ans))
print(ans)


n = int(input())
a = list(map(int, input().split()))
sp = [0]
s = ''
l = 0
r = n - 1
while r - l != -1:
	if a[l] == a[r] and l != r:
		sp1 = sp.copy()
		s1 = s
		while sp[-1] < a[l]:
			sp.append(a[l])
			l += 1
			s += 'L'
		while sp1[-1] < a[r]:
			sp1.append(a[r])
			r -= 1
			s1 += 'R'
		if len(sp) > len(sp1):
			print(len(sp) - 1)
			print(s)
		else:
			print(len(sp1) - 1)
			print(s1)
		exit(0)
	if a[l] > sp[-1] and a[r] <= sp[-1]:
		sp.append(a[l])
		l += 1
		s += 'L'
	elif a[r] > sp[-1] and a[l] <= sp[-1]:
		sp.append(a[r])
		r -= 1
		s += 'R'
	elif a[l] > sp[-1] and a[r] > sp[-1]:
		if a[l] < a[r]:
			sp.append(a[l])
			l += 1
			s += 'L'
		else:
			sp.append(a[r])
			r -= 1
			s += 'R'
	else:
		break
print(len(sp) - 1)
print(s)


from collections import deque
n = int(input())
A = list(map(int, input().split()))
A = deque(A)
ans = ''
last = 0
while len(A) and (A[0] > last or A[-1] > last):
	if len(A) == 1:
		ans += 'R'
		break
	if A[0] > last and A[-1] > last:
		if A[0] < A[-1]:
			last = A[0]
			A.popleft()
			ans += 'L'
		elif A[-1] < A[0]:
			last = A[-1]
			A.pop()
			ans += 'R'
		else:
			lal = last
			cnt1 = -1
			for i in A:
				cnt1 += 1
				if i > lal:
					lal = i
				else:
					break
			lol = last
			cnt2 = -1
			for i in range(len(A) - 1, -1, -1):
				cnt2 += 1
				if A[i] > lol:
					lol = A[i]
				else:
					break
			if cnt1 > cnt2:
				ans += 'L' * cnt1
			else:
				ans += 'R' * cnt2
			break
	elif A[0] > last:
		last = A[0]
		A.popleft()
		ans += 'L'
	else:
		last = A[-1]
		A.pop()
		ans += 'R'
print(len(ans))
print(ans)


n = int(input())
ans = []
a = list(map(int, input().split()))
l = -0
r = n - 1
now = 0
while l <= r:
	if a[l] < a[r] and a[l] > now:
		now = a[l]
		l += 1
		ans.append('L')
	elif a[r] < a[l] and a[r] > now:
		now = a[r]
		r -= 1
		ans.append('R')
	elif a[r] < a[l] and a[l] > now:
		now = a[l]
		l += 1
		ans.append('L')
	elif a[l] < a[r] and a[r] > now:
		now = a[r]
		r -= 1
		ans.append('R')
	elif a[l] == a[r]:
		if a[l] <= now:
			break
		nowl = now
		nowr = now
		pos = l
		cnt1 = 0
		cnt2 = 0
		while pos <= r and a[pos] > nowl:
			nowl = a[pos]
			pos += 1
			cnt1 += 1
		pos = r
		while l <= pos and a[pos] > nowr:
			nowr = a[pos]
			pos -= 1
			cnt2 += 1
		if cnt1 > cnt2:
			for i in range(0, cnt1):
				ans.append('L')
		else:
			for i in range(0, cnt2):
				ans.append('R')
		break
	else:
		break
print(len(ans))
for i in ans:
	print(i, end='')


def check_l(a, l, r):
	d = 0
	last = 0
	while l <= r and a[l] > last:
		last = a[l]
		l += 1
		d += 1
	return d

def check_r(a, l, r):
	d = 0
	last = 0
	while l <= r and a[r] > last:
		last = a[r]
		r -= 1
		d += 1
	return d

def main():
	input()
	a = [int(x) for x in input().split(' ')]
	n = len(a)
	(l, r) = (0, n - 1)
	res = []
	last = 0
	picked = True
	while l <= r and picked:
		picked = False
		if last < a[l] and last < a[r]:
			if a[l] < a[r]:
				last = a[l]
				res.append('L')
				l += 1
			elif a[l] == a[r]:
				dl = check_l(a, l, r)
				dr = check_r(a, l, r)
				if dl > dr:
					res.extend(['L'] * dl)
					last = a[l + dl - 1]
					l += dl
				else:
					res.extend(['R'] * dr)
					last = a[r - dr + 1]
					r -= dr
			else:
				last = a[r]
				res.append('R')
				r -= 1
			picked = True
			continue
		if last < a[l]:
			last = a[l]
			res.append('L')
			l += 1
			picked = True
			continue
		if last < a[r]:
			last = a[r]
			res.append('R')
			r -= 1
			picked = True
			continue
	print(len(res))
	print(''.join(res))
main()


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	last = 0
	s = ''
	(l, r) = (0, n - 1)
	while True:
		if last > max(arr[l], arr[r]) or l > r:
			break
		elif arr[r] < arr[l] and arr[r] > last or arr[r] > last > arr[l]:
			last = arr[r]
			r -= 1
			s += 'R'
		elif arr[l] < arr[r] and arr[l] > last or arr[l] > last > arr[r]:
			last = arr[l]
			l += 1
			s += 'L'
		else:
			(lt, rt) = (0, 0)
			if arr[l] > last:
				lt = 1
				while True:
					if l + lt > r:
						break
					if arr[l + lt] > arr[l + lt - 1]:
						pass
					else:
						break
					lt += 1
			if arr[r] > last:
				rt = 1
				while True:
					if r - rt < l:
						break
					if arr[r - rt] > arr[r - rt + 1]:
						pass
					else:
						break
					rt += 1
			if lt > rt:
				s += 'L' * lt
			else:
				s += 'R' * rt
			break
	print(len(s))
	print(s)
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
a = [int(x) for x in input().split()]
m = 0
sol = ''
while len(a) > 0:
	if a[0] > m and a[-1] > m and (a[0] != a[-1]):
		if a[0] < a[-1]:
			sol += 'L'
			m = a[0]
			a.pop(0)
		else:
			sol += 'R'
			m = a[-1]
			a.pop(-1)
	elif a[0] > m and a[-1] > m and (a[0] == a[-1]):
		k1 = 1
		k2 = 1
		s2 = sol + 'L'
		s3 = sol + 'R'
		for i in range(1, len(a)):
			if a[i] > a[i - 1]:
				s2 += 'L'
			else:
				break
		i = len(a) - 2
		while i >= 0:
			if a[i] > a[i + 1]:
				s3 += 'R'
			else:
				break
			i -= 1
		if len(s2) > len(s3):
			print(len(s2))
			print(s2)
			exit()
		else:
			print(len(s3))
			print(s3)
			exit()
	elif a[0] > m:
		sol += 'L'
		m = a[0]
		a.pop(0)
	elif a[-1] > m:
		sol += 'R'
		m = a[-1]
		a.pop(-1)
	else:
		break
print(len(sol))
print(sol)


from collections import deque
n = int(input())
A = deque(map(int, input().split()))
ans = ''
last = 0
i = 0
while i < n and max(A[0], A[-1]) > last:
	if A[0] == A[-1] and A[0] > last:
		(left, right) = (A[0], A[-1])
		size = len(A)
		ansLeft = 'L'
		for i in range(1, size):
			if A[i] > left:
				ansLeft += 'L'
				left = A[i]
			else:
				break
		ansRight = 'R'
		for i in reversed(range(size - 1)):
			if A[i] > right:
				ansRight += 'R'
				right = A[i]
			else:
				break
		ans += ansRight if len(ansRight) >= len(ansLeft) else ansLeft
		break
	if A[0] > last and (A[0] < A[-1] or A[-1] <= last):
		ans += 'L'
		last = A.popleft()
	else:
		ans += 'R'
		last = A.pop()
	i += 1
print(len(ans))
print(ans)


import sys
sys.setrecursionlimit(10 ** 7)
N = int(input())
number = list(map(int, input().split()))
seq = []
ans = []
l = 0
r = N - 1

def correct(ans, l, r, action):
	for i in range(len(ans) - 1, -1, -1):
		if not ans[i] == 'X':
			break
		ans[i] = action
		if action == 'L':
			r += 1
		else:
			l -= 1
	return (l, r)

def run(number, l, r, seq):
	ans = []
	while l <= r:
		n1 = number[l]
		n2 = number[r]
		if n1 == n2:
			if seq and n1 <= seq[-1]:
				break
			steps = 0
			last = -1
			if seq:
				last = seq[-1]
			while l + steps <= r - steps and number[l + steps] == number[r - steps] and (number[l + steps] > last):
				seq.append(number[l + steps])
				ans.append('X')
				last = number[l + steps]
				steps += 1
			s1 = run(number, l + steps, r, [seq[-1]])
			s2 = run(number, l, r - steps, [seq[-1]])
			if len(s1) > len(s2):
				return ''.join(ans).replace('X', 'L') + s1
			else:
				return ''.join(ans).replace('X', 'R') + s2
		elif not seq:
			if n1 < n2:
				seq.append(n1)
				ans.append('L')
				l += 1
			else:
				seq.append(n2)
				ans.append('R')
				r -= 1
		else:
			last = seq[-1]
			if last < n1 < n2 or n2 <= last < n1:
				(l, r) = correct(ans, l, r, 'L')
				seq.append(n1)
				ans.append('L')
				l += 1
			elif last < n2 < n1 or n1 <= last < n2:
				(l, r) = correct(ans, l, r, 'R')
				seq.append(n2)
				ans.append('R')
				r -= 1
			else:
				break
	return ''.join(ans)
ans = run(number, 0, N - 1, [])
print(len(ans))
print(ans)


def __starting_point():
	n = int(input())
	nums = list(map(int, input().split()))
	result = []
	last = -1
	left = 0
	right = len(nums) - 1
	while left <= right:
		if nums[left] < nums[right]:
			if nums[left] > last:
				last = nums[left]
				left += 1
				result.append('L')
			elif nums[right] > last:
				last = nums[right]
				right -= 1
				result.append('R')
			else:
				break
		elif nums[left] > nums[right]:
			if nums[right] > last:
				last = nums[right]
				right -= 1
				result.append('R')
			elif nums[left] > last:
				last = nums[left]
				left += 1
				result.append('L')
			else:
				break
		elif nums[left] == nums[right]:
			l_n = left
			r_n = right
			l_c = 0
			r_c = 0
			l_last = last
			r_last = last
			while l_n <= right:
				if nums[l_n] > l_last:
					l_c += 1
					l_last = nums[l_n]
					l_n += 1
				else:
					break
			while r_n >= left:
				if nums[r_n] > r_last:
					r_c += 1
					r_last = nums[r_n]
					r_n -= 1
				else:
					break
			if l_c > r_c:
				for i in range(l_c):
					result.append('L')
			else:
				for i in range(r_c):
					result.append('R')
			break
	print(len(result))
	print(''.join(result))
__starting_point()


def main():
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = n - 1
	last = -1
	anz = []
	stop = False
	while i != j:
		if a[i] == a[j]:
			stop = True
			break
		if a[i] < a[j]:
			if a[i] > last:
				last = a[i]
				i += 1
				anz.append('L')
			elif a[j] > last:
				last = a[j]
				j -= 1
				anz.append('R')
			else:
				break
		elif a[j] > last:
			last = a[j]
			j -= 1
			anz.append('R')
		elif a[i] > last:
			last = a[i]
			i += 1
			anz.append('L')
		else:
			break
	if i == j and a[i] > last:
		anz.append('R')
	if stop:
		l = []
		r = []
		i1 = i
		last1 = last
		while last1 < a[i] and i != j:
			last1 = a[i]
			i += 1
			l.append('L')
		while last < a[j] and i1 != j:
			last = a[j]
			j -= 1
			r.append('R')
		if len(l) > len(r):
			print(len(anz) + len(l))
			for elem in anz:
				print(elem, end='')
			for elem in l:
				print(elem, end='')
		else:
			print(len(r) + len(anz))
			for elem in anz:
				print(elem, end='')
			for elem in r:
				print(elem, end='')
	else:
		print(len(anz))
		for elem in anz:
			print(elem, end='')
main()


n = int(input())
M = list(map(int, input().split()))
L = [1] * n
R = [1] * n
for i in range(1, len(M)):
	if M[i] < M[i - 1]:
		R[i] = R[i - 1] + 1
	if M[n - 1 - i] < M[n - i]:
		L[n - 1 - i] = L[n - i] + 1
ans = []
last = -1
(l, r) = (0, n - 1)
while r >= l:
	if M[r] <= last and M[l] <= last:
		break
	if M[r] == M[l]:
		if L[l] > R[r]:
			ans += ['L']
			last = M[l]
			l += 1
		else:
			ans += ['R']
			last = M[r]
			r -= 1
	elif M[r] <= last:
		last = M[l]
		ans += ['L']
		l += 1
	elif M[l] <= last:
		last = M[r]
		ans += ['R']
		r -= 1
	elif M[l] < M[r]:
		last = M[l]
		ans += ['L']
		l += 1
	else:
		last = M[r]
		ans += ['R']
		r -= 1
print(len(ans))
print(''.join(ans))


N = int(input())
a_list = list(map(int, input().split()))
from collections import deque
import bisect
deq = deque(a_list)
ans = ''
left = 0
right = 0
min_num = -1
cnt = 0
f = False
lr = False
while len(deq) >= 2:
	left = deq.popleft()
	right = deq.pop()
	max_num = max(left, right)
	if max_num > min_num:
		if left == right:
			deq.appendleft(left)
			deq.append(right)
			lr = True
			break
		cnt += 1
		if right > left > min_num:
			ans += 'L'
			deq.append(right)
			min_num = left
		elif left > right > min_num:
			ans += 'R'
			deq.appendleft(left)
			min_num = right
		elif left > min_num:
			ans += 'L'
			deq.append(right)
			min_num = left
		else:
			ans += 'R'
			deq.appendleft(left)
			min_num = right
	else:
		f = True
		break
if lr:
	l = 0
	r = 0
	tmp = deq.copy()
	prv = -1
	for t in tmp:
		if t > prv:
			l += 1
		else:
			break
		prv = t
	tmp = list(reversed(tmp))
	prv = -1
	for t in tmp:
		if t > prv:
			r += 1
		else:
			break
		prv = t
	if l > r:
		print(cnt + l)
		print(ans + 'L' * l)
	else:
		print(cnt + r)
		print(ans + 'R' * r)
	return
if f:
	print(cnt)
	print(ans)
else:
	tmp = deq.pop()
	if tmp > min_num:
		cnt += 1
		print(cnt)
		print(ans + 'R')
	else:
		print(cnt)
		print(ans)


n = int(input())
a = [int(i) for i in input().split()]
moves = ''
last = 0
left = 0
right = n - 1
while left <= right and max(a[left], a[right]) > last:
	if a[left] == a[right] and left != right:
		l = left + 1
		r = right - 1
		while a[l] > a[l - 1]:
			l += 1
		while a[r] > a[r + 1]:
			r -= 1
		if l - left > right - r:
			last = a[left]
			moves += 'L'
			left += 1
		else:
			last = a[right]
			moves += 'R'
			right -= 1
	elif a[left] > a[right]:
		if a[right] > last:
			last = a[right]
			moves += 'R'
			right -= 1
		else:
			last = a[left]
			moves += 'L'
			left += 1
	elif a[right] > a[left]:
		if a[left] > last:
			last = a[left]
			moves += 'L'
			left += 1
		else:
			last = a[right]
			moves += 'R'
			right -= 1
	elif a[left] == a[right] and left == right:
		last = a[right]
		moves += 'R'
		right -= 1
	else:
		break
print(len(moves))
print(moves)


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 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 = []
	l = 0
	r = n - 1
	x = 0
	while l <= r:
		if a[l] <= x and a[r] <= x:
			break
		if a[l] > x and a[r] > x:
			if a[l] < a[r]:
				b.append('L')
				x = a[l]
				l += 1
			elif l != r and a[l] == a[r]:
				(a1, b1) = (0, 0)
				x1 = x
				for i in range(l, r):
					if a[i] > x1:
						x1 = a[i]
						a1 += 1
					else:
						break
				x2 = x
				for i in range(r, l, -1):
					if a[i] > x2:
						x2 = a[i]
						b1 += 1
					else:
						break
				if a1 > b1:
					b += ['L'] * a1
				else:
					b += ['R'] * b1
				break
			else:
				x = a[r]
				b.append('R')
				r -= 1
		elif a[l] > x:
			x = a[l]
			b.append('L')
			l += 1
		elif a[r] > x:
			x = a[r]
			b.append('R')
			r -= 1
		else:
			break
	print(len(b))
	print(''.join(b))
solve()


n = int(input())
arr = [int(x) for x in input().split()]
ans = ''
i = 0
j = n - 1
last = 0
while i <= j:
	if arr[i] > last and arr[j] > last:
		if arr[i] > arr[j]:
			ans += 'R'
			last = arr[j]
			j -= 1
		elif arr[j] > arr[i]:
			ans += 'L'
			last = arr[i]
			i += 1
		else:
			a1 = 1
			a2 = 1
			while i + a1 <= j and arr[i + a1] > arr[i + a1 - 1]:
				a1 += 1
			while j - a2 >= i and arr[j - a2 + 1] < arr[j - a2]:
				a2 += 1
			if a1 > a2:
				ans += 'L' * a1
				break
			else:
				ans += 'R' * a2
				break
	elif arr[i] > last:
		ans += 'L'
		last = arr[i]
		i += 1
	elif arr[j] > last:
		ans += 'R'
		last = arr[j]
		j -= 1
	else:
		break
print(len(ans))
print(ans)


from collections import deque
n = int(input())
a = deque((int(x) for x in input().split()))
ans = []
temp = 0
while a:
	if a[0] > temp:
		if a[-1] > temp and a[-1] < a[0]:
			ans.append('R')
			temp = a[-1]
			a.pop()
		elif a[-1] == a[0]:
			le = a.copy()
			temp2 = le[0]
			le.popleft()
			while le and temp2 < le[0]:
				temp2 = le[0]
				le.popleft()
			ri = a.copy()
			temp3 = ri[0]
			ri.pop()
			while ri and temp3 < ri[-1]:
				temp3 = ri[-1]
				ri.pop()
			if len(a) - len(le) > len(a) - len(ri):
				ans.append('L')
				temp = a[0]
				a.popleft()
			else:
				ans.append('R')
				temp = a[-1]
				a.pop()
		else:
			ans.append('L')
			temp = a[0]
			a.popleft()
	elif a[-1] > temp:
		ans.append('R')
		temp = a[-1]
		a.pop()
	else:
		break
print(len(ans))
print(''.join(ans))


n = int(input())
lst = list(map(int, input().split()))
(mn, i, j, k, res, yes) = (3000000, 0, n - 1, -1, '', 0)
while mn != -1:
	(x, y) = (lst[i], lst[j])
	if max(x, y) <= k:
		mn = -1
		continue
	elif i == j:
		res += 'R'
		break
	else:
		if x == y:
			yes = 1
			break
		if x > y:
			if y > k:
				res += 'R'
				k = y
				j -= 1
			else:
				res += 'L'
				k = x
				i += 1
		if y > x:
			if x > k:
				res += 'L'
				k = x
				i += 1
			else:
				res += 'R'
				k = y
				j -= 1
if yes == 1:
	schet1 = 0
	for e in range(i + 1, j):
		if lst[e] <= lst[e - 1]:
			break
		schet1 += 1
	schet2 = 0
	for e in range(j - 1, i, -1):
		if lst[e] <= lst[e + 1]:
			break
		schet2 += 1
	if schet1 > schet2:
		res += 'L' * (schet1 + 1)
	else:
		res += 'R' * (schet2 + 1)
print(len(res))
print(res)


n = input()
data = input()
data = data.split(' ')

def Left_or_Right_Move(data_temp, direct):
	str_temp = direct
	i_ = 0
	j_ = len(data_temp) - 1
	if direct == 'L':
		while i_ + 1 < j_:
			if int(data_temp[i_]) < int(data_temp[i_ + 1]):
				str_temp += 'L'
				i_ += 1
			else:
				return str_temp
	else:
		while j_ - 1 > 0:
			if int(data_temp[j_]) < int(data_temp[j_ - 1]):
				str_temp += 'R'
				j_ -= 1
			else:
				return str_temp
	return str_temp

def find_increase_string():
	i = 0
	j = int(n) - 1
	sub_sequence = []
	str_ = ''
	if int(data[0]) > int(data[-1]):
		temp = int(data[-1])
		idx = -1
	elif int(data[0]) < int(data[-1]):
		temp = int(data[0])
		idx = 0
	else:
		l_ = Left_or_Right_Move(data[i:j + 1], 'L')
		r_ = Left_or_Right_Move(data[i:j + 1], 'R')
		if len(l_) > len(r_):
			str_ += l_
		else:
			str_ += r_
		print(len(str_))
		print(str_)
		return
	sub_sequence.append(temp)
	if idx == 0:
		str_ += 'L'
		i += 1
	else:
		str_ += 'R'
		j -= 1
	while i <= j:
		d0 = int(data[i])
		d1 = int(data[j])
		if d0 > d1:
			if d1 > sub_sequence[-1]:
				sub_sequence.append(d1)
				str_ += 'R'
				j -= 1
			elif d0 > sub_sequence[-1]:
				sub_sequence.append(d0)
				str_ += 'L'
				i += 1
			else:
				break
		elif d0 < d1:
			if d0 > sub_sequence[-1]:
				sub_sequence.append(d0)
				str_ += 'L'
				i += 1
			elif d1 > sub_sequence[-1]:
				sub_sequence.append(d1)
				str_ += 'R'
				j -= 1
			else:
				break
		else:
			if d0 > sub_sequence[-1]:
				l_ = Left_or_Right_Move(data[i:j + 1], 'L')
				r_ = Left_or_Right_Move(data[i:j + 1], 'R')
				if len(l_) > len(r_):
					str_ += l_
				else:
					str_ += r_
			break
	print(len(str_))
	print(str_)
find_increase_string()


n = int(input())
nums = list(map(int, input().split()))
res = []
left = 0
right = n - 1
cur = -1
for i in range(n):
	l = nums[left]
	r = nums[right]
	if l > cur and r > cur:
		if r > l:
			res.append('L')
			left += 1
			cur = l
		elif r < l:
			res.append('R')
			right -= 1
			cur = r
		else:
			rMax = 0
			lMax = 0
			nCur = cur
			for i in range(left, right + 1):
				if nCur >= nums[i]:
					break
				lMax += 1
				nCur = nums[i]
			nCur = cur
			for i in range(right, left - 1, -1):
				if nCur >= nums[i]:
					break
				rMax += 1
				nCur = nums[i]
			if lMax > rMax:
				while cur < nums[left] and left <= right:
					res.append('L')
					cur = nums[left]
					left += 1
			else:
				while cur < nums[right] and left <= right:
					res.append('R')
					cur = nums[right]
					right -= 1
			break
	elif l > cur:
		res.append('L')
		left += 1
		cur = l
	elif r > cur:
		res.append('R')
		right -= 1
		cur = r
	else:
		break
print(len(res))
for x in res:
	print(x, end='')


n = int(input())
x = list(map(int, input().strip().split()))
i = 0
j = n - 1
v = -1
steps = ''
while i != j:
	if x[i] > x[j]:
		if x[j] > v:
			steps += 'R'
			v = x[j]
			j -= 1
		elif x[i] > v:
			steps += 'L'
			v = x[i]
			i += 1
		else:
			break
	elif x[j] > x[i]:
		if x[i] > v:
			steps += 'L'
			v = x[i]
			i += 1
		elif x[j] > v:
			steps += 'R'
			v = x[j]
			j -= 1
		else:
			break
	else:
		if x[i] > v:
			vk = v
			k = i
			stepsL = ''
			while x[k] > vk and j != k:
				stepsL += 'L'
				vk = x[k]
				k += 1
			vk = v
			k = j
			stepsR = ''
			while x[k] > vk and i != k:
				stepsR += 'R'
				vk = x[k]
				k -= 1
			if len(stepsL) > len(stepsR):
				steps += stepsL
			else:
				steps += stepsR
		print(len(steps))
		print(steps)
		exit()
if x[i] > v:
	steps += 'R'
print(len(steps))
print(steps)


def fun(array):
	ans = ''
	count = 0
	(l, r) = (0, n - 1)
	last = -1
	while l <= r:
		if min(array[l], array[r]) <= last:
			break
		if array[l] < array[r]:
			last = array[l]
			ans += 'L'
			l += 1
			count += 1
		elif array[l] > array[r]:
			last = array[r]
			ans += 'R'
			r -= 1
			count += 1
		else:
			break
	ans_1 = ans
	last_1 = last
	l_1 = l
	while l_1 <= r and array[l_1] > last_1:
		ans_1 += 'L'
		last_1 = array[l_1]
		l_1 += 1
	ans_2 = ans
	last_2 = last
	r_2 = r
	while r_2 >= l and array[r_2] > last_2:
		ans_2 += 'R'
		last_2 = array[r_2]
		r_2 -= 1
	if len(ans_1) > len(ans_2):
		ans = ans_1
	else:
		ans = ans_2
	print(len(ans))
	print(ans)
n = int(input())
array = list(map(int, input().split()))
fun(array)


from collections import deque

def solve(A):
	res = []
	last = 0
	while A:
		if max(A[0], A[-1]) <= last:
			return res
		if A[0] == A[-1]:
			v = A[0] - 1
			for (i, a) in enumerate(A):
				if v < a:
					v = a
				else:
					break
			else:
				i += 1
			L = i
			v = A[-1] - 1
			for (i, a) in enumerate(reversed(A)):
				if v < a:
					v = a
				else:
					break
			else:
				i += 1
			R = i
			(_, op) = max((L, ['L'] * L), (R, ['R'] * R))
			res.extend(op)
			return res
		(v, op) = min(((v, op) for (v, op) in ((A[0], 'L'), (A[-1], 'R')) if v > last))
		last = v
		res.append(op)
		if op == 'L':
			A.popleft()
		else:
			A.pop()

def main():
	input()
	A = deque(map(int, input().split()))
	res = solve(A)
	print(len(res))
	print(*res, sep='')
main()


n = int(input())
sequencia = [int(i) for i in input().split()]
res = []
lastValue = 0
l = 0
r = len(sequencia) - 1
decide = 0
while l <= r:
	if sequencia[l] < sequencia[r] or decide == 1:
		if lastValue < sequencia[l]:
			res.append('L')
			lastValue = sequencia[l]
			l += 1
		elif lastValue < sequencia[r]:
			res.append('R')
			lastValue = sequencia[r]
			r -= 1
		else:
			break
	elif sequencia[r] < sequencia[l] or decide == 2:
		if lastValue < sequencia[r]:
			res.append('R')
			lastValue = sequencia[r]
			r -= 1
		elif lastValue < sequencia[l]:
			res.append('L')
			lastValue = sequencia[l]
			l += 1
		else:
			break
	else:
		qtdL = 0
		indL = l
		nValue = lastValue
		while indL < len(sequencia) and nValue < sequencia[indL]:
			nValue = sequencia[indL]
			indL += 1
			qtdL += 1
		qtdR = 0
		indR = r
		nValue = lastValue
		while indR >= 0 and nValue < sequencia[indR]:
			nValue = sequencia[indR]
			indR -= 1
			qtdR += 1
		if qtdL > qtdR:
			decide = 1
		else:
			decide = 2
print(len(res))
print(''.join(res))


sz = int(input())
lst = list(map(int, input().split(' ')))
(l, r, out) = (0, sz - 1, [])
for i in range(len(lst)):
	if not i:
		if lst[l] < lst[r]:
			out.append('L')
			prev = lst[l]
			l += 1
		else:
			out.append('R')
			prev = lst[r]
			r -= 1
	else:
		b = False
		if l < len(lst) and lst[l] > prev and (lst[l] < lst[r]):
			out.append('L')
			prev = lst[l]
			l += 1
			b = True
		elif r >= 0 and lst[r] > prev:
			out.append('R')
			prev = lst[r]
			r -= 1
			b = True
		if not b and l < len(lst) and (lst[l] > prev):
			out.append('L')
			prev = lst[l]
			l += 1
(l, r, out2) = (0, sz - 1, [])
for i in range(len(lst)):
	if not i:
		if lst[l] < lst[r]:
			out2.append('L')
			prev = lst[l]
			l += 1
		else:
			out2.append('R')
			prev = lst[r]
			r -= 1
	else:
		b = False
		if l < len(lst) and lst[l] > prev and (lst[l] <= lst[r]):
			out2.append('L')
			prev = lst[l]
			l += 1
			b = True
		elif r >= 0 and lst[r] > prev:
			out2.append('R')
			prev = lst[r]
			r -= 1
			b = True
		if not b and l < len(lst) and (lst[l] > prev):
			out2.append('L')
			prev = lst[l]
			l += 1
if len(out2) > len(out):
	out = out2.copy()
(l, r, out3) = (0, sz - 1, [])
for i in range(len(lst)):
	if not i:
		if lst[l] <= lst[r]:
			out3.append('L')
			prev = lst[l]
			l += 1
		else:
			out3.append('R')
			prev = lst[r]
			r -= 1
	else:
		b = False
		if l < len(lst) and lst[l] > prev and (lst[l] <= lst[r]):
			out3.append('L')
			prev = lst[l]
			l += 1
			b = True
		elif r >= 0 and lst[r] > prev:
			out3.append('R')
			prev = lst[r]
			r -= 1
			b = True
		if not b and l < len(lst) and (lst[l] > prev):
			out3.append('L')
			prev = lst[l]
			l += 1
if len(out3) > len(out):
	out = out3.copy()
(l, r, out4) = (0, sz - 1, [])
for i in range(len(lst)):
	if not i:
		if lst[l] <= lst[r]:
			out4.append('L')
			prev = lst[l]
			l += 1
		else:
			out4.append('R')
			prev = lst[r]
			r -= 1
	else:
		b = False
		if l < len(lst) and lst[l] > prev and (lst[l] < lst[r]):
			out4.append('L')
			prev = lst[l]
			l += 1
			b = True
		elif r >= 0 and lst[r] > prev:
			out4.append('R')
			prev = lst[r]
			r -= 1
			b = True
		if not b and l < len(lst) and (lst[l] > prev):
			out4.append('L')
			prev = lst[l]
			l += 1
if len(out4) > len(out):
	out = out4.copy()
print(len(out))
print(*out, sep='')


from sys import stdin, stdout, maxsize
import math
from copy import deepcopy
from collections import defaultdict
import heapq
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
sequence = [0]
moves = []
i = 0
j = n - 1
while i != j:
	if arr[i] >= arr[j]:
		if arr[j] > sequence[-1]:
			sequence.append(arr[j])
			j -= 1
			moves.append('R')
		elif arr[i] > sequence[-1]:
			sequence.append(arr[i])
			i += 1
			moves.append('L')
		else:
			break
	elif arr[i] > sequence[-1]:
		sequence.append(arr[i])
		i += 1
		moves.append('L')
	elif arr[j] > sequence[-1]:
		sequence.append(arr[j])
		j -= 1
		moves.append('R')
	else:
		break
if i == j:
	if arr[i] > sequence[-1]:
		sequence.append(arr[i])
		moves.append('R')
sequence2 = [0]
moves2 = []
i = 0
j = n - 1
while i != j:
	if arr[i] > arr[j]:
		if arr[j] > sequence2[-1]:
			sequence2.append(arr[j])
			j -= 1
			moves2.append('R')
		elif arr[i] > sequence2[-1]:
			sequence2.append(arr[i])
			i += 1
			moves2.append('L')
		else:
			break
	elif arr[i] > sequence2[-1]:
		sequence2.append(arr[i])
		i += 1
		moves2.append('L')
	elif arr[j] > sequence2[-1]:
		sequence2.append(arr[j])
		j -= 1
		moves2.append('R')
	else:
		break
if i == j:
	if arr[i] > sequence2[-1]:
		sequence2.append(arr[i])
		moves2.append('R')
if len(sequence2) > len(sequence):
	print(len(sequence2) - 1)
	for i in moves2:
		print(i, end='')
else:
	print(len(sequence) - 1)
	for i in moves:
		print(i, end='')


from collections import deque
n = int(input())
a = [int(x) for x in input().split()]
q = deque(a)
last = -1
left = [1] * n
right = [1] * n
for i in range(1, n):
	if a[i] < a[i - 1]:
		left[i] = left[i - 1] + 1
for i in range(n - 2, -1, -1):
	if a[i] < a[i + 1]:
		right[i] = right[i + 1] + 1
ans = []
le = 0
ri = n - 1
while len(q) > 0:
	if q[0] <= last and q[-1] <= last:
		break
	if (q[0] < q[-1] or (q[0] == q[-1] and right[le] > left[ri])) and q[0] > last or q[-1] <= last:
		ans.append('L')
		le += 1
		last = q.popleft()
	else:
		ans.append('R')
		ri -= 1
		last = q.pop()
print(len(ans))
print(*ans, sep='')


n = int(input())
a = list(map(int, input().split()))
Lind = 0
Rind = n - 1
flag = True
ans = list()
ans.append(0)
ans2 = list()
flag5 = True
flag6 = True
while flag and Rind >= Lind and flag5 and flag6:
	if a[Rind] > a[Lind]:
		if a[Lind] > ans[-1]:
			ans.append(a[Lind])
			Lind += 1
			ans2.append('L')
		else:
			flag5 = False
	elif a[Lind] > a[Rind]:
		if a[Rind] > ans[-1]:
			ans.append(a[Rind])
			Rind -= 1
			ans2.append('R')
		else:
			flag6 = False
	else:
		flag = False
if not flag or not flag5 or (not flag6):
	lmax = 0
	rmax = 0
	for i in range(Lind, Rind):
		if a[i] > ans[-1]:
			if a[i + 1] > a[i]:
				lmax += 1
			else:
				lmax += 1
				break
		else:
			break
	for i in reversed(range(Lind + 1, Rind + 1)):
		if a[i] > ans[-1]:
			if a[i - 1] > a[i]:
				rmax += 1
			else:
				rmax += 1
				break
		else:
			break
	if lmax > rmax:
		i = Lind
		while Lind <= Rind:
			if a[i] > ans[-1]:
				ans.append(a[i])
				ans2.append('L')
				i += 1
			else:
				break
	else:
		i = Rind
		while Rind >= Lind:
			if a[i] > ans[-1]:
				ans.append(a[i])
				ans2.append('R')
				i -= 1
			else:
				break
print(len(ans) - 1)
print(*ans2, end='', sep='')


n = int(input())
a = [int(x) for x in input().split(' ')]
left = 0
right = n - 1
curr = 0
ans = ''
while left <= right:
	if curr < a[left] and curr < a[right]:
		if a[left] < a[right]:
			ans += 'L'
			curr = a[left]
			left += 1
		elif a[left] > a[right]:
			ans += 'R'
			curr = a[right]
			right -= 1
		else:
			count_left = left + 1
			while count_left <= right and a[count_left] > a[count_left - 1]:
				count_left += 1
			count_right = right - 1
			while count_right >= left and a[count_right] > a[count_right + 1]:
				count_right -= 1
			if count_left - left - 1 > right - count_right - 1:
				ans += 'L'
				curr = a[left]
				left += 1
			else:
				ans += 'R'
				curr = a[right]
				right -= 1
	elif curr < a[left]:
		ans += 'L'
		curr = a[left]
		left += 1
	elif curr < a[right]:
		ans += 'R'
		curr = a[right]
		right -= 1
	else:
		break
print(len(ans))
print(ans)


def calculate(a, choice=None, l=None, r=None):
	if l is None:
		l = 0
		r = len(a) - 1
	latest = 0
	resp = ''
	while r >= l:
		if a[l] > latest:
			if a[r] <= latest:
				resp += 'L'
				latest = a[l]
				l += 1
			elif a[l] < a[r]:
				resp += 'L'
				latest = a[l]
				l += 1
			elif a[l] == a[r]:
				if choice == 'L':
					resp += 'L'
					latest = a[l]
					l += 1
					choice = None
				elif choice == 'R':
					resp += 'R'
					latest = a[r]
					r -= 1
					choice = None
				else:
					lvar = calculate(a, 'L', l, r)
					rvar = calculate(a, 'R', l, r)
					if len(lvar) > len(rvar):
						return resp + lvar
					else:
						return resp + rvar
			else:
				resp += 'R'
				latest = a[r]
				r -= 1
		elif a[r] > latest:
			resp += 'R'
			latest = a[r]
			r -= 1
		else:
			break
	return resp
n = int(input())
a = [int(i) for i in input().split()]
res = calculate(a)
print(len(res))
print(res)