結果

問題 No.1288 yuki collection
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-17 10:02:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,058 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 85,632 KB
最終ジャッジ日時 2023-10-11 21:34:15
合計ジャッジ時間 24,220 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,228 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 120 ms
77,404 KB
testcase_04 RE -
testcase_05 WA -
testcase_06 AC 85 ms
75,196 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 124 ms
77,560 KB
testcase_10 AC 127 ms
77,552 KB
testcase_11 AC 108 ms
77,356 KB
testcase_12 AC 138 ms
78,028 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 738 ms
84,380 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 879 ms
85,216 KB
testcase_20 WA -
testcase_21 AC 877 ms
82,520 KB
testcase_22 AC 903 ms
85,156 KB
testcase_23 AC 925 ms
85,596 KB
testcase_24 AC 882 ms
84,988 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 514 ms
82,860 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 189 ms
80,716 KB
testcase_31 AC 207 ms
80,572 KB
testcase_32 AC 206 ms
80,132 KB
testcase_33 AC 832 ms
81,712 KB
testcase_34 AC 869 ms
85,024 KB
testcase_35 AC 856 ms
84,972 KB
testcase_36 AC 897 ms
82,236 KB
testcase_37 AC 911 ms
83,080 KB
testcase_38 AC 482 ms
80,112 KB
testcase_39 AC 468 ms
80,364 KB
testcase_40 AC 136 ms
78,980 KB
testcase_41 WA -
testcase_42 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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+2:
    print(0)
    exit()
  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