結果

問題 No.3605 Grand Cross
コンテスト
ユーザー ゼット
提出日時 2026-07-31 22:27:09
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 2,217 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 440 ms
コンパイル使用メモリ 96,104 KB
実行使用メモリ 207,648 KB
最終ジャッジ日時 2026-07-31 22:27:47
合計ジャッジ時間 18,692 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

Q=int(input())
for _ in range(Q):
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  B=list(map(int,input().split()))
  C=list(map(int,input().split()))
  D=list(map(int,input().split()))
  Ga=[[] for i in range(N+M+1)]
  Gb=[[] for i in range(N+M+1)]
  result=-10**100
  for i in range(N):
    x=C[i]
    Ga[x].append(i)
  for i in range(M):
    x=D[i]
    Gb[x].append(i)
  va=[0]*(N+1)
  vb=[0]*(M+1)
  for i in range(N):
    va[i]=va[i-1]+A[i]
  for i in range(M):
    vb[i]=vb[i-1]+B[i]
  for x in range(1,N+M+1):
    if len(Ga[x])*len(Gb[x])==0:
      continue
    d=-1
    for pos in Ga[x]:
      if pos<N//2:
        d=max(d,pos)
    if d>=0:
      s=va[2*d]
      score=-10**20
      for pos in Gb[x]:
        l,r=pos-d,pos+d
        if 0<=l<M and 0<=r<M:
          ans=vb[r]-vb[l-1]
          score=max(score,ans)
      result=max(result,score+s)
    d=-1
    for pos in Ga[x]:
      if pos>=N//2:
        d=max(d,N-1-pos)
    s=va[N-1]-va[N-1-(2*d+1)]
    score=-10**20
    if d>=0:
      for pos in Gb[x]:
        l,r=pos-d,pos+d
        if 0<=l<M and 0<=r<M:
          ans=vb[r]-vb[l-1]
          score=max(score,ans)
      result=max(result,score+s)
  A,B=B,A
  C,D=D,C
  N,M=M,N
  Ga=[[] for i in range(N+M+1)]
  Gb=[[] for i in range(N+M+1)]
  for i in range(N):
    x=C[i]
    Ga[x].append(i)
  for i in range(M):
    x=D[i]
    Gb[x].append(i)
  va=[0]*(N+1)
  vb=[0]*(M+1)
  for i in range(N):
    va[i]=va[i-1]+A[i]
  for i in range(M):
    vb[i]=vb[i-1]+B[i]
  for x in range(1,N+M+1):
    if len(Ga[x])*len(Gb[x])==0:
      continue
    d=-1
    for pos in Ga[x]:
      if pos<N//2:
        d=max(d,pos)
    s=va[2*d]
    score=-10**20
    if d>=0:
      for pos in Gb[x]:
        l,r=pos-d,pos+d
        if 0<=l<M and 0<=r<M:
          ans=vb[r]-vb[l-1]
          score=max(score,ans)
      result=max(result,score+s)
    d=-1
    for pos in Ga[x]:
      if pos>=N//2:
        d=max(d,N-1-pos)
    s=va[N-1]-va[N-1-(2*d+1)]
    score=-10**20
    if d>=0:
      for pos in Gb[x]:
        l,r=pos-d,pos+d
        if 0<=l<M and 0<=r<M:
          ans=vb[r]-vb[l-1]
          score=max(score,ans)
      result=max(result,score+s)
  if result==-10**100:
   	result=-1
  print(result)
0