結果
問題 | No.1288 yuki collection |
ユーザー |
![]() |
提出日時 | 2023-10-18 16:04:08 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,351 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 81,960 KB |
実行使用メモリ | 89,864 KB |
最終ジャッジ日時 | 2024-09-18 12:10:57 |
合計ジャッジ時間 | 21,300 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 TLE * 1 -- * 9 |
ソースコード
import sys# sys.setrecursionlimit(200005)# sys.set_int_max_str_digits(200005)int1 = lambda x: int(x)-1pDB = 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+7md = 998244353from heapq import *class MinCostFlow:def __init__(self, n):self.inf = infself.n = nself.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 = 0n = self.npot = [0]*nwhile f:dist = [self.inf]*npre_u = [-1]*n # 最短距離の遷移元の頂点pre_e = [-1]*n # 最短距離の遷移元の辺# ダイクストラで最短距離を求めるhp = []heappush(hp, (0, s))dist[s] = 0while hp:d, u = heappop(hp)if d > dist[u]: continuefor i, (v, cap, cost, rev) in enumerate(self.to[u]):if cap == 0: continuend = dist[u]+cost+pot[u]-pot[v]if nd >= dist[v]: continuedist[v] = ndpre_u[v] = upre_e[v] = iheappush(hp, (nd, v))# sinkまで届かなかったら不可能ということif dist[t] == self.inf: return -1# ポテンシャルを更新するfor u in range(n): pot[u] += dist[u]# パスs-t上で最小の容量=流す量を求めるu = tmin_cap = fwhile u != s:u, e = pre_u[u], pre_e[u]min_cap = min(min_cap, self.to[u][e][1])# フローから流す量を減らし、コストを加えるf -= min_capmin_cost += min_cap*pot[t]# パスs-tの容量を更新するu = twhile u != s:u, e, v = pre_u[u], pre_e[u], uself.to[u][e][1] -= min_caprev = self.to[u][e][3]self.to[v][rev][1] += min_capreturn min_costn = II()S = ["yuki".find(c) for c in SI()]vv = LI()mf = MinCostFlow(n+2)s = nt = s+1ii = [[], [], [], []]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)