問題一覧 > 通常問題

No.3605 Grand Cross

レベル : / AI推定難易度 : 1900 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 15
作問者 : askr58 / テスター : 👑 AngrySadEight
お気に入りにしたユーザー ProblemId : 13646 / 自分の提出
問題文最終更新日: 2026-07-30 06:11:12
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 507 オムニバス (順位表) の他の問題:

Problem Statement

There are an integer sequence $A=(A_1,A_2,\cdots,A_N)$ of length $N$ and an integer sequence $B=(B_1,B_2,\cdots,B_M)$ of length $M$. Each element of $A$ and $B$ is painted in a color numbered with an integer between $1$ and $N+M$, inclusive. The $i$-th element of $A$ is painted in color $C_i$, and the $i$-th element of $B$ is painted in color $D_i$.

A pair $(X,Y)$ of a non-empty contiguous subarray $X=(X_1,X_2,\cdots,X_{|X|})$ of $A$ and a non-empty contiguous subarray $Y=(Y_1,Y_2,\cdots,Y_{|Y|})$ of $B$ that satisfies the following condition is called a cross.

  • $|X|=|Y|$, $|X|$ is odd, and the $\frac{|X|+1}{2}$-th element of $X$ and the $\frac{|Y|+1}{2}$-th element of $Y$ are painted in the same color.

The score of a cross $(X,Y)$ is defined as $\sum\limits_{i=1}^{|X|} (X_i+Y_i)$. Find the maximum possible score of a cross. If not even $1$ cross exists, report that fact.

Solve $T$ test cases.

Constraints

  • $1\le T\le 2\times 10^5$
  • $1\le N,M\le 2\times 10^5$
  • $1\le A_i,B_j\le 10^9(1\le i\le N,1\le j\le M)$
  • $1\le C_i,D_j\le N+M(1\le i\le N,1\le j\le M)$
  • The sum of $N$ and the sum of $M$ over the $T$ test cases each do not exceed $2\times 10^5$
  • All inputs are integers

Input

The input is given in the following format, where $case_i$ denotes the $i$-th test case.

$T$
$case_1$
$case_2$
$\vdots$
$case_T$

Each test case is given in the following format.

$N\ M$
$A_1\ A_2\ \cdots\ A_N$
$B_1\ B_2\ \cdots\ B_M$
$C_1\ C_2\ \cdots\ C_N$
$D_1\ D_2\ \cdots\ D_M$

Output

Print $T$ lines. On the $i$-th line, for the $i$-th test case, print the maximum possible score of a cross if a cross exists, and -1 otherwise.

Sample

Sample 1
Input
3
8 5
4 3 1 1 5 6 1 4
5 1 5 2 3
8 8 4 7 1 5 1 2
1 1 6 6 2
3 2
3 4 5
4 3
1 1 1
2 2
15 20
7 9 8 4 10 7 2 5 3 11 3 1 9 1 2
11 11 7 4 3 7 2 2 6 8 5 1 7 1 9 9 3 1 3 2
12 6 7 12 6 10 14 6 17 4 17 16 4 3 8
14 11 3 14 18 11 17 19 2 7 17 13 2 12 6 17 3 12 17 6
Output
23
-1
140

In test case $1$, the following are examples of crosses.

  • Let $X=(A_4,A_5,A_6),Y=(B_1,B_2,B_3)$. Then $|X|=|Y|=3$, and the colors of $X_2$ and $Y_2$ are both $C_5=D_2=1$, so this is a cross. The score of this cross is $(1+5)+(5+1)+(6+5)=23$.
  • Let $X=(A_8),Y=(B_5)$. Then $|X|=|Y|=1$, and the colors of $X_1$ and $Y_1$ are both $C_8=D_5=2$, so this is a cross. The score of this cross is $4+3=7$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。