結果

問題 No.1288 yuki collection
ユーザー mkawa2mkawa2
提出日時 2023-10-18 16:01:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,266 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,724 KB
実行使用メモリ 85,964 KB
最終ジャッジ日時 2023-10-18 16:02:08
合計ジャッジ時間 24,951 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,472 KB
testcase_01 AC 41 ms
53,468 KB
testcase_02 AC 43 ms
53,472 KB
testcase_03 AC 88 ms
75,644 KB
testcase_04 AC 42 ms
53,472 KB
testcase_05 AC 41 ms
53,472 KB
testcase_06 AC 57 ms
64,096 KB
testcase_07 AC 42 ms
53,472 KB
testcase_08 AC 105 ms
75,796 KB
testcase_09 AC 88 ms
75,684 KB
testcase_10 AC 106 ms
75,844 KB
testcase_11 AC 75 ms
72,380 KB
testcase_12 AC 96 ms
75,804 KB
testcase_13 AC 985 ms
85,164 KB
testcase_14 AC 1,007 ms
84,876 KB
testcase_15 AC 793 ms
82,900 KB
testcase_16 AC 802 ms
82,868 KB
testcase_17 AC 993 ms
85,964 KB
testcase_18 AC 1,024 ms
85,208 KB
testcase_19 AC 951 ms
85,172 KB
testcase_20 AC 1,021 ms
85,168 KB
testcase_21 AC 961 ms
85,484 KB
testcase_22 AC 1,011 ms
85,500 KB
testcase_23 AC 985 ms
85,408 KB
testcase_24 AC 996 ms
85,440 KB
testcase_25 AC 998 ms
85,036 KB
testcase_26 AC 982 ms
85,532 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 568 ms
81,216 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 31)
# inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

from heapq import *
class MinCostFlow:
    def __init__(self, n):
        self.inf=10**9
        self.n = n
        self.to = [[] for _ in range(n)]

    def add_edge(self, u, v, cap, cost):
        self.to[u].append([v, cap, cost, len(self.to[v])])
        self.to[v].append([u, 0, -cost, len(self.to[u])-1])

    # s...source,t...sink,f...flow
    # これが本体
    def cal(self,s,t,f):
        min_cost=0
        n=self.n
        pot=[0]*n
        while f:
            dist = [self.inf] * n
            pre_u=[-1]*n   # 最短距離の遷移元の頂点
            pre_e=[-1]*n   # 最短距離の遷移元の辺
            # ダイクストラで最短距離を求める
            hp=[]
            heappush(hp,(0,s))
            dist[s]=0
            while hp:
                d,u=heappop(hp)
                if d>dist[u]:continue
                for i,(v,cap,cost,rev) in enumerate(self.to[u]):
                    if cap==0:continue
                    nd=dist[u]+cost+pot[u]-pot[v]
                    if nd>=dist[v]:continue
                    dist[v]=nd
                    pre_u[v]=u
                    pre_e[v]=i
                    heappush(hp,(nd,v))
            # sinkまで届かなかったら不可能ということ
            if dist[t]==self.inf:return -1
            # ポテンシャルを更新する
            for u in range(n):pot[u]+=dist[u]
            # パスs-t上で最小の容量=流す量を求める
            u=t
            min_cap=f
            while u!=s:
                u,e=pre_u[u],pre_e[u]
                min_cap=min(min_cap,self.to[u][e][1])
            # フローから流す量を減らし、コストを加える
            f-=min_cap
            min_cost+=min_cap*pot[t]
            # パスs-tの容量を更新する
            u=t
            while u!=s:
                u,e,v=pre_u[u],pre_e[u],u
                self.to[u][e][1]-=min_cap
                rev=self.to[u][e][3]
                self.to[v][rev][1]+=min_cap
        return min_cost

n = II()
S = ["yuki".find(c) for c in SI()]
vv = LI()

mf = MinCostFlow(n+2)
s = n
t = s+1

ii = [[], [], [], []]
for i in range(n)[::-1]:
    c = S[i]
    if c == 3:
        mf.add_edge(i, t, 1, -vv[i])
    elif ii[c+1]:
        mf.add_edge(i, ii[c+1][-1], 1, -vv[i])
    ii[c].append(i)

for c in range(4):
    for i, j in zip(ii[c], ii[c][1:]):
        mf.add_edge(j, i, n, 0)

if ii[0]: mf.add_edge(s, ii[0][-1], n, 0)
mf.add_edge(s,t,n//4,0)

ans = mf.cal(s, t,n//4)
print(-ans)
0