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

#TP0000013. Dãy con tăng - bản khó (Increasing Subsequence - Hard Version)

HỌC THEO LỘ TRÌNH

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

2 lộ trình

Dãy con tăng - bản khó (Increasing Subsequence - Hard Version)

Increasing Subsequence (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of nn positive integers. At each move, take either the leftmost or the rightmost remaining element, append its value to the result, and remove it from the sequence.

The resulting values must be strictly increasing. You may stop early and want to take as many elements as possible. Unlike the easy version, input values may be equal. Find the maximum possible number of moves and print a string of moves achieving that maximum.

Input

  • The first line contains integer nn, the length of the sequence.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

  • The first line contains the maximum possible length kk of a strictly increasing sequence.
  • The second line contains a string of length kk consisting of L and R in move order. Any optimal answer is allowed.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤ai≤2⋅1051\le a_i\le 2\cdot10^5 for every 1≤i≤n1\le i\le n; values may repeat.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le 5000; 1≤ai≤2⋅1051\le a_i\le 2\cdot10^5 for every 1≤i≤n1\le i\le n; values may repeat.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le 2\cdot10^5; 1≤ai≤2⋅1051\le a_i\le 2\cdot10^5 for every 1≤i≤n1\le i\le n; values may repeat.

Examples

Input

5
1 2 4 3 2

Output

4
LRRR

Explanation

Choose left 1, then right 2, right 3, and right 4 to obtain 1,2,3,4. When endpoints match, compare the increasing runs available in both directions.