結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,560 KB
testcase_01 AC 40 ms
53,528 KB
testcase_02 AC 41 ms
53,532 KB
testcase_03 AC 87 ms
75,780 KB
testcase_04 AC 41 ms
53,532 KB
testcase_05 AC 40 ms
53,532 KB
testcase_06 AC 56 ms
64,200 KB
testcase_07 AC 41 ms
53,532 KB
testcase_08 AC 105 ms
75,920 KB
testcase_09 AC 89 ms
75,804 KB
testcase_10 AC 103 ms
75,968 KB
testcase_11 AC 72 ms
72,484 KB
testcase_12 AC 99 ms
75,896 KB
testcase_13 AC 1,044 ms
88,924 KB
testcase_14 AC 1,040 ms
88,348 KB
testcase_15 AC 842 ms
85,672 KB
testcase_16 AC 852 ms
85,688 KB
testcase_17 AC 1,039 ms
88,836 KB
testcase_18 AC 1,077 ms
89,240 KB
testcase_19 AC 1,041 ms
88,724 KB
testcase_20 AC 983 ms
85,408 KB
testcase_21 AC 970 ms
84,792 KB
testcase_22 AC 1,067 ms
88,956 KB
testcase_23 AC 1,053 ms
88,552 KB
testcase_24 AC 1,081 ms
88,832 KB
testcase_25 AC 984 ms
84,736 KB
testcase_26 AC 1,058 ms
89,088 KB
testcase_27 AC 583 ms
83,312 KB
testcase_28 AC 677 ms
83,896 KB
testcase_29 AC 573 ms
83,256 KB
testcase_30 AC 189 ms
79,172 KB
testcase_31 AC 199 ms
79,224 KB
testcase_32 AC 234 ms
79,516 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//4, 0)

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