結果

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

ソースコード

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=-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]
  from bisect import bisect_right
  for x in range(1,N+M+1):
    if len(Ga[x])*len(Gb[x])==0:
      continue
    d=-1
    g=[]
    h=[]
    for pos in Ga[x]:
      if pos<N//2:
        d=pos
        g.append(d)
        h.append(va[2*pos])
    if d>=0:
      s=va[2*d]
      score=-10**20
      for pos in Gb[x]:
        k=min(pos,N-1-pos)
        t=bisect_right(g,k)
        if t>0:
          d=g[t-1]
          l,r=pos-d,pos+d
          if 0<=l<M and 0<=r<M:
            ans=vb[r]-vb[l-1]
            score=max(score,ans+h[t-1])
      result=max(result,score)
    d=-1
    g=[]
    h=[]
    for pos in Ga[x]:
      if pos>=N//2:
        d=N-1-pos
        g.append(d)
        h.append(va[N-1]-va[N-1-(2*d+1)])
    g=g[::-1]
    h=h[::-1]
    score=-10**20
    if d>=0:
      for pos in Gb[x]:
        k=min(pos,N-1-pos)
        t=bisect_right(g,k)
        if t>0:
          d=g[t-1]
          l,r=pos-d,pos+d
          if 0<=l<M and 0<=r<M:
            ans=vb[r]-vb[l-1]
            score=max(score,ans+h[t-1])
      result=max(result,score)
  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]
  from bisect import bisect_right
  for x in range(1,N+M+1):
    if len(Ga[x])*len(Gb[x])==0:
      continue
    d=-1
    g=[]
    h=[]
    for pos in Ga[x]:
      if pos<N//2:
        d=pos
        g.append(d)
        h.append(va[2*pos])
    if d>=0:
      s=va[2*d]
      score=-10**20
      for pos in Gb[x]:
        k=min(pos,N-1-pos)
        t=bisect_right(g,k)
        if t>0:
          d=g[t-1]
          l,r=pos-d,pos+d
          if 0<=l<M and 0<=r<M:
            ans=vb[r]-vb[l-1]
            score=max(score,ans+h[t-1])
      result=max(result,score)
    d=-1
    g=[]
    h=[]
    for pos in Ga[x]:
      if pos>=N//2:
        d=N-1-pos
        g.append(d)
        h.append(va[N-1]-va[N-1-(2*d+1)])
    g=g[::-1]
    h=h[::-1]
    score=-10**20
    if d>=0:
      for pos in Gb[x]:
        k=min(pos,N-1-pos)
        t=bisect_right(g,k)
        if t>0:
          d=g[t-1]
          l,r=pos-d,pos+d
          if 0<=l<M and 0<=r<M:
            ans=vb[r]-vb[l-1]
            score=max(score,ans+h[t-1])
      result=max(result,score)
  if result==-10**100:
   	result=-1
  print(result)
0