input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import copy MOD = 10 ** 9 + 7 def make_list(*args): r = [0] * args[-1] for n in args[len(args) - 2::-1]: r = [copy.deepcopy(r) for _ in range(n)] return r N = int(eval(input())) dp = make_list(N + 1, 5, 5, 5) dp[0][0][0][0] = 1 # INIT = 0, A = 1, C = 2, G = 3, T = 4 for n in range(N): for i ...
import math, heapq from operator import itemgetter as ig from collections import defaultdict as dd # 定数 INF = float("inf") MOD = int(1e9 + 7) # データ構造:ヒープ class heapque: def __init__(self, *args): self.que = [] for arg in args: self.push(arg) def push(self, v): he...
p03088
#x=int(input()) n=int(eval(input())) m=102 chars=['A','T','C','G'] mo=10**9+7 d3=set() d4=set() for i in chars: for j in chars: for k in chars: d3.add(i+j+k) for l in chars: d4.add(i+j+k+l) kinsoku4=set() kinsoku3=['AGC','ACG','GAC'] for y i...
n=int(eval(input())) m=102 chars=['A','T','C','G'] mo=10**9+7 d4=set() for i in chars: for j in chars: for k in chars: for l in chars: d4.add(i+j+k+l) kinsoku4=set() kinsoku3=['AGC','ACG','GAC'] for y in chars: kinsoku4.add('AG'+y+'C') kinsoku4.a...
p03088
def main(): n = int(eval(input())) MOD = 10 ** 9 + 7 note = [{} for _ in range(n+1)] def ok(last4): for i in range(4): t = list(last4) if i >= 1: t[i-1], t[i] = t[i], t[i-1] if ''.join(t).count('AGC') >= 1: return Fa...
# from https://atcoder.jp/contests/abc122/submissions/4932940 def main(): n = int(eval(input())) MOD = 10 ** 9 + 7 dpa = [0, 0, 1] dpc = [0, 0, 1] dpg = [0, 0, 1] dpt = [0, 0, 1] for i in range(2, n + 2): total = dpa[i] + dpc[i] + dpg[i] + dpt[i] if i == n + 1...
p03088
N = int(eval(input())) memo = [{} for i in range(N + 1)] MOD = 10 ** 9 + 7 def ok(last4): for i in range(4): t = list(last4) if i >= 1: t[i], t[i - 1] = t[i - 1], t[i] if "".join(t).count("AGC") >= 1: return False return True ...
N = int(eval(input())) MOD = 10 ** 9 + 7 memo = [{} for i in range(N + 1)] def ok(last4): for i in range(4): t = list(last4) if i >= 1: t[i], t[i - 1] = t[i - 1], t[i] if "".join(t).count("AGC") >= 1: return False return True def dfs(cur, last3): ...
p03088
mod = 1000000007 N = int(eval(input())) dp = [ [[[0 for c3 in range(4)] for c2 in range(4)] for c1 in range(4)] for n in range(N+1)] # 全部TのときはOK dp[0][3][3][3] = 1 # 文字の数 for n in range(N): # 後ろから1文字目 for c1 in range(4): # 後ろから2文字目 for c2 in range(4): # 後ろから3文字目 for c3 in r...
# DPをつかった解放を全然思いつけなかった # 解説動画がわかりやすい mod = 1000000007 N = int(eval(input())) dp = [ [[[0 for c3 in range(4)] for c2 in range(4)] for c1 in range(4)] for n in range(N+1)] # 全部TのときはOK dp[0][3][3][3] = 1 # 文字の数 for n in range(N): # 後ろから1文字目 for c1 in range(4): # 後ろから2文字目 for c2 in range(4...
p03088
n=int(eval(input())) A,C,G,T,M=0,1,2,3,10**9+7 r=range dp=[[[[0]*4for k in r(4)]for j in r(4)]for i in r(n+1)] dp[0][T][T][T]=1 for i in r(1,n+1): for j in r(4): for k in r(4): for l in r(4): for m in r(4): if any(t==(k,l,m)for t in((A,G,C),(A,C,G),(G,A,C)))or(j,l,m)==(A,G,C)or(j...
n=int(eval(input())) A,C,G,T,M,R=0,1,2,3,10**9+7,list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][T][T][T]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if not(any(t==(k,l,m)for t in((A,G,C),(A,C,G),(G,A,C)))or(j,l,m)==(A,G,C)or...
p03088
n=int(eval(input())) A,C,G,T,M,R=0,1,2,3,10**9+7,list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][T][T][T]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if not(any(t==(k,l,m)for t in((A,G,C),(A,C,G),(G,A,C)))or(j,l,m)==(A,G,C)or...
print(((388130742,597534442,616680192,329869205,170591295,333099663,133009077,738390145,866398347,718889563,252858375,847205021,915711339,123820509,940737264,371653459,323137024,353770801,206282374,847501730,25328739,842176273,569725155,781042769,427196161,570246669,191483611,673385295,324759692,245601370,975058685,406...
p03088
n=int(eval(input())) A,C,G,T,M,R=0,1,2,3,10**9+7,list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][T][T][T]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if(G,A,C)!=(k,l,m)!=(A,C,G)!=(k,l,m)!=(A,G,C)!=(j,l,m)!=(A,G,C)!=(j,k,m):dp...
n=int(eval(input())) M,R=10**9+7,list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][3][3][3]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if(2,0,1)!=(k,l,m)!=(0,1,2)!=(k,l,m)!=(0,2,1)!=(j,l,m)!=(0,2,1)!=(j,k,m):dp[i][k][l][m]=(dp...
p03088
n=int(eval(input())) M,R=10**9+7,list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][3][3][3]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if(2,0,1)!=(k,l,m)!=(0,1,2)!=(k,l,m)!=(0,2,1)!=(j,l,m)!=(0,2,1)!=(j,k,m):dp[i][k][l][m]=(dp...
n=int(eval(input())) R=list(range(4)) dp=[[[[0]*4for k in R]for j in R]for i in range(n+1)] dp[0][3][3][3]=1 for i in range(1,n+1): for j in R: for k in R: for l in R: for m in R: if(2,0,1)!=(k,l,m)!=(0,1,2)!=(k,l,m)!=(0,2,1)!=(j,l,m)!=(0,2,1)!=(j,k,m):dp[i][k][l][m]+=dp[i-1][j][k...
p03088
import re,functools as f @f.lru_cache(None) def d(n,b):return n<1or sum(d(n-1,b[1:]+s)for s in'ACGT'if not re.match('.AGC|.ACG|.GAC|A.GC|AG.C',b+s))%(10**9+7) print((d(int(eval(input())),'TTT')))
import re,functools as f;d=f.lru_cache(None)(lambda n,b:n<1or sum(d(n-1,b[1:]+s)for s in'ACGT'if not re.match('.AGC|.ACG|.GAC|A.GC|AG.C',b+s))%(10**9+7));print((d(int(eval(input())),'TTT')))
p03088
import re,functools as f;d=f.lru_cache(999)(lambda n,b:n<1or sum(d(n-1,b[1:]+s)for s in'ACGT'if not re.match('.AGC|.ACG|.GAC|A.GC|AG.C',b+s))%(10**9+7));print((d(int(eval(input())),'TTT')))
a=b=c=d=e=f=0;g=1;exec('a,b,c,d,e,f,g=b,e,e-d,f,g,g-c,(g*4-b-c-d-a*3)%(10**9+7);'*int(eval(input())));print(g)
p03088
N = int(eval(input())) if N==3: print((61)) exit() MOD = 10**9+7 dp = [[0]*64 for i in range(N-2)] A,C,G,T = 0,1,2,3 for i in range(4): for j in range(4): for k in range(4): if i==A and j==G and k==C: continue if i==A and j==C and k==G: continue if...
N = int(eval(input())) dp = [[[[0]*4 for _ in range(4)] for _ in range(4)] for _ in range(N-2)] for i in range(4): for j in range(4): for k in range(4): if i==0 and j==1 and k==2: continue #AGC if i==0 and j==2 and k==1: continue #ACG if i==1 and j==0 and k==2: con...
p03088
n = int(eval(input())) MOD = 10 ** 9 + 7 dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(n)] for a in range(4): for b in range(4): for c in range(4): dp[2][a][b][c] = 1 dp[2][0][1][2] = 0 dp[2][0][2][1] = 0 dp[2][1][0][2] = 0 for i in range(...
n = int(eval(input())) ans = [0, 0, 0, 61, 230, 865, 3247, 12185, 45719, 171531, 643550, 2414454, 9058467, 33985227, 127504505, 478366600, 794717734, 733354121, 261943303, 776803305, 580025381, 51688048, 44657419, 737209731, 604119499, 159693437, 858533109, 639056669, 549054109, 996291083, 531294469, 23314687, 78302...
p03088
import functools N, MOD = int(eval(input())), 10 ** 9 + 7 def ok(last4): for i in range(4): t = list(last4) if i >= 1: t[i-1], t[i] = t[i], t[i-1] if ''.join(t).count('AGC') >= 1: return False return True @functools.lru_cache(maxsize=None) d...
import functools N, MOD = int(eval(input())), 10 ** 9 + 7 @functools.lru_cache(maxsize=None) def ok(last4): for i in range(4): t = list(last4) if i >= 1: t[i-1], t[i] = t[i], t[i-1] if ''.join(t).count('AGC') >= 1: return False return True @...
p03088
import collections, itertools N = int(eval(input())) mod = 10**9 + 7 cd = collections.defaultdict dp = cd(lambda: cd(lambda: cd(lambda: cd(int)))) dp[0][3][3][3] = 1 count = 0 for i in range(N): for c0, c1, c2, c3 in itertools.product(list(range(4)), repeat=4): A1 = [c1, c2, c3] A2 = [c0, ...
import collections, itertools N = int(eval(input())) mod = 10**9 + 7 # cd = collections.defaultdict # dp = cd(lambda: cd(lambda: cd(lambda: cd(int)))) dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N + 1)] dp[0][3][3][3] = 1 count = 0 for i in range(N): for c0, c1...
p03088
import collections, itertools N = int(eval(input())) mod = 10**9 + 7 dp = collections.defaultdict(int) dp['TTTA'] = dp['TTTG'] = dp['TTTC'] = dp['TTTT'] = 1 for _ in range(N - 1): dp2 = collections.defaultdict(int) for p, q, r, s, t in itertools.product('AGCT', repeat=5): if 'AGC' in [q + s + t,...
import collections, itertools N = int(eval(input())) mod = 10**9 + 7 dp = collections.defaultdict(int) dp['TTTA'] = dp['TTTG'] = dp['TTTC'] = dp['TTTT'] = 1 for _ in range(N - 1): dp2 = collections.defaultdict(int) for p, q, r, s, t in itertools.product('AGCT', repeat=5): if 'AGC' in [q + s + t,...
p03088
from itertools import product from copy import deepcopy n = int(eval(input())) mod = 10 ** 9 + 7 comp = {"A": 0, "C": 1, "G": 2, "T": 3} dp = [[0] * (4 ** 3) for _ in range(n + 1)] bad = [0b001001, 0b000110, 0b100001] for i in range(4 ** 3): if not i in bad: dp[3][i] = 1 def is_ok(li):...
from itertools import product from copy import deepcopy n = int(eval(input())) mod = 10 ** 9 + 7 comp = {"A": 0, "C": 1, "G": 2, "T": 3} keys = list(comp.keys()) dp = [[0] * (4 ** 3) for _ in range(n + 1)] bad = [0b001001, 0b000110, 0b100001] for i in range(4 ** 3): if not i in bad: dp[3][...
p03088
from itertools import product from collections import defaultdict MOD = 10**9 + 7 N = int(eval(input())) # XAGC, XGAC, AXGC, AGXC, XACG: prohibited A=0; C=1; G=2; T=3 cur = defaultdict(lambda: 1) cur[(A,G,C)] = cur[(G,A,C)] = cur[(A,C,G)] = 0 for _ in range(3,N): prev = cur cur = defaultdict(int) ...
from itertools import product from collections import defaultdict MOD = 10**9 + 7 N = int(eval(input())) D, L = 4, 3 AGC = list(range(L)) ATGC = list(range(D)) cur = defaultdict(lambda: 1) # XAGC, XGAC, AXGC, AGXC, XACG: prohibited flip = lambda x, n: x[:n] + [x[n+1],x[n]] + x[n+2:] prohibited1 = [tuple(AGC)]...
p03088
def main(): atgc = ["A", "T", "G", "C"] mod = 10 ** 9 + 7 n = int(eval(input())) dp = [[[[0 for i3 in range(4)] for i2 in range(4)] for i1 in range(4)] for i0 in range(n)] for i in range(4): for j in range(4): for k in range(4): dp[0][i][j][k] = 1 ...
import itertools def main(): atgc = ["A", "T", "G", "C"] mod = 10 ** 9 + 7 n = int(eval(input())) dp = [[[[0 for i3 in range(4)] for i2 in range(4)] for i1 in range(4)] for i0 in range(n)] for i, j, k in itertools.product(list(range(4)), list(range(4)), list(range(4))): dp[0][i]...
p03088
n = int(eval(input())) ans = 0 hoges = [] seeds = 'ATCG' def ng(currents): v = 'AGC' in currents # for k in range(max(1, 0), len(currents)): for k in range(max(1, len(currents) - 3), len(currents)): v = v or 'AGC' in (currents[:k-1] + currents[k] + currents[k-1] + currents[k+1:]) return v memo = {...
n = int(eval(input())) seeds = 'ATCG' def ng(currents): v = 'AGC' in currents for k in range(max(1, len(currents) - 3), len(currents)): # 先頭の方はチェック済み v = v or 'AGC' in (currents[:k-1] + currents[k] + currents[k-1] + currents[k+1:]) return v memo = {} # 今の長さと最新4つだけメモする def f(index, currents): ke...
p03088
Mod = 10**9+7 p = ["XAGC", "XGAC", "XACG"] + ["AGCX", "GACX", "ACGX"] + ["AGXC", "AXGC"] q = ["A", "G", "C", "T"] f = [] for s in p: for c in q: f.append(s.replace("X",c)) r = ["AGC", "GAC", "ACG"] d = {} e = {} for x in q: for y in q: for z in q: s = x+y+z if not s in r: ...
Mod = 10**9+7 f3 = ["AGC", "GAC", "ACG"] f4 = ["AGAC","AGGC","AGCC","AGTC"] + ["AAGC","AGGC","ACGC","ATGC"] q = ["A", "G", "C", "T"] d = {} e = {} for x in q: for y in q: for z in q: s = x+y+z if not s in f3: d[s] = 1 e[s] = 0 n = int(eval(input())) for i in ran...
p03088
N = int(input().strip()) M = pow(10, 9) + 7 a = 1 g = 1 ag = 0 agg = 0 agt = 0 atg = 0 ac = 0 at = 0 aa = 0 ga = 0 al = 4 nal = 0 for i in range(N-1): nal = (al * 4) % M nal = (nal + M - (ag + ac + ga + agg + agt + atg) ) % M agg = ag agt = ag atg = at ag = a aa = ...
a,g,a_,a__,ac,ga,al=[0]*6+[1] for i in range(int(eval(input()))): a__,a_,ac,ga,a,g,al=a_,a,a-ga,g,al,al-ac,(al*4-(a_+ac+ga+a__*3))%(pow(10,9)+7) print(al)
p03088
n, mod = int(eval(input())), 10 ** 9 + 7 memo = [{} for i in range(n+1)] def check(l): for i in range(4): t = list(l) if i >= 1: t[i-1], t[i] = t[i], t[i-1] # 順序の入れ替え if ''.join(t).count('AGC') >= 1: return False return True def dfs(cur, last3): i...
""" n, mod = int(input()), 10 ** 9 + 7 memo = [{} for i in range(n+1)] def check(l): for i in range(4): t = list(l) if i >= 1: t[i-1], t[i] = t[i], t[i-1] # 順序の入れ替え if ''.join(t).count('AGC') >= 1: return False return True def dfs(cur, last3): if...
p03088
import sys from collections import defaultdict read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N = int(readline()) dp = [defaultdict(int) for _ in range(N)] for c in 'ACGT': ...
import sys from collections import defaultdict read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): N = int(readline()) dp = [defaultdict(int) for _ in range(N)] for c in 'ACGT': ...
p03088
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 6) import math from collections import Counter from collections import deque from operator import itemgetter import time, random MOD = 10 ** 9 + 7 from itertools import permutations def dfs(s): if len(...
import sys readline = sys.stdin.readline MOD = 10 ** 9 + 7 def main(): N = int(readline()) A, G, C, T = 0, 1, 2, 3 dp = [[[[0] * 4 for _ in range(4)] for _ in range(4)] for _ in range(N+1)] for j in range(4): # i - 2 文字目 for k in range(4): # i - 1 文字目 for l in range(4): ...
p03088
n = int(eval(input())) dp = [[0]*4 for i in range(n+1)] mod = 10**9+7 dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = 1 dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = 4 for i in range(2,n): for j in range(4): dp[i][j] = sum(dp[i-1])%mod dp[i][1] -= dp[i-2][0] dp[i][2] -= (dp[i-2][0] + dp[i-2][...
n = int(eval(input())) mod = 10**9 + 7 a = [0]*n c = [0]*n g = [0]*n t = [0]*n a[0] = c[0] = g[0] = t[0] = 1 a[1] = c[1] = g[1] = t[1] = 4 a[2], c[2], g[2], t[2] = 16, 14, 15, 16 for i in range(3, n): v = (a[i-1] + c[i-1] + g[i-1] + t[i-1]) % mod a[i] = v c[i] = (v - a[i-2] - g[i-2] - 3*...
p03088
#################### #################### #################### #################### from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>=r:s+=r;x+=[[k,l-r]] else:s+=l;y+=[[n-k,r-l]]*(k<n) for x in x,y: x.sort();h=[] for k,d ...
#################### #################### #################### #################### from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>=r:x+=[[k,l,r]] else:y+=[[n-k,r,l]] for x in x,y: x.sort();h=[] for k,l,r in x: if k...
p02610
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: x.sort();h=[] for k,l,r in x: s+=r if k:s+=l-r;heappush(h,l-r) if len(h)>k:s-=heappop(h) print(s)
from heapq import* i=input for s in[0]*int(i()): n,*x=int(i()),[],[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x[l>r]+=[[[n-k,r,l],t][l>r]] for x in x: h=[] for k,l,r in sorted(x):heappush(h,l-r);s+=l-(k<len(h)and heappop(h)) print(s)
p02610
from collections import deque from heapq import heappush, heappushpop import sys input = sys.stdin.readline def calc(camels): camels = deque(sorted(camels, key=lambda x: x[0])) N = len(camels) heap = [] while camels and camels[0][0] == 0: camels.popleft() for i in range(1, N+1): ...
from heapq import heappush, heappop import sys input = sys.stdin.readline def calc(camels, N): camels.sort(key=lambda x: x[0]) heap = [] for i, x in camels: heappush(heap, x) if len(heap) > i: heappop(heap) return sum(heap) T = int(eval(input())) for _ in rang...
p02610
import heapq T=int(eval(input())) for _ in range(T): N=int(eval(input())) llist=[] rlist=[] base=0 for _ in range(N): K,L,R=list(map(int,input().split())) base+=min(L,R) if L>R: llist.append((K,L-R)) elif L<R: rlist.append((N-K,R-L)) llist.sort() rl...
import heapq T=int(eval(input())) for _ in range(T): N=int(eval(input())) llist=[] rlist=[] base=0 for _ in range(N): K,L,R=list(map(int,input().split())) if L>R: llist.append((K,L-R)) base+=R elif L<R: rlist.append((N-K,R-L)) base+=L else: ...
p02610
def main(): from collections import deque import sys input = sys.stdin.readline T = int(eval(input())) def res_1(lis,ans): flag = [False] * (N) for klr in lis: x = klr[0] for _ in range(N): if x<0: ans += klr[2] break if flag[x]: ...
def main(): from collections import deque import sys input = sys.stdin.readline from heapq import heapify,heappop,heappush T = int(eval(input())) for _ in range(T): N = int(eval(input())) KLR = [list(map(int,input().split())) for _ in range(N)] left = deque([]) right = deque([]) ...
p02610
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): r...
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): r...
p02610
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): r...
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): r...
p02610
from heapq import* i=input s=sorted def f(x): s,n,*h=0,len(x) while n: while x and x[-1][0]>=n:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[1] n-=1 return s+sum(r for _,_,r in x+h) for _ in'_'*int(i()): n,x,*y=int(i()),[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>r:x+=(...
from heapq import* i=input s=sorted def f(x): s,n,*h=0,len(x) while n: while x and x[-1][0]>=n:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[1] n-=1 return s+sum(r for*_,r in x+h) for _ in'_'*int(i()): n,x,*y=int(i()),[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>r:x+=(k,...
p02610
from heapq import* i=input def f(x): x.sort();s,n,*h=0,len(x) while n: n-=1 while x and x[-1][0]>n:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[1] return s+sum(r for*_,r in x+h) for _ in'_'*int(i()): n,x,*y=int(i()),[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>r:x+=(k,l,...
from heapq import* i=input for _ in'_'*int(i()): n,s,x,*y=int(i()),0,[] for _ in'_'*n: k,l,r=list(map(int,i().split())) if l>r:x+=(k,l,r), else:y+=(n-k,r,l), for x in(x,y): x.sort();n,*h=len(x), while n: n-=1 while x and x[-1][0]>n:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h...
p02610
from heapq import* i=input for _ in'_'*int(i()): n,s,x,*y=int(i()),0,[];exec('k,l,r=t=[*map(int,i().split())];x+=[t]*(l>r);y+=[(n-k,r,l)]*(l<=r);'*n) for x in x,y: x.sort();n,*h=len(x), while n: n-=1 while x and x[-1][0]>n:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[1] for*_,r in x...
from heapq import* i=input for _ in'_'*int(i()): n,s,x,*y=int(i()),0,[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: x.sort();n,*h=len(x), while n: while[[n,0,0]]<x[-1:]:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[1] n-=1 fo...
p02610
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: x.sort();n,*h=len(x), while h+x: while[[n]]<x[-1:]:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[2-(n>0)] n-=1 pri...
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: x.sort();n,*h=len(x), while h or x: while[[n]]<x[-1:]:k,l,r=x.pop();heappush(h,(r-l,l,r)) if h:s+=heappop(h)[~(n>0)] n-=1 p...
p02610
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: x.sort();n=len(x);h=[(0,0)]*n*2 while h: while[[n]]<x[-1:]:k,l,r=x.pop();heappush(h,(r-l,l,r)) s+=heappop(h)[~(n>0)];n-=1 print...
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y: h=[] for k,l,r in sorted(x): s+=r if k:s+=l-r;heappush(h,l-r) if len(h)>k:s-=heappop(h) print(s)
p02610
from heapq import* i=input for s in[0]*int(i()): n,x,*y=int(i()),[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r) for x in x,y:h=[];s+=sum(heappush(h,l-r)or l-(k<len(h)and heappop(h))for k,l,r in sorted(x)) print(s)
from heapq import* i=input for s in[0]*int(i()): n,*x=int(i()),[],[] for _ in'_'*n:k,l,r=t=[*list(map(int,i().split()))];x[l>r]+=[[n-k,r,l],t][l>r], for x in x: h=[] for k,l,r in sorted(x):heappush(h,l-r);s+=l-(k<len(h)and heappop(h)) print(s)
p02610
# point update # range query class SegmentTreeNode: def __init__(self, start, end, initial, merge): # print("s", start, end) self.start = start self.end = end self.width = end - start self.merge = merge self.initial = initial if self.width > 1: w2 = self.width // 2 self.left = Segmen...
T = int(eval(input())) import heapq def calc(camels): camels.sort() camels.reverse() count = len(camels) score = 0 queue = [] i = 0 for k in range(count, 0, -1): while i < count and camels[i][0] >= k: K, L, R = camels[i] heapq.heappush(queue, (-(L - R), L, R)) i += 1 if len(queu...
p02610
import sys from heapq import heappush, heappop readline = sys.stdin.readline def solve(): N = int(readline()) left = [[] for _ in range(N)] right = [[] for _ in range(N)] ans = 0 for _ in range(N): K, L, R = list(map(int, readline().split())) if L > R: ...
import sys from heapq import heappush, heappop readline = sys.stdin.readline def solve(): N = int(readline()) left = [[] for _ in range(N)] right = [[] for _ in range(N)] ans = 0 for _ in range(N): K, L, R = list(map(int, readline().split())) if L > R: ...
p02610
import sys from heapq import heappush, heappop readline = sys.stdin.readline def solve(): N = int(readline()) left = [[] for _ in range(N)] right = [[] for _ in range(N)] ans = 0 for _ in range(N): K, L, R = list(map(int, readline().split())) if L > R: ...
import sys from heapq import heappush, heappop readline = sys.stdin.readline def solve(): N = int(readline()) left = [[] for _ in range(N)] right = [[] for _ in range(N)] ans = 0 for _ in range(N): K, L, R = list(map(int, readline().split())) if L > R: ...
p02610
n = eval(input()) numbers = input().split() print((' '.join(numbers[::-1])))
n = eval(input()) numbers = list(input().split()) print((" ".join(numbers[::-1])))
p02407
input() i = list(map(int, input().split())) i.reverse() for j in range(len(i)) : if j != 0 : print(' ', end = '') print(i[j], end = '') print()
eval(input()) data = input().split() data.reverse() print((' '.join(data)))
p02407
eval(input()) a = list(input().split()) a.reverse() print((" ".join(a)))
eval(input()) print((" ".join(reversed(input().split()))))
p02407
import sys n = int( sys.stdin.readline() ) nums = sys.stdin.readline().rstrip().split( " " ) nums.reverse() output = [] for i in range( n ): output.append( nums[i] ) if i < (n-1): output.append( " " ) print(( "".join( output ) ))
import sys n = int( sys.stdin.readline() ) nums = sys.stdin.readline().rstrip().split( " " ) nums.reverse() print(( " ".join( nums ) ))
p02407
n = int(input()) a = list(map(int,input().split())) a.reverse() for i in range(n-1): print(a[i],end = " ") print(a[n-1])
n =int(input()) a = list(map(int,input().split())) a.reverse() for i in range(n): if i!=n-1: print(a[i],end=" ") else: print(a[i])
p02407
n = int(input()) ls = list(map(int,input().split())) print(' '.join(map(str,ls[::-1])))
n = int(input()) print(' '.join(input().split()[::-1]))
p02407
#coding:utf-8 #1_6_A 2015.4.1 n = int(input()) numbers = list(map(int,input().split())) for i in range(n): if i == n - 1: print(numbers[-i-1]) else: print(numbers[-i-1], end = ' ')
#coding:utf-8 #1_6_A 2015.4.1 eval(input()) numbers = input().split() numbers.reverse() print((' '.join(numbers)))
p02407
n,k = list(map(int,input().split())) pList = list(map(int,input().split())) cList = list(map(int,input().split())) cSum = sum(cList) p = dict() c = dict() for i in range(n): p[i] = pList[i] c[i] = cList[i] highScore = min(cList) for i in range(n): count = 0 zahyou = i score = ...
n,k = list(map(int,input().split())) pList = list(map(int,input().split())) cList = list(map(int,input().split())) cSum = sum(cList) p = dict() c = dict() for i in range(n): p[i] = pList[i] c[i] = cList[i] highScore = min(cList) for i in range(n): count = 0 zahyou = i score = ...
p02585
import sys import copy input = sys.stdin.readline N,K=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) ans=-1*(10**10) for i in range(N): tmp=0 pos=i count=0 while count<K: #print(pos,P[pos]) tmp+=C[P[pos]-1] ...
import sys import copy input = sys.stdin.readline N,K=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) ans=-1*(10**10) if K<10000000: for i in range(N): tmp=0 pos=i count=0 while count<K: #print(pos,...
p02585
N, K = list(map(int, input().split())) P = list(map(int, input().split())) C = list(map(int, input().split())) INF = 10**9+1 def solve(): cycles = [] memo = [False] * N i = 0 while i < N-1: cycle = [] j = i while not memo[j]: cycle.append(j) ...
N, K = list(map(int, input().split())) P = list(map(int, input().split())) C = list(map(int, input().split())) INF = 10**9+1 def solve(): ans = -INF for i in range(N): memo = [0] * (N+1) j = 0 p = i while j < N: memo[j+1] = memo[j] + C[p] p...
p02585
N, K = list(map(int, input().split())) P = list(map(int, input().split())) P = [p - 1 for p in P] C = list(map(int, input().split())) vis = [False] * N ans = [-float('inf')] * N for root in range(N): if vis[root]: continue vis[root] = True cycle = [root] costs = [C[root]] u...
N, K = list(map(int, input().split())) P = list(map(int, input().split())) P = [p - 1 for p in P] C = list(map(int, input().split())) vis = [False] * N ans = [-float('inf')] * N for root in range(N): if vis[root]: continue vis[root] = True cycle = [root] costs = [C[root]] u...
p02585
N, K = list(map(int, input().split())) P = list(map(int, input().split())) P = [p - 1 for p in P] C = list(map(int, input().split())) vis = [False] * N ans = [-float('inf')] * N for root in range(N): if vis[root]: continue cycle = [root] costs = [C[root]] u = P[root] while ...
N, K = list(map(int, input().split())) P = list(map(int, input().split())) P = [p - 1 for p in P] C = list(map(int, input().split())) vis = [False] * N ans = [-float('inf')] * N for root in range(N): if vis[root]: continue cycle = [root] costs = [C[root]] u = P[root] while ...
p02585
import sys from collections import deque N, K = list(map(int, sys.stdin.readline().split())) P = list(map(int, sys.stdin.readline().split())) C = list(map(int, sys.stdin.readline().split())) # 重み付き有向グラフ # edges = [[] for _ in range(N)] # for i in range(N): # edges[i].append((P[i]-1, C[i])) # DFSで閉路と経...
import sys input = sys.stdin.readline N, K = list(map(int, input().split())) P = list(map(int, input().split())) C = list(map(int, input().split())) ans = -float('inf') for s in range(N): S = [] i = P[s] - 1 S.append(C[i]) while i != s: i = P[i] - 1 S.append(S[-1] + C[i...
p02585
import sys import itertools # import numpy as np import time import math import heapq from collections import defaultdict sys.setrecursionlimit(10 ** 7) INF = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split())...
import sys import itertools # import numpy as np import time import math import heapq from collections import defaultdict sys.setrecursionlimit(10 ** 7) INF = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split())...
p02585
def main(): from sys import setrecursionlimit, stdin, stderr from os import environ from collections import defaultdict, deque, Counter from math import ceil, floor from itertools import accumulate, combinations, combinations_with_replacement setrecursionlimit(10**6) dbg = (lambda *so...
def main(): from sys import setrecursionlimit, stdin, stderr from os import environ from collections import defaultdict, deque, Counter from math import ceil, floor from itertools import accumulate, combinations, combinations_with_replacement setrecursionlimit(10**6) dbg = (lambda *so...
p02585
import sys read = sys.stdin.read readlines = sys.stdin.readlines from itertools import accumulate from collections import deque, defaultdict def main(): n, ko = list(map(int, input().split())) p = tuple(map(int, input().split())) c = tuple(map(int, input().split())) scores_all = deque() ...
import sys read = sys.stdin.read readlines = sys.stdin.readlines from itertools import accumulate def main(): n, k = list(map(int, input().split())) p = tuple(map(int, input().split())) c = tuple(map(int, input().split())) if k == 1: print((max(c))) sys.exit() seen = [0]...
p02585
import sys sys.setrecursionlimit(10 ** 8) input = sys.stdin.readline def main(): N, K = [int(x) for x in input().split()] P = [int(x) for x in input().split()] C = [int(x) for x in input().split()] ans = -float("inf") for i in range(N): next = P[i] - 1 cnt = 0 ...
import sys sys.setrecursionlimit(10 ** 8) input = sys.stdin.readline def main(): N, K = [int(x) for x in input().split()] P = [int(x) - 1 for x in input().split()] C = [int(x) for x in input().split()] ans = max(C) for i in range(N): next = P[i] cnt = 0 t...
p02585
N, K = list(map(int,input().split())) P = [int(x)-1 for x in input().split()] C = [int(x) for x in input().split()] def loop(s): value = 0 done = set() while s not in done: done.add(s) s = P[s] value += C[s] return len(done), value def f(s,K): ans = -10**18 ...
N, K = list(map(int,input().split())) P = [int(x)-1 for x in input().split()] C = [int(x) for x in input().split()] def loop(s): value = 0 done = set() while s not in done: done.add(s) s = P[s] value += C[s] return len(done), value def f(s,K): ans = -10**18 ...
p02585
INF=10**9 f=lambda:[*list(map(int,input().split()))] n,k=f() p,c=f(),f() p=[*[x-1 for x in p]] tp=[[*list(range(n))] for _ in range(n+1)] tc=[[0]*n for _ in range(n+1)] lt=[0]*n lc=[0]*n for i in range(n): for j in range(n): t=p[tp[i][j]] tp[i+1][j]=t tc[i+1][j]=tc[i][j]+c[t] if t==j and...
f=lambda:[*list(map(int,input().split()))] n,k=f() p,c=f(),f() p=[*[x-1 for x in p]] a=-10**9 for i in range(n): x=i l=[] s=0 while 1: x=p[x] l+=[c[x]] s+=c[x] if x==i: break m=len(l) t=0 for j in range(m): if j+1>k: break t+=l[j] a=max(a,t+(k-j-1)//m*s*(s>0)...
p02585
N, K = list(map(int, input().split())) P = list(map(int, input().split())) C = list(map(int, input().split())) P = [None] + P C = [None] + C all_max = C[1] for st in range(1, N + 1): scores = [] visit = set() p = st while p not in visit and len(visit) < K: next_p = P[p] ...
N, K = list(map(int, input().split())) P = list(map(int, input().split())) C = list(map(int, input().split())) P = [None] + P C = [None] + C all_max = C[1] for st in range(1, N + 1): p = P[st] scores = [C[p]] while p != st and len(scores) < K: p = P[p] scores.append(C[p]) ...
p02585
n,k = list(map(int,input().split())) p = list(map(int,input().split())) for i in range(n): p[i] -= 1 c = list(map(int,input().split())) reach = [False]*n x = [] for i in range(n): if reach[i]:continue buf = [] cur = i while not reach[cur]: buf.append(c[cur]) reach[cur]...
n,k = list(map(int,input().split())) p = list(map(int,input().split())) for i in range(n): p[i] -= 1 c = list(map(int,input().split())) reach = [False]*n x = [] for i in range(n): if reach[i]:continue buf = [] cur = i while not reach[cur]: buf.append(c[cur]) reach[cur]...
p02585
n, k = list(map(int, input().split())) p = [0] + list(map(int, input().split())) c = [0] + list(map(int, input().split())) ans = - 10 ** 18 for i in range(1, n+1): base = i nxt = p[i] cnt = 0 score = 0 while True: cnt += 1 if cnt > k: break score +=...
n, k = list(map(int, input().split())) p = [0] + list(map(int, input().split())) c = [0] + list(map(int, input().split())) ans = - 10 ** 18 for i in range(1, n+1): base = i nxt = p[i] cnt = 0 score = 0 while True: cnt += 1 if cnt > k: break score +=...
p02585
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) mv_array = [set() for _ in range(n)] score_mv_array = [[] for _ in range(n)] score_array = [0]*n def get_sum(array): score_array = [0] for i, a in enumerate(array): score_array.app...
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) # mv_array = [set() for _ in range(n)] score_mv_array = [[] for _ in range(n)] score_array = [0]*n def get_sum(array): score_array = [0] for i, a in enumerate(array): score_array.a...
p02585
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) # mv_array = [set() for _ in range(n)] # score_mv_array = [[] for _ in range(n)] score_array = [0]*n def get_sum(array): score_array = [0] for i, a in enumerate(array): score_array...
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) # mv_array = [set() for _ in range(n)] # score_mv_array = [[] for _ in range(n)] # score_array = [0]*n def get_sum(array): score_array = [0] for i, a in enumerate(array): score_arr...
p02585
''' 自宅用PCでの解答 ''' import math #import numpy as np import itertools import queue import bisect from collections import deque,defaultdict import heapq as hpq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecurs...
''' 自宅用PCでの解答 ''' import math #import numpy as np import itertools import queue import bisect from collections import deque,defaultdict import heapq as hpq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecurs...
p02585
import copy import math import time import statistics import math import itertools import bisect from decimal import * # a = get_int() def get_int(): return int(eval(input())) # a = get_string() def get_string(): return eval(input()) # a_list = get_int_list() def get_int_list(): return [in...
import copy import math import time import statistics import math import itertools import bisect from decimal import * # a = get_int() def get_int(): return int(eval(input())) # a = get_string() def get_string(): return eval(input()) # a_list = get_int_list() def get_int_list(): ...
p02585
import sys input = sys.stdin.readline #from itertools import accumulate #from itertools import combinations N,K = list(map(int,input().split())) P = list(map(int,input().split())) C = list(map(int,input().split())) #N,K = 4,4 #P = [2,3,4,1] #C = [-9,9,1,-1] visited = [False]*(N+1) loops = [] for v in ra...
import sys input = sys.stdin.readline #from itertools import accumulate #from itertools import combinations N,K = list(map(int,input().split())) P = list(map(int,input().split())) C = list(map(int,input().split())) #N,K = 4,4 #P = [2,3,4,1] #C = [-9,9,1,-1] #N = 5000 #K = 10**8 #P = [i+1 for i in range(1,...
p02585
import sys sys.setrecursionlimit(10**7) readline = sys.stdin.buffer.readline def readstr():return readline().rstrip().decode() def readstrs():return list(readline().decode().split()) def readint():return int(readline()) def readints():return list(map(int,readline().split())) def printrows(x):print(('\n'.join(m...
import sys sys.setrecursionlimit(10**7) readline = sys.stdin.buffer.readline def readstr():return readline().rstrip().decode() def readstrs():return list(readline().decode().split()) def readint():return int(readline()) def readints():return list(map(int,readline().split())) def printrows(x):print(('\n'.join(m...
p02585
from collections import defaultdict INF = float('inf') N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = -INF for init in range(1, N + 1): # 初めの場所をiとする score = defaultdict(int) # int/bool/list.... visited = [...
from collections import defaultdict N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = max(C[1:]) for init in range(1, N + 1): # 初めの場所をiとする score = defaultdict(int) # int/bool/list.... visited = [-1] * (N + 1...
p02585
from collections import defaultdict N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = max(C[1:]) for init in range(1, N + 1): # 初めの場所をiとする score = defaultdict(int) # int/bool/list.... visited = [-1] * (N + 1...
N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = max(C[1:]) for init in range(1, N + 1): # 初めの場所をinitとする score = [0] # k回移動後のスコア i = init for k in range(1, K + 1): i = P[i] # k回移動後に着くところ ...
p02585
N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = max(C[1:]) for init in range(1, N + 1): # 初めの場所をinitとする score = [0] # k回移動後のスコア i = init for k in range(1, K + 1): i = P[i] # k回移動後に着くところ ...
N, K = [int(x) for x in input().split()] P = [0] + [int(x) for x in input().split()] C = [0] + [int(x) for x in input().split()] max_score = max(C[1:]) for init in range(1, N + 1): # 初めの場所をinitとする score = [0] # k回移動後のスコア i = init for k in range(1, K + 1): i = P[i] # k回移動後に着くところ ...
p02585
n, k = list(map(int,input().split())) p = list(map(int,input().split())) c = list(map(int,input().split())) ans = -float('inf') for i in range(n): score = [] check = [0] * n now = i cycle = 0 for _ in range(k): go = p[now] - 1 if check[go] == 1: break ...
n, k = list(map(int,input().split())) p = list(map(int,input().split())) c = list(map(int,input().split())) ans = -float('inf') for i in range(n): score = [] check = [0] * n now = i cycle = 0 for _ in range(k): go = p[now] - 1 if check[go] == 1: break ...
p02585
n, k = list(map(int, input().split())) # 先頭に0番要素を追加すれば、配列の要素番号と入力pが一致する p = [0] + list(map(int, input().split())) c = [0] + list(map(int, input().split())) ans = -1e9*1e9 s = [None] * n # 開始ノードをsi for si in range(1, n+1): # 閉ループのスコアを取り出す x = si #s = list() # ノードの各スコアを保存 s_len = 0 to...
n, k = list(map(int, input().split())) # 先頭に0番要素を追加すれば、配列の要素番号と入力pが一致する p = [0] + list(map(int, input().split())) c = [0] + list(map(int, input().split())) ans = -float('inf') # 開始ノードをsi for si in range(1, n+1): # 閉ループのスコアを取り出す x = si s = list() # ノードの各スコアを保存 #s_len = 0 tot = 0 #...
p02585
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**6) INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 10 ** 9 + 7 def debug(*x): print(*x, file=sys.stderr) def solve0(N, K, PS, CS): PS = [x - 1 for x in PS] debug(": PS", PS) CS = [CS[PS[i]] for i in range(N)] PSS = ...
#!/usr/bin/env python3 import sys from collections import defaultdict sys.setrecursionlimit(10**6) INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 10 ** 9 + 7 def debug(*x): print(*x, file=sys.stderr) def solve(N, K, PS, CS): PS = [x - 1 for x in PS] CS = [CS[PS[i]] for i in range(...
p02585
n, k = list(map(int, input().split())) p = list(map(int, input().split())) p = [x-1 for x in p] c = list(map(int, input().split())) visited = [False for _ in range(n)] lp = [] lp_cum = [] lp_sum = [] range_s = [] for i in range(n): if not visited[i]: lp.append([i]) lp_cum.append([0, c[i]]) visited...
n, k = list(map(int, input().split())) p = list(map(int, input().split())) p = [x-1 for x in p] c = list(map(int, input().split())) visited = [False for _ in range(n)] lp = [] lp_cum = [] ans = -10**9 for i in range(n): if not visited[i]: lp.append([i]) lp_cum.append([0, c[i]]) visited[i] = True ...
p02585
N,K=list(map(int,input().split())) P=list([int(x)-1 for x in input().split()]) C=list(map(int,input().split())) dp = [] init = 0 if K<=N: ans = set() for i in range(N): tmp,now,last = [0],i,0 for _ in range(K): last += C[P[now]] ans.add(last) now=P[now] print((max(ans)))...
N,K=list(map(int,input().split())) P=list([int(x)-1 for x in input().split()]) C=list(map(int,input().split())) if K<=N: ans = -10**9 for i in range(N): now,last = i,0 for _ in range(K): last += C[P[now]] ans = max(ans,last) now=P[now] print(ans) exit() ans = -10**9 for...
p02585
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) for i in range(n): p[i] -= 1 def score(a, k): now = p[a] s = c[now] cnt = 1 l = [s] while now != a and cnt < k: now = p[now] s += c[now] l.append(...
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) for i in range(n): p[i] -= 1 def score(a, k): now = p[a] s = c[now] cnt = 1 l = [s] while now != a and cnt < k: now = p[now] s += c[now] l.append(...
p02585
n,k=list(map(int,input().split())) p=list([int(x)-1 for x in input().split()]) c=list(map(int,input().split())) ans=-10**20 for i in range(n): x=[] for _ in range(k): i=p[i] x.append(c[i]) anss=-10**20 s=x[0] anss=max(anss,s) for i in x[1:]: s+=i anss=max(anss,s) ans=max(ans...
def solve(n,x,k): ans=-10**20 s=0 sx=sum(x) for i in range(n): if (i+1)>k:break s+=x[i] d=((k-i-1)//n)*sx ans=max(ans,s+d,s) return ans n,k=list(map(int,input().split())) p=list([int(x)-1 for x in input().split()]) c=list(map(int,input().split())) ans=-10**20 for i in range(n):...
p02585
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemge...
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemge...
p02585
n , k = list(map(int,input().split())) p = list(map(int,input().split())) c = list(map(int,input().split())) loop = [] visit = [False for i in range(n)] for i in range(n): if visit[i]: continue tyu = [i] now = i visit[i] = True while True: nex = p[now] - 1 if ne...
n , k = list(map(int,input().split())) p = list(map(int,input().split())) c = list(map(int,input().split())) loop = [] visit = [(-1,-1) for i in range(n)] cou = -1 for i in range(n): if visit[i] != (-1,-1): continue cou += 1 tyu = [i] now = i visit[i] = (cou,0) t = 1 w...
p02585
# from pathlib import Path """ 解説動画のコードを参考にしました https://atcoder.jp/contests/abc175/submissions/15945829 """ stdin = iter( list(map(int, l.split())) for l in # Path('./test/sample-4.in').read_text().split('\n') open(0).read().split('\n') ) N,K = next(stdin) *P, = next(stdin) *C, = next(st...
""" 解説動画のコードを参考にしました https://atcoder.jp/contests/abc175/submissions/15945829 """ N,K = list(map(int, input().split())) *P, = list(map(int, input().split())) *C, = list(map(int, input().split())) ans = int(-1e18) for start in P: pos = start scores = [] # スコア格納用 while True: pos = P...
p02585
from itertools import accumulate def solve(n, k, ppp, ccc): NINF = -(10 ** 18) ans = NINF checked = [False] * n for s in range(n): if checked[s] == True: continue checked[s] = True scores = [ccc[ppp[s]]] starts = [s] v = ppp[s] ...
from itertools import accumulate def solve(n, k, ppp, ccc): NINF = -(10 ** 18) ans = NINF checked = [False] * n for s in range(n): if checked[s] == True: continue checked[s] = True scores = [ccc[ppp[s]]] v = ppp[s] while v != s: ...
p02585
n,k=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) m=min(C) score_max=m for L in range(n): score=0 d=[0]*n r=0 d[L]=1 for i in range(k): r+=1 L=P[L]-1 score+=C[L] score_max=max(score,score_max) if d[L]==0: d[L]=1 ...
n,k=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) score_max=max(C) #スタート地点=L for L in range(n): score=0 d=[0]*n r=0 d[L]=1 #k回移動する for _ in range(k): r+=1#移動回数をカウント L=P[L]-1#移動先の位置 score+=C[L] score_max=max(score,score_max) ...
p02585
n,k=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) score_max=max(C) #スタート地点=L for L in range(n): score=0 d=[0]*n r=0 d[L]=1 #k回移動する for _ in range(k): r+=1#移動回数をカウント L=P[L]-1#移動先の位置 score+=C[L] score_max=max(score,score_max) ...
n,k=list(map(int,input().split())) P=list(map(int,input().split())) C=list(map(int,input().split())) score_max=max(C) #スタート地点=L for L in range(n): score=0 d=[0]*n r=0 d[L]=1 #k回移動する for _ in range(k): r+=1#移動回数をカウント L=P[L]-1#移動先の位置 score+=C[L] score_max=max(score,score_max) ...
p02585
def li(): return [int(x) for x in input().split()] N, K = li() P = [0] + li() C = [0] + li() max_scores = [-10**12] * (N+10) for init_i in range(1, N+1): i = init_i scores = [0] for l in range(1, K+1): i = P[i] score = scores[l-1] + C[i] scores.append(score) ...
def li(): return [int(x) for x in input().split()] N, K = li() P = [0] + li() C = [0] + li() def get_cycle(start_i): cycle = [] i = start_i while True: i = P[i] if i == start_i: break cycle.append(i) cycle.append(start_i) return cycle def ...
p02585
N, K = list(map(int, input().split())) P, C = [list(map(int, input().split())) for _ in range(2)] c = [1] * N m = [[0] * (N+1) for i in range(N)] for i in range(N): m[i][1] = (C[P[i]-1]) ans = max(C) for i in range(N): j = i while P[j]-1 != i: j = P[j]-1 c[i] += 1 m[i][c[...
N, K = list(map(int, input().split())) P, C = [list(map(int, input().split())) for _ in range(2)] ans = max(C) for i in range(N): m = [0] * (N+1) k = i for j in range(N): m[j+1] = m[j] + C[k] k = P[k]-1 if k == i: break if m[j+1] > 0: ans = max(ans...
p02585
# O(EV) def bellman_ford(s): d = [0]*n # 各頂点への最小コスト d[s] = 0 # 自身への距離は0 x=s for tt in range(k): update = False # 更新が行われたか aa,bb,cc=g[x][0],g[x][1],g[x][2] if d[bb] < d[aa] + cc: d[bb] = d[aa] + cc update = True if n...
n,k=list(map(int,input().split())) p=list(map(int,input().split())) c=list(map(int,input().split())) ans=float('inf')*-1 for i in range(n): visited=[i] pin=0 now=i for j in range(k): now=p[now]-1 pin+=c[now] ans=max(ans,pin) visited+=[now] if vi...
p02585
n,k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) def sim(start,mx): # simulate mx step from start, returns max_score if mx==0: return 0 now_p=0 nxt=p[start]-1 cnt=0 p_lis=[] while True: nxt=p[nxt]-1 ...
n,k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) def sim(start,mx): # simulate mx step from start, returns max_score if mx==0: return 0 now_p=0 nxt=p[start]-1 cnt=0 p_lis=[] while True: nxt=p[nxt]-1 ...
p02585
def cycle_getter(N, start): """ :param N: 移動回数 :param start: 初期条件 :return front: cycleまでの要素のリスト cycle: cycle内の要素のリスト end: cycle後の余った部分の要素のリスト cnt: cycle回数 """ p = start front, cycle, end = [], [], [] cnt = 0 visit = {p:0} L, R = N,...
def cycle_getter(N, start): """ :param N: 移動回数 :param start: 初期条件 :return front: cycleまでの要素のリスト cycle: cycle内の要素のリスト end: cycle後の余った部分の要素のリスト cnt: cycle回数 """ p = start front, cycle, end = [], [], [] cnt = 0 visit = {p:0} L, R = N,...
p02585
N,K = list(map(int,input().split())) P = list(map(int,input().split())) C = list(map(int,input().split())) # グルーピング import copy P2 = copy.copy(P) Group = [] while len(P2)>0: temp = P2[0] tempGroup = [] while temp in P2: tempGroup.append(temp) P2.remove(temp) temp = P[t...
def main(): N,K = list(map(int,input().split())) P = list(map(int,input().split())) C = list(map(int,input().split())) # グルーピング import copy P2 = set(P) Group = [] while len(P2)>0: temp = list(P2)[0] tempGroup = [] while temp in P2: temp...
p02585
import sys import resource sys.setrecursionlimit(10000) n,k=list(map(int,input().rstrip().split())) p=list(map(int,input().rstrip().split())) c=list(map(int,input().rstrip().split())) def find(start,now,up,max,sum,count,flag): if start==now: flag+=1 if start==now and flag==2: return [m...
import sys import resource sys.setrecursionlimit(10000) n,k=list(map(int,input().rstrip().split())) p=list(map(int,input().rstrip().split())) c=list(map(int,input().rstrip().split())) def find(start,now,up,max,sum,count,flag): if start==now: flag+=1 if start==now and flag==2: return [m...
p02585
#!/usr/bin/env python3 import sys from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union # import time # import math, cmath # import numpy as np # import scipy.sparse.csgraph as cs # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, ret...
#!/usr/bin/env python3 import sys def main(): # inf = float('inf') # sys.float_info.max = 1.79e+308 inf = 2 ** 63 - 1 # (for fast JIT compile in PyPy) 9.22e+18 sys.setrecursionlimit(10**6) # 1000 -> 1000000 def input(): return sys.stdin.readline().rstrip() d...
p02585
(N, K) = [int(x) for x in input().split()] P = [int(x) - 1 for x in input().split()] # 0 indexed C = [int(x) for x in input().split()] inf = float("inf") ans = max(C) # at least one move for pos in range(N): i = pos score = 0 cycleLen = None cycleGain = None bestInc = 0 for ...
(N, K) = [int(x) for x in input().split()] P = [int(x) - 1 for x in input().split()] # 0 indexed C = [int(x) for x in input().split()] inf = float("inf") ans = max(C) # at least one move for pos in range(N): i = pos score = 0 cycleLen = None cycleGain = None bestInc = 0 k = ...
p02585
from collections import deque import sys, copy,itertools input = sys.stdin.readline n, k = list(map(int,input().split())) P = list(map(int,input().split())) C = list(map(int,input().split())) POINT = [[-10**12 for i in range(n)] for j in range(k)] PLACE = [[0 for i in range(n)] for j in range(k)] for i in r...
from collections import deque import sys, copy,itertools input = sys.stdin.readline n, k = list(map(int,input().split())) #P = list(map(int,input().split())) P = list([int(x) - 1 for x in input().split()]) #ひとつづつずらす C = list(map(int,input().split())) SCORE = [0]*(n+1) max_score = -10**12 for i in range(n):...
p02585
import sys input = sys.stdin.readline n, k = list(map(int, input().split())) p = list(map(int, input().split())) p = [0] + p c = list(map(int, input().split())) c = [0] + c score = [[0] * (n + 1) for _ in range(n + 1)] # score[i][j]: iからstartしてj回移動した時のスコア l_cycle = [0] * (n + 1) for i in range(1, n + 1): ...
n, k = list(map(int, input().split())) p = list(map(int, input().split())) c = list(map(int, input().split())) for i in range(n): p[i] -= 1 ans = -float('inf') for st in range(n): x = st score = [] tot = 0 while True: x = p[x] score.append(c[x]) tot += c[x] ...
p02585
from itertools import accumulate def main(): cells, moves = [int(x) for x in input().split()] jumps_from = [int(x) - 1 for x in input().split()] score_on = [int(x) for x in input().split()] ans = - 10**10 for start in range(cells): route = [] route_append = route.append...
def main(): cells, can_jump = [int(x) for x in input().split()] jumps_from = [int(x) - 1 for x in input().split()] score_on = [int(x) for x in input().split()] ans = - 10**10 for start in range(cells): route = [] position = start while True: route.appen...
p02585
n, k = list(map(int, input().split())) P = [*[int(x)-1 for x in input().split()]] C = [*list(map(int, input().split()))] INF = 10**18 score = -INF for st in range(n): tot = 0 # cost of one lap costs = [] nx = st while True: nx = P[nx] costs.append(C[nx]) tot += C[nx...
n, k = list(map(int, input().split())) P = [*[int(x)-1 for x in input().split()]] C = [*list(map(int, input().split()))] INF = 10**18 max_score = -INF for st in range(n): lc = 0 # lap count lap_sc = 0 # lap score nx = st while True: lc += 1 lap_sc += C[nx] nx = P[nx...
p02585
n, k = list(map(int, input().split())) P = [*[int(x)-1 for x in input().split()]] C = [*list(map(int, input().split()))] score = -10**18 for st in range(n): lap_cn = 0 lap_sc = 0 nx = st while True: lap_cn += 1 lap_sc += C[nx] nx = P[nx] if nx == st: break ...
n, k = list(map(int, input().split())) P = [*[int(x)-1 for x in input().split()]] C = [*list(map(int, input().split()))] score = -10**18 for st in range(n): lap_cn = lap_sc = sum_sc = 0 nx = st while True: lap_cn += 1 lap_sc += C[nx] nx = P[nx] if nx == st: break...
p02585