結果

問題 No.1288 yuki collection
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-17 10:07:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,126 ms / 5,000 ms
コード長 1,897 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 91,348 KB
最終ジャッジ日時 2023-10-11 21:38:44
合計ジャッジ時間 27,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,060 KB
testcase_01 AC 78 ms
71,164 KB
testcase_02 AC 81 ms
71,060 KB
testcase_03 AC 117 ms
77,364 KB
testcase_04 AC 81 ms
71,452 KB
testcase_05 AC 82 ms
71,064 KB
testcase_06 AC 86 ms
75,412 KB
testcase_07 AC 80 ms
70,992 KB
testcase_08 AC 133 ms
77,560 KB
testcase_09 AC 118 ms
77,672 KB
testcase_10 AC 130 ms
77,376 KB
testcase_11 AC 109 ms
77,240 KB
testcase_12 AC 135 ms
77,884 KB
testcase_13 AC 1,059 ms
89,232 KB
testcase_14 AC 1,016 ms
88,732 KB
testcase_15 AC 895 ms
87,048 KB
testcase_16 AC 843 ms
87,056 KB
testcase_17 AC 1,021 ms
89,608 KB
testcase_18 AC 1,081 ms
90,108 KB
testcase_19 AC 1,018 ms
89,780 KB
testcase_20 AC 985 ms
86,076 KB
testcase_21 AC 1,008 ms
88,452 KB
testcase_22 AC 1,098 ms
91,184 KB
testcase_23 AC 1,058 ms
91,348 KB
testcase_24 AC 1,088 ms
89,120 KB
testcase_25 AC 992 ms
86,216 KB
testcase_26 AC 1,071 ms
90,048 KB
testcase_27 AC 590 ms
85,208 KB
testcase_28 AC 646 ms
85,520 KB
testcase_29 AC 578 ms
84,876 KB
testcase_30 AC 193 ms
81,276 KB
testcase_31 AC 208 ms
80,632 KB
testcase_32 AC 211 ms
80,212 KB
testcase_33 AC 916 ms
86,376 KB
testcase_34 AC 1,063 ms
89,164 KB
testcase_35 AC 1,048 ms
88,688 KB
testcase_36 AC 1,092 ms
85,632 KB
testcase_37 AC 1,126 ms
86,484 KB
testcase_38 AC 509 ms
82,172 KB
testcase_39 AC 551 ms
82,772 KB
testcase_40 AC 136 ms
79,244 KB
testcase_41 AC 79 ms
71,400 KB
testcase_42 AC 79 ms
71,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
# 最小費用流
from heapq import heappush, heappop
class MinCostFlow:
  INF = 10**18

  def __init__(self, N):
    self.N = N
    self.G = [[] for i in range(N)]

  def add_edge(self, fr, to, cap, cost):
    forward = [to, cap, cost, None]
    backward = forward[3] = [fr, 0, -cost, forward]
    self.G[fr].append(forward)
    self.G[to].append(backward)

  def flow(self, s, t, f):
    N = self.N; G = self.G
    INF = MinCostFlow.INF

    res = 0
    H = [0]*N
    prv_v = [0]*N
    prv_e = [None]*N

    d0 = [INF]*N
    dist = [INF]*N

    while f:
      dist[:] = d0
      dist[s] = 0
      que = [(0, s)]

      while que:
        c, v = heappop(que)
        if dist[v] < c:
          continue
        r0 = dist[v] + H[v]
        for e in G[v]:
          w, cap, cost, _ = e
          if cap > 0 and r0 + cost - H[w] < dist[w]:
            dist[w] = r = r0 + cost - H[w]
            prv_v[w] = v; prv_e[w] = e
            heappush(que, (r, w))
      if dist[t] == INF:
        return res
        return None

      for i in range(N):
        H[i] += dist[i]

      d = f; v = t
      while v != s:
        d = min(d, prv_e[v][1])
        v = prv_v[v]
      f -= d
      res += d * H[t]
      v = t
      while v != s:
        e = prv_e[v]
        e[1] -= d
        e[3][1] += d
        v = prv_v[v]
    return res

def main1(n,s,v):
  mcf=MinCostFlow(n+1)
  inf=10**9
  # n->n+1
  ary=[n+1]*5
  ary[4]=n
  d={'y':0,'u':1,'k':2,'i':3}
  for i in range(n-1,-1,-1):
    k=d[s[i]]
    if ary[k]<n+1:
      mcf.add_edge(i,ary[k],n//4,0)
    if ary[k+1]<n+1:
      mcf.add_edge(i,ary[k+1],1,inf-v[i])
    ary[k]=i
  if ary[0]==n+1:
    return 0
  mcf.add_edge(ary[0],n,n//4,inf*4)
  return 4*inf*(n//4)-mcf.flow(ary[0],n,n//4)

if __name__=='__main__':
  n=int(input())
  s=list(input())
  v=list(map(int,input().split()))
  ret1=main1(n,s,v)
  print(ret1)
0