結果

問題 No.1288 yuki collection
ユーザー mkawa2mkawa2
提出日時 2023-10-18 16:13:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,377 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 81,516 KB
実行使用メモリ 89,140 KB
最終ジャッジ日時 2023-10-18 16:13:43
合計ジャッジ時間 25,610 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,620 KB
testcase_01 AC 41 ms
53,412 KB
testcase_02 AC 42 ms
53,412 KB
testcase_03 AC 87 ms
75,676 KB
testcase_04 AC 41 ms
53,412 KB
testcase_05 AC 41 ms
53,412 KB
testcase_06 AC 56 ms
64,092 KB
testcase_07 AC 42 ms
53,412 KB
testcase_08 AC 107 ms
75,820 KB
testcase_09 AC 87 ms
75,700 KB
testcase_10 AC 104 ms
75,868 KB
testcase_11 AC 74 ms
72,376 KB
testcase_12 AC 103 ms
75,792 KB
testcase_13 AC 1,019 ms
88,800 KB
testcase_14 AC 1,038 ms
88,240 KB
testcase_15 AC 844 ms
85,568 KB
testcase_16 AC 864 ms
85,572 KB
testcase_17 AC 1,018 ms
88,740 KB
testcase_18 AC 1,086 ms
89,140 KB
testcase_19 AC 1,061 ms
88,644 KB
testcase_20 AC 982 ms
85,264 KB
testcase_21 AC 1,000 ms
84,664 KB
testcase_22 AC 1,064 ms
88,840 KB
testcase_23 AC 1,061 ms
88,476 KB
testcase_24 AC 1,100 ms
88,728 KB
testcase_25 AC 976 ms
84,708 KB
testcase_26 AC 1,064 ms
88,988 KB
testcase_27 AC 624 ms
83,204 KB
testcase_28 AC 669 ms
83,836 KB
testcase_29 AC 593 ms
83,148 KB
testcase_30 AC 189 ms
79,084 KB
testcase_31 AC 202 ms
79,116 KB
testcase_32 AC 209 ms
79,444 KB
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 = inf
        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, 0)

ans = mf.cal(s, t, min(len(ii[c]) for c in range(4)))
print(-ans)
0