結果

問題 No.1288 yuki collection
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-17 10:05:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 905 ms / 5,000 ms
コード長 4,411 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 84,020 KB
最終ジャッジ日時 2024-09-13 20:28:30
合計ジャッジ時間 21,504 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,632 KB
testcase_01 AC 46 ms
53,248 KB
testcase_02 AC 48 ms
53,632 KB
testcase_03 AC 92 ms
74,368 KB
testcase_04 AC 44 ms
53,504 KB
testcase_05 AC 45 ms
53,760 KB
testcase_06 AC 52 ms
59,776 KB
testcase_07 AC 44 ms
53,888 KB
testcase_08 AC 107 ms
76,160 KB
testcase_09 AC 103 ms
76,160 KB
testcase_10 AC 105 ms
76,160 KB
testcase_11 AC 82 ms
70,144 KB
testcase_12 AC 115 ms
76,288 KB
testcase_13 AC 857 ms
84,020 KB
testcase_14 AC 809 ms
82,672 KB
testcase_15 AC 707 ms
81,832 KB
testcase_16 AC 706 ms
81,876 KB
testcase_17 AC 879 ms
83,488 KB
testcase_18 AC 893 ms
83,648 KB
testcase_19 AC 864 ms
83,268 KB
testcase_20 AC 791 ms
80,108 KB
testcase_21 AC 868 ms
79,940 KB
testcase_22 AC 893 ms
83,532 KB
testcase_23 AC 905 ms
83,560 KB
testcase_24 AC 865 ms
83,332 KB
testcase_25 AC 792 ms
80,192 KB
testcase_26 AC 905 ms
83,748 KB
testcase_27 AC 501 ms
80,844 KB
testcase_28 AC 556 ms
81,080 KB
testcase_29 AC 482 ms
80,640 KB
testcase_30 AC 166 ms
78,880 KB
testcase_31 AC 186 ms
78,560 KB
testcase_32 AC 184 ms
78,460 KB
testcase_33 AC 819 ms
80,564 KB
testcase_34 AC 859 ms
83,724 KB
testcase_35 AC 831 ms
83,564 KB
testcase_36 AC 867 ms
80,896 KB
testcase_37 AC 882 ms
80,832 KB
testcase_38 AC 461 ms
78,780 KB
testcase_39 AC 461 ms
79,044 KB
testcase_40 AC 115 ms
77,824 KB
testcase_41 AC 45 ms
53,504 KB
testcase_42 AC 44 ms
53,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
# 最小費用流
from heapq import heappush, heappop
class M1inCostFlow:
  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


import heapq
class MinCostFlow:
    def __init__(self, n, neg=False):
        self.n=n
        self.g=[[] for _ in range(n)]
        self.h=[0]*n
        self.neg=neg
    def add_edge(self, frm, to, cap, cost):
        g=self.g
        g[frm].append([to, cap, cost, len(g[to])])
        g[to].append([frm, 0, -cost, len(g[frm])-1])
    def bellman_ford(self, s, t, prevv, preve):
        n=self.n
        g=self.g
        h=self.h
        INF=10**18
        for x in range(n):
            h[x]=INF
        h[s]=0
        for _ in range(n-1):
            for x in range(n):
                for i, e in enumerate(g[x]):
                    if e[1]==0:
                        continue
                    to, cost=e[0], e[2]
                    if h[to]>h[x]+cost:
                        h[to]=h[x]+cost
                        prevv[to]=x
                        preve[to]=i
    def flow(self, s, t, f):
        res=0
        n=self.n
        g=self.g
        h=self.h
        INF=10**18
        LOG=13 #n<2**LOG
        prevv=[0]*n
        preve=[0]*n
        while f>0:
            if self.neg:
                self.neg=False
                self.bellman_ford(s, t, prevv, preve)
                if h[t]==INF:
                    return -1
            else:
                que=[]
                dist=[INF]*n
                dist[s]=0
                heapq.heappush(que, s)
                while que:
                    p=heapq.heappop(que)
                    v=(p&((1<<LOG)-1))
                    if dist[v]<(p>>LOG):
                        continue
                    for i, e in enumerate(g[v]):
                        if e[1]==0:
                            continue
                        to, cost=e[0], e[2]
                        if dist[to]>dist[v]+cost+h[v]-h[to]:
                            dist[to]=dist[v]+cost+h[v]-h[to]
                            prevv[to]=v
                            preve[to]=i
                            heapq.heappush(que, (dist[to]<<LOG)^to)
                for i, d in enumerate(dist):
                    h[i]+=d
                if dist[t]==INF:
                    return -1
            d=f
            v=t
            while v!=s:
                d=min(d, g[prevv[v]][preve[v]][1])
                v=prevv[v]
            f-=d
            res+=d*h[t]
            v=t
            while v!=s:
                g[prevv[v]][preve[v]][1]-=d
                rev=g[prevv[v]][preve[v]][3]
                g[v][rev][1]+=d
                v=prevv[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