P25363 [AtCoder] Sum of Cards
省选
题目描述

Problem Description

Takahashi first wrote integers from $1$ to $N$ on the front of $N$ cards, then shuffled them and wrote integers from $1$ to $N$ on the back of each card.

The $i$-th card has $a_i$ on the front and $b_i$ on the back.

Takahashi wants to place these $N$ cards with any side facing up to maximize the sum of the visible integers.

However, this task is too easy for Takahashi.

Takahashi gets sad if there are too few different visible integers, so he decided to find the maximum sum under the condition that at least $K$ different integers are visible.

Please calculate this value for Takahashi.

Input Format

The input is given from the standard input in the following format:

$ N $ $ K $ $ a_1 $ $ a_2 $ ... $ a_{N-1} $ $ a_N $ $ b_1 $ $ b_2 $ ... $ b_{N-1} $ $ b_N $

Output Format

Output the maximum sum.

Sample Input and Output

Sample Input #1

2 2
2 1
1 2

Sample Output #1

3

Sample Input #2

2 1
2 1
1 2

Sample Output #2

4

Sample Input #3

3 2
2 3 1
1 3 2

Sample Output #3

7

Notes/Hints

Constraints

  • $1 \leq K \leq N \leq 5000$
  • $1 \leq a_i, b_i \leq N$
  • $ a_1, a_2, ..., a_N $ contain each of $1, 2, ..., N$ exactly once.
  • $ b_1, b_2, ..., b_N $ contain each of $1, 2, ..., N$ exactly once.
  • All inputs are integers.

Sample Explanation 1

There are cards with fronts and backs $\{1, 2\}$ and $\{2, 1\}$. You can either place both cards face up or both face down to see two different integers, and the sum is $3$.

Sample Explanation 2

There are cards with fronts and backs $\{1, 2\}$ and $\{2, 1\}$. Placing both cards with the $2$ side up is best, and the sum is $4$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 2 2 1 1 2
样例输出
3