atcoder#ARC149B. [ARC149B] Two LIS Sum
[ARC149B] Two LIS Sum
Score : points
Problem Statement
For a sequence , let denote the length of a longest increasing subsequence.
You are given permutations and of integers from through . You may perform the following operation on these sequences any number of times (possibly zero).
- Choose an integer such that . Swap and , and swap and .
Find the maximum possible final value of .
What is a longest increasing subsequence?
A subsequence of a sequence is a sequence obtained by removing zero or more elements from the original sequence and then concatenating the remaining elements without changing the order. For instance, is a subsequence of , but is not a subsequence of .
A longest increasing subsequence of a sequence is a subsequence of that sequence with the greatest length among its subsequences that are strictly increasing.
Constraints
- and , if .
Input
The input is given from Standard Input in the following format:
Output
Print the maximum possible final value of .
5
5 2 1 4 3
3 1 2 5 4
8
Here is one way to achieve .
- Do the operation with . Now, , .
- Do the operation with . Now, , .
- Do the operation with . Now, , .
Here, has a longest increasing subsequence , so , and has a longest increasing subsequence , so .
5
1 2 3 4 5
1 2 3 4 5
10
You can decide not to perform the operation at all to achieve .