input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import sys sys.setrecursionlimit(10**7) def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def II(): return int(sys.stdin.readline()) def SI(): return sys.stdin.readline().strip() INF = 10 ** 18 MOD = 10 ** 9 + 7 def main(): s = SI() t = SI() from collections import defaultdict s_dict = defaultdict(list) for i, char in enumerate(s): s_dict[char].append(i) # s_dict[char] には、 char が s の何番目に入っているかが記載されている。 from bisect import bisect, bisect_left current_position = 0 s_loops = 0 first = True for char in t: li = s_dict[char] if not li: print((-1)) return # current_position より大きい最小の要素を二分探索で得る。 if first: first=False current_position = li[0] else: pos = bisect_left(li, current_position) try: if li[pos] == current_position: current_position = li[pos+1] else: current_position = li[pos] except IndexError: # s は次のループに入った。 s_loops += 1 current_position = li[0] # print(current_position) # if len(set(t)) == 1: # add = 1 # else: add = 1 print((len(s)*s_loops + current_position+add)) main()
import sys sys.setrecursionlimit(10**7) def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def II(): return int(sys.stdin.readline()) def SI(): return sys.stdin.readline().strip() INF = 10 ** 18 MOD = 10 ** 9 + 7 def main(): s = SI() t = SI() from collections import defaultdict s_dict = defaultdict(list) for i, char in enumerate(s): s_dict[char].append(i) # s_dict[char] には、 char が s の何番目に入っているかが記載されている。 from bisect import bisect, bisect_left current_position = 0 s_loops = 0 first = True for char in t: li = s_dict[char] if not li: print((-1)) return if first: # 最初の位置は current_position を信用しないで決める。(そうしないと s,t の1文字目が一致していた場合にひとつ後ろから始めてしまう) first = False current_position = li[0] else: # current_position より大きい最小の要素を二分探索で得る。 # たぶんここで二分探索ができることがオーダーの削減に一役買っていて、このループの計算量が O(len(t) log len(s)) になってる。 pos = bisect_left(li, current_position) try: if li[pos] == current_position: current_position = li[pos+1] else: current_position = li[pos] except IndexError: # s は次のループに入った。 s_loops += 1 current_position = li[0] print((len(s)*s_loops + current_position+1)) main()
p02937
import collections s = input() t = input() i = 0 c = 0 while(True): progress = False j = 0 for j in range(len(s)): if s[j] == t[i]: i+=1 progress = True if i == len(t): break if progress is False: break if i == len(t): break c += 1 print(c * len(s) + j + 1 if progress else -1)
import collections s = input() t = input() sb = s + s h = {chr(ord('a') + i):-1 for i in range(26)} dp = [] for j in range(len(sb)-1,-1,-1): dp.append({k:h[k] for k in h}) h[sb[j]] = j special = h dp = dp[::-1] i = -1 j = 0 c = 0 while(j < len(t)): ha = dp[i] if i >= 0 else special letter = t[j] if ha[letter] != -1: i = ha[letter] j += 1 else: j = -1 break if i >= len(s): i %= len(s) c+=1 print((c * len(s) + i + 1) if j != -1 else -1)
p02937
import sys def SI(): return sys.stdin.readline().strip() def main(): s = SI() t = SI() import collections didx = collections.defaultdict(int) d = collections.defaultdict(list) for i, c in enumerate(s): d[c].append(i) keys = list(d.keys()) for c in set(t): if c not in keys: print((-1)) return import bisect i_loop = 0 currentidx = -1 for c in t: # 次の文字を読む poslist = d[c] if len(poslist) <= didx[c]: # 文字を取り切っている場合は次のループに i_loop += 1 didx = collections.defaultdict(int) currentidx = poslist[0] didx[c] += 1 else: # とり切っていない場合 poslist_afteridx = poslist[didx[c]:] posidx = bisect.bisect_right(poslist_afteridx, currentidx) if posidx >= len(poslist_afteridx): # とれないなら次のループでとったことにする i_loop += 1 didx = collections.defaultdict(int) currentidx = poslist[0] didx[c] += 1 else: # 同じループでとれるなら取って、現在位置を更新 pos = poslist_afteridx[posidx] currentidx = pos didx[c] = posidx + 1 print((len(s)*i_loop + currentidx + 1)) main()
import sys def SI(): return sys.stdin.readline().strip() def main(): s = SI() t = SI() import collections d = collections.defaultdict(list) for i, c in enumerate(s): d[c].append(i) keys = list(d.keys()) for c in set(t): if c not in keys: print((-1)) return import bisect i_loop = 0 currentidx = -1 for c in t: # 次の文字を読む poslist = d[c] # とり切っていない場合 posidx = bisect.bisect_right(poslist, currentidx) if posidx >= len(poslist): # とれないなら次のループでとったことにする i_loop += 1 currentidx = poslist[0] else: # 同じループでとれるなら取って、現在位置を更新 pos = poslist[posidx] currentidx = pos print((len(s)*i_loop + currentidx + 1)) main()
p02937
import sys readline = sys.stdin.readline S = readline().rstrip() T = readline().rstrip() if len(set(T) - set(S)) > 0: print((-1)) exit(0) import bisect # Tの各文字を探す S2 = S * 2 from collections import defaultdict # ある文字が何文字目にあるか辞書 dic = defaultdict(list) for i in range(len(S2)): dic[S2[i]] += [i] ind = -1 ans = 0 for t in T: # 探す文字はt targets = dic[t] # print("targets",targets,"t",t,"ind + 1",ind + 1) newind = targets[bisect.bisect_left(targets, ind + 1)] # print("t",t,"S2",S2,"newind",newind) ans += (newind - ind) if newind >= len(S): newind -= len(S) ind = newind print(ans)
import sys readline = sys.stdin.readline S = readline().rstrip() T = readline().rstrip() if len(set(T) - set(S)) > 0: print((-1)) exit(0) import bisect S2 = S * 2 from collections import defaultdict dic = defaultdict(list) for i in range(len(S2)): dic[S2[i]] += [i] ind = -1 ans = 0 for t in T: targets = dic[t] newind = targets[bisect.bisect_left(targets, ind + 1)] ans += (newind - ind) if newind >= len(S): newind -= len(S) ind = newind print(ans)
p02937
from collections import defaultdict s=input().strip() t=input().strip() def gg(cc, c, l, d): if d[c][-1] <= cc: return d[c][1], l+1 ll = d[c] mx, mn = len(ll)-2, 0 x = (mx - mn)//2 while True: if ll[x] <= cc and cc < ll[x+1]: return ll[x+1], l if cc < ll[x]: mx = x x = (mx-mn)//2 else: mn = x x = (mx-mn)//2 def main(): ss=set(s) tt=set(t) if len(tt - ss) != 0: return -1 d = dict() for i, c in enumerate(s): if c not in d: d[c] = [0] d[c].append(i+1) cc, l = 0, 0 for c in t: cc, l = gg(cc, c, l, d) return l*len(s) + cc if __name__ == '__main__': print((main()))
from collections import defaultdict s=input().strip() t=input().strip() def gg(cc, c, l, d): if d[c][-1] <= cc: return d[c][1], l+1 ll = d[c] mx, mn = len(ll)-1, 0 x = (mx + mn)//2 while True: # print(x,cc,mx,mn,ll) if ll[x] <= cc and cc < ll[x+1]: return ll[x+1], l if cc < ll[x]: mx = x x = (mx+mn)//2 else: mn = x x = (mx+mn)//2 def main(): ss=set(s) tt=set(t) if len(tt - ss) != 0: return -1 d = dict() for i, c in enumerate(s): if c not in d: d[c] = [0] d[c].append(i+1) cc, l = 0, 0 for c in t: # print(c) cc, l = gg(cc, c, l, d) return l*len(s) + cc if __name__ == '__main__': print((main()))
p02937
from collections import defaultdict as dd N, M = list(map(int, input().split())) c = list(map(int, input().split())) c.sort() m = dd(int) d = dd(int) for n in c: d[n] += 1 m[n % M] += 1 res = 0 for i in range(M): temp = min(m[i], m[(M - i) % M]) if i == (M - i) % M: temp = m[i] // 2 m[i] -= temp m[(M - i) % M] -= temp res += temp for i in range(N): n = c[i] if m[n % M] > 1: k = m[n % M] // 2 if d[n] > 1: temp = min(d[n] // 2, k) res += temp d[n] -= temp * 2 m[n % M] -= temp * 2 print(res)
import sys input = sys.stdin.readline N, M = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() m = [0] * M d = [0] * (max(a) + 1) for x in a: d[x] += 1 m[x % M] += 1 res = 0 for i in range(M): t = min(m[i], m[(M - i) % M]) if i == (M - i) % M: t = m[i] // 2 m[i] -= t m[(M - i) % M] -= t res += t for i in range(N): x = a[i] if m[x % M] > 1: k = m[x % M] // 2 if d[x] > 1: t = min(d[x] // 2, k) res += t d[x] -= t * 2 m[x % M] -= t * 2 print(res)
p03912
n,m=list(map(int,input().split())) a=list(map(int,input().split())) from collections import Counter mo=[[]for _ in range(m)] for i in a:mo[i%m].append(i) ans=len(mo[0])//2 if m%2==0:ans+=len(mo[m//2])//2 for i in range(1,0--m//2): if len(mo[i])>len(mo[m-i]):d=Counter(mo[i]) elif len(mo[i])==len(mo[m-i]): ans+=len(mo[i]) continue else:d=Counter(mo[m-i]) anss=0 for j in d:anss+=d[j]//2 ans+=min(len(mo[i]),len(mo[m-i]))+min(abs(len(mo[i])-len(mo[m-i]))//2,anss) print(ans)
n,m=list(map(int,input().split())) x=list(map(int,input().split())) a=[[]for _ in range(m)] for i in x:a[i%m].append(i) b=[] from collections import Counter for i in range(m): d=Counter(a[i]) ans=0 for j in d:ans+=2*(d[j]//2) b.append([len(a[i]),ans]) ans=0 if m%2==0:ans+=len(a[m//2])//2 ans+=len(a[0])//2 for i in range(1,0--m//2): sc,sp=b[i] tc,tp=b[m-i] v=min(sc,tc) ans+=v ans+=min(sp,sc-v)//2 ans+=min(tp,tc-v)//2 print(ans)
p03912
from collections import defaultdict, Counter N, M = list(map(int, input().split())) X = list(map(int, input().split())) d = defaultdict(int) #mod Mで見たときの個数 for x in X: d[x % M] += 1 ans = 0 #Mの倍数//2 組の和はすべてMの倍数となる # ans += d[0] // 2 lst = [0] * M for m in range(1, M): ans += min(d[m], d[M - m]) lst[m] = d[m] - min(d[m], d[M - m]) #mod Mで見たときに残っている個数 ans //= 2 ans += d[0] // 2 # print (ans) # print ('lst', lst) # print ('d', d) C = Counter(X) for c in C: tmp = min(C[c], lst[c % M]) // 2 lst[c % M] -= 2 * tmp ans += tmp print (ans)
#違う数との組-->同じ数での組の順に考える from collections import defaultdict, Counter N, M = list(map(int, input().split())) X = list(map(int, input().split())) d = defaultdict(int) #mod Mで見たときの個数 for x in X: d[x % M] += 1 ans = 0 #Mの倍数//2 組の和はすべてMの倍数となる lst = [0] * M for m in range(1, M): ans += min(d[m], d[M - m]) lst[m] = d[m] - min(d[m], d[M - m]) #mod Mで見たときに残っている個数 ans //= 2 ans += d[0] // 2 #0だけは1ダブルカウントしていないのであとから足す C = Counter(X) for c in C: tmp = min(C[c], lst[c % M]) // 2 lst[c % M] -= 2 * tmp ans += tmp print (ans)
p03912
from collections import Counter N, M = list(map(int, input().split())) X = list(map(int, input().split())) modGrp = [[] for _ in range(M)] for x in X: modGrp[x % M].append(x) ans = len(modGrp[0]) // 2 modGrp[0] = [] if M % 2 == 0: ans += len(modGrp[M // 2]) // 2 modGrp[M // 2] = [] for m in range(1, M // 2 + 1): S = modGrp[m] T = modGrp[M - m] if len(S) < len(T): S, T = T, S cnt = Counter(S) dis = 0 for c in list(cnt.values()): while c >= 2 and len(S) - dis * 2 > len(T): dis += 1 c -= 2 ans += 1 ans += min(len(T), len(S) - dis * 2) modGrp[m] = [] modGrp[M - m] = [] print(ans)
from collections import Counter, defaultdict N, M = list(map(int, input().split())) X = list(map(int, input().split())) cntMod = defaultdict(int) cntDup = defaultdict(int) for x, c in list(Counter(X).items()): cntMod[x % M] += c cntDup[x % M] += c // 2 ans = 0 for m in range(M): if m > M // 2: break n = (M - m) % M if n == m: ans += cntMod[n] // 2 else: mi = min(cntMod[n], cntMod[m]) ans += mi cntMod[n] -= mi cntMod[m] -= mi ans += min(cntMod[n] // 2, cntDup[n]) ans += min(cntMod[m] // 2, cntDup[m]) print(ans)
p03912
from collections import Counter N, M = list(map(int, input().split())) cntX = Counter(list(map(int, input().split()))) cntMod = Counter() cntDup = Counter() for x, c in list(cntX.items()): cntMod[x % M] += c cntDup[x % M] += c // 2 ans = 0 for n in range(M // 2 + 1): m = (M - n) % M if m == n: ans += cntMod[n] // 2 continue cnt = min(cntMod[n], cntMod[m]) cntMod[n] -= cnt cntMod[m] -= cnt ans += cnt ans += min(cntMod[n] // 2, cntDup[n]) + min(cntMod[m] // 2, cntDup[m]) print(ans)
from collections import Counter N, M = list(map(int, input().split())) X = list(map(int, input().split())) dupCnt = Counter() modCnt = Counter() for x, c in list(Counter(X).items()): x %= M dupCnt[x] += c // 2 modCnt[x] += c ans = 0 for i in range(M): j = M - i if i > j: break if i == j or i == 0: ans += modCnt[i] // 2 continue mi = min(modCnt[i], modCnt[j]) ans += mi ans += min((modCnt[i] - mi) // 2, dupCnt[i]) ans += min((modCnt[j] - mi) // 2, dupCnt[j]) print(ans)
p03912
MAX_X = 10 ** 5 + 100 def main(): N, M = [int(i) for i in input().split()] X = [0] * (MAX_X + 1) for i in map(int, input().split()): X[i] += 1 Y = [sum(X[i::M]) for i in range(min(M, MAX_X))] # Mの倍数 ans = Y[0] // 2 Y[0] = 0 # M%2==0のM/2 if M % 2 == 0: ans += Y[M//2] // 2 Y[M//2] = 0 for i in range(1, M//2+1): j = M - i tmp = min(Y[i], Y[j]) ans += tmp Y[i] -= tmp Y[j] -= tmp try: for i, n in enumerate(X): j = i % M if Y[j] < 2 or n < 2: continue tmp = min(Y[j]-Y[j]%2, n-n%2) ans += tmp // 2 Y[j] -= tmp except IndexError: print((-1)) print(ans) if __name__ == "__main__": main()
MAX_X = 10 ** 5 + 100 def main(): N, M = [int(i) for i in input().split()] X = [0] * (MAX_X + 1) for i in map(int, input().split()): X[i] += 1 Y = [sum(X[i::M]) for i in range(min(M, MAX_X))] # Mの倍数 ans = Y[0] // 2 Y[0] = 0 # M%2==0のM/2 try: if M % 2 == 0: ans += Y[M//2] // 2 Y[M//2] = 0 for i in range(1, M//2+1): j = M - i tmp = min(Y[i], Y[j]) ans += tmp Y[i] -= tmp Y[j] -= tmp except IndexError: print((-1)) for i, n in enumerate(X): j = i % M if Y[j] < 2 or n < 2: continue tmp = min(Y[j]-Y[j]%2, n-n%2) ans += tmp // 2 Y[j] -= tmp print(ans) if __name__ == "__main__": main()
p03912
from collections import Counter n,m=list(map(int,input().split())) x=[int(i) for i in input().split()] v=[[] for i in range(m)] for i in x:v[i%m].append(i) a=0 for i in range(1,m//2+1*(m%2==1)): d=len(v[i])-len(v[m-i]) if d<0: d=-d v[i],v[m-i]=v[m-i],v[i] c=sorted(list(Counter(v[i]).items()),key=lambda x:x[1]) p=0 for t in c: p+=(t[1]//2) a+=len(v[m-i])+min(p,(d//2)) a+=len(v[0])//2+(len(v[m//2])//2 if m%2==0 else 0) print(a)
from collections import Counter n,m=list(map(int,input().split())) x=[int(i) for i in input().split()] v=[[] for i in range(m)] for i in x:v[i%m].append(i) a=0 for i in range(1,m//2+1*(m%2==1)): x,y=v[i],v[m-i] d=len(x)-len(y) if d<0: d=-d x,y=y,x c=sorted(list(Counter(x).items()),key=lambda x:x[1]) p=0 for t in c: p+=(t[1]//2) a+=len(y)+min(p,(d//2)) a+=len(v[0])//2+(len(v[m//2])//2 if m%2==0 else 0) print(a)
p03912
from collections import defaultdict, Counter N, M = list(map(int, input().split())) #X = list(map(int, input().split())) C = Counter(list(map(int, input().split()))) D = defaultdict(int) E = defaultdict(int) for k, v in list(C.items()): D[k%M] += v E[k%M] += v//2 ans = D[0] // 2 for i in range(1, M): j = M - i if i < j: x = min(D[i], D[j]) y = min((D[i] - x)//2, E[i]) + min((D[j] - x)//2, E[j]) ans += x + y elif i == j: ans += D[i] //2 else: break print(ans)
from collections import defaultdict, Counter N, M = list(map(int, input().split())) C = Counter(list(map(int, input().split()))) #D = defaultdict(int) #E = defaultdict(int) D = [0]*M E = [0]*M for k, v in list(C.items()): D[k%M] += v E[k%M] += v//2 ans = D[0] // 2 for i in range(1, M): j = M - i if i < j: x = min(D[i], D[j]) y = min((D[i] - x)//2, E[i]) + min((D[j] - x)//2, E[j]) ans += x + y elif i == j: ans += D[i] //2 else: break print(ans)
p03912
import sys input = sys.stdin.readline sys.setrecursionlimit(1000000) from collections import deque def getN(): return int(eval(input())) def getList(): return list(map(int, input().split())) import math n, m = getList() nums = getList() n_mods = [0 for i in range(m)] pair_mods = [0 for i in range(m)] num_mods = [{} for i in range(m)] for num in nums: modres = num % m n_mods[modres] += 1 if num in num_mods[modres]: num_mods[modres][num] += 1 else: num_mods[modres][num] = 1 if num_mods[modres][num] % 2 == 0: pair_mods[modres] += 1 # print(n_mods) # print(pair_mods) ans = 0 for i in range(1, (m+1) // 2): left = i right = m - i ans += min(n_mods[left], n_mods[right]) if n_mods[left] > n_mods[right]: ans += max(0, min(pair_mods[left], (n_mods[left] - n_mods[right]) // 2)) else: ans += max(0, min(pair_mods[right], (n_mods[right] - n_mods[left]) // 2)) # print(left, right, ans) ans += n_mods[0] // 2 if m % 2 == 0: ans += n_mods[m//2] // 2 print(ans)
import sys input = sys.stdin.buffer.readline def getN(): return int(eval(input())) def getNM(): return list(map(int, input().split())) def getlist(): return list(map(int, input().split())) import math import heapq from collections import defaultdict, Counter, deque MOD = 10**9 + 7 INF = 10**15 def main(): n,m = getlist() nums = getlist() cnt = Counter(nums) mod = [0 for i in range(m)] same = [0 for i in range(m)] for k, v in list(cnt.items()): mod[k%m] += v same[k%m] += v // 2 ans = mod[0] // 2 for i in range(1, int((m+2) // 2)): if i == m / 2: ans += mod[i] // 2 else: other = m - i ans += min(mod[i], mod[other]) if mod[i] > mod[other]: ans += min(same[i], (mod[i] - mod[other]) // 2) if mod[i] < mod[other]: ans += min(same[other], -(mod[i] - mod[other]) // 2) print(ans) # print(same, mod) if __name__ == '__main__': main() """ 9999 3 2916 """
p03912
# 大混乱したので乱択 from random import randint def main(): import sys sys.setrecursionlimit(500000) N = int(eval(input())) if N==2: print((1)) exit() E = [[] for _ in range(N)] for _ in range(N-1): a, b = list(map(int, input().split())) E[a].append(b) E[b].append(a) def dfs(v=0, p=-1, root=0): cnt_child = 0 cnt_false = 0 res = 0 for u in E[v]: if u!=p: cnt_child += 1 d = dfs(u, v, root) res += d cnt_false += d == 0 return res + max(0, cnt_false - 1) #for i in range(N): # print(dfs(i, -1, i)) print((max(dfs(i, -1, i) for i in sorted(list(range(N)), key=lambda x: -len(E[x]))[:5]))) main()
# 大混乱したので乱択 # これ嘘じゃないのか??? def main(): import sys sys.setrecursionlimit(500000) N = int(eval(input())) if N==2: print((1)) exit() E = [[] for _ in range(N)] for _ in range(N-1): a, b = list(map(int, input().split())) E[a].append(b) E[b].append(a) def dfs(v=0, p=-1, root=0): cnt_child = 0 cnt_false = 0 res = 0 for u in E[v]: if u!=p: cnt_child += 1 d = dfs(u, v, root) res += d cnt_false += d == 0 return res + max(0, cnt_false - 1) #for i in range(N): # print(dfs(i, -1, i)) print((max(dfs(i, -1, i) for i in sorted(list(range(N)), key=lambda x: -len(E[x]))[:1]))) main()
p03441
from itertools import accumulate def solve(): MOD = 10 ** 9 + 7 def max2(x, y): return x if x >= y else y N, K = list(map(int, input().split())) As = list(map(int, input().split())) dp = [0] * (K+1) dp[0] = 1 for A in As: accDP = [0] + list(accumulate(dp)) accDP = [accDPi % MOD for accDPi in accDP] dp = [(accDP[j+1] - accDP[max2(0, j-A)]) % MOD for j in range(K+1)] print((dp[K])) solve()
from itertools import accumulate def solve(): MOD = 10**9 + 7 N, K = list(map(int, input().split())) As = list(map(int, input().split())) dp = [0] * (K+1) dp[0] = 1 for A in As: dp = list(accumulate(dp)) for j, d in enumerate(dp[:K-A], start=A+1): dp[j] -= d dp = [dpi % MOD for dpi in dp] print((dp[K])) solve()
p03172
N, K = list(map(int, input().split())) a = tuple(map(int, input().split())) def main(): l = [[0] * (K+1) for _ in range(N)] for i in range(N): t = 0 for j in range(K+1): if i == 0: if a[i] >= j: l[i][j] = 1 elif j == 0: t = 1 l[i][j] = 1 else: if j <= a[i]: t += l[i - 1][j] else: t += (l[i - 1][j] - l[i - 1][j - a[i] - 1]) l[i][j] += t return l[-1][-1] % (10 ** 9 + 7) print((main()))
N, K = list(map(int, input().split())) a = tuple(map(int, input().split())) def main(): l = [0] * (K+1) for i in range(N): t = 0 lt = l l = [0] * (K + 1) for j in range(K+1): if i == 0: if a[i] >= j: l[j] = 1 elif j == 0: t = 1 l[j] = 1 else: if j <= a[i]: t += lt[j] else: t += (lt[j] - lt[j - a[i] - 1]) l[j] += t return l[-1] % (10 ** 9 + 7) print((main()))
p03172
N, K = list(map(int, input().split())) a = tuple(map(int, input().split())) def main(): l = [1 if a[0] >= j else 0 for j in range(K+1)] lt= [1 if a[0] >= j else 0 for j in range(K+1)] s = a[0] for i in range(1,N): lt, l = l, lt s += a[i] l[0] = 1 t = 1 for j in range(1, a[i]+1): t += lt[j] l[j] = t for j in range(a[i]+1, min(K+1, s+1)): t += (lt[j] - lt[j - a[i] - 1]) l[j] = t return l[-1] % (10 ** 9 + 7) print((main()))
N, K = list(map(int, input().split())) a = tuple(map(int, input().split())) def main(): l = [1 if a[0] >= j else 0 for j in range(K+1)] lt= [0] * (K+1) s = a[0] for i in range(1,N): lt, l = l, lt s += a[i] l[0] = 1 t = 1 for j in range(1, a[i]+1): t += lt[j] l[j] = t for j in range(a[i]+1, min(K+1, s+1)): t += (lt[j] - lt[j - a[i] - 1]) l[j] = t return l[-1] % (10 ** 9 + 7) print((main()))
p03172
# coding: utf-8 from itertools import accumulate as accum import sys input = sys.stdin.readline def f(n, k, ai): mod = int(1e9 + 7) dp = [0] * (k + 1) dp[0] = 1 for a in ai: # csum: Cumulative sum # dp[j] = dp[0] + dp[1] + ... + dp[j] dp = list(accum(dp, lambda x, y: (x + y) % mod)) # j = 0 ~ a[i] までは累積和の結果そのままで a[i] + 1 以降は # dp[j] -= dp[j] - dp[j - a[i] - 1] dp[a + 1:] = [x - y for x, y in zip(dp[a + 1:], dp)] return(dp[k]) n, k = list(map(int, input().split())) # 1 <= n <= 100, 0 <= k <= 10^5 a = list(map(int, input().split())) print((f(n, k, a)))
# coding: utf-8 from itertools import accumulate as accum import sys input = sys.stdin.readline def f(n, k, ai): mod = int(1e9 + 7) dp = [0] * (k + 1) dp[0] = 1 for a in ai: # csum: Cumulative sum # dp[j] = dp[0] + dp[1] + ... + dp[j] # dp = list(accum(dp, lambda x, y: (x + y) % mod)) dp = list(accum(dp)) # j = 0 ~ a[i] までは累積和の結果そのままで a[i] + 1 以降は # dp[j] -= dp[j] - dp[j - a[i] - 1] dp[a + 1:] = [x - y for x, y in zip(dp[a + 1:], dp)] dp = [x % mod for x in dp] return(dp[k]) n, k = list(map(int, input().split())) # 1 <= n <= 100, 0 <= k <= 10^5 a = list(map(int, input().split())) print((f(n, k, a)))
p03172
N, K = list(map(int, input().split())) *a, = list(map(int, input().split())) MOD = 10**9+7 dp = [[0]*(K+1) for _ in range(N+1)] for i in range(N+1): dp[i][0] = 1 sums = [0]*(K+2) for i in range(1, N+1): sums[0] = 0 for j in range(1, K+2): sums[j] = (dp[i-1][j-1]+sums[j-1]) % MOD for j in range(1, K+1): sj = max([0, j-a[i-1]]) dp[i][j] = (sums[j+1]-sums[sj]+MOD) % MOD print((dp[N][K]))
N, K = list(map(int, input().split())) *a, = list(map(int, input().split())) MOD = 10**9+7 dp = [0]*(K+1) sums = [0]*(K+2) dp[0] = 1 for i in range(1, N+1): sums[0] = 0 for j in range(1, K+2): sums[j] = (dp[j-1]+sums[j-1]) % MOD for j in range(1, K+1): sj = max([0, j-a[i-1]]) dp[j] = (sums[j+1]-sums[sj]+MOD) % MOD print((dp[K]))
p03172
def f(k, a_s): md = 10 ** 9 + 7 dp = [[0] * (k + 1) for _ in range(n)] dp[0][0] = 1 for i, a in enumerate(a_s[:-1]): dp[i + 1][0] = s = dp[i][0]% md for j in range(1, a + 1): dp[i + 1][j] = s = (s + dp[i][j])% md for j in range(a + 1, k + 1): dp[i + 1][j] = s = (s + dp[i][j] - dp[i][j - a - 1])% md return sum(dp[-1][k - a_s[-1]:]) % md n, k = list(map(int, input().split())) a_s = list(map(int, input().split())) print((f(k, a_s)))
def f(k, a_s): md = 10 ** 9 + 7 dp = [[0] * (k + 1) for _ in range(n)] dp[0][0] = 1 for i, a in enumerate(a_s[:-1]): dpi1 = dp[i + 1] dpi = dp[i] dpi1[0] = s = dp[i][0] % md for j in range(1, a + 1): dpi1[j] = s = (s + dpi[j]) % md for j in range(a + 1, k + 1): dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md return sum(dp[-1][k - a_s[-1]:]) % md n, k = list(map(int, input().split())) a_s = list(map(int, input().split())) print((f(k, a_s)))
p03172
def f(n, k, a_s): md = 10 ** 9 + 7 dp = [[0] * (k + 1) for _ in range(n)] dp[0][0] = 1 dpi = dp[0] for i, a in enumerate(a_s[:-1]): dpi1 = dp[i + 1] dpi1[0] = dp[i][0] % md for j in range(1, a + 1): dpi1[j] = (dpi1[j - 1] + dpi[j]) % md for j in range(a + 1, k + 1): dpi1[j] = (dpi1[j - 1] + dpi[j] - dpi[j - a - 1]) % md dpi = dpi1 return sum(dp[-1][k - a_s[-1]:]) % md n, k = list(map(int, input().split())) a_s = list(map(int, input().split())) print((f(n, k, a_s)))
def f(k, a_s): md = 10 ** 9 + 7 dp = [[0] * (k + 1) for _ in range(n)] dp[0][0] = 1 dpi = dp[0] for dpi1, a in zip(dp[1:],a_s[:-1]): dpi1[0] = s = dpi[0] % md for j in range(1, a + 1): dpi1[j] = s = (s + dpi[j]) % md for j in range(a + 1, k + 1): dpi1[j] = s = (s + dpi[j] - dpi[j - a - 1]) % md dpi=dpi1 return sum(dp[-1][k - a_s[-1]:]) % md n, k = list(map(int, input().split())) a_s = list(map(int, input().split())) print((f(k, a_s)))
p03172
class BIT(object): def __init__(self, N, init=0, mod=1): self.N = N self.bit = [init for _ in range(N)] self.mod = mod def add(self, k, w): x = k while x < self.N: self.bit[x] += w % self.mod self.bit[x] %= self.mod x |= x + 1 def sum(self, k): s = 0 x = k - 1 while x >= 0: s += self.bit[x] s %= self.mod x = (x & (x + 1)) - 1 return s MOD = 10 ** 9 + 7 n, k = list(map(int, input().split())) a = list(map(int, input().split())) bit = BIT(k + 1, mod=MOD) bit.add(0, 1) for ai in a: for i in range(k, 0, -1): if ai >= i: x = bit.sum(i) else: x = bit.sum(i) - bit.sum(i - ai) bit.add(i, x % MOD) print((bit.sum(k + 1) - bit.sum(k)))
MOD = 10 ** 9 + 7 n, k = list(map(int, input().split())) a = list(map(int, input().split())) dp = [0 for _ in range(k + 1)] dp[0] = 1 b = [1 for _ in range(k + 2)] b[0] = 0 for ai in a: for i in range(k, -1, -1): dp[i] += (b[i] - b[i - min(ai, i)]) % MOD dp[i] %= MOD for i in range(1, k + 2): b[i] = (b[i - 1] + dp[i - 1]) % MOD b[i] %= MOD print((dp[-1]))
p03172
import sys input=sys.stdin.readline n,k=list(map(int,input().split())) a=list(map(int,input().split())) def main(): dp=[[0]*(k+1) for _ in range(n+1)] mod=1000000007 #i人目までに0個配る方法は1通り #for i in range(n+1): # dp[i][0]=1 for i in range(0,n+1): for j in range(0,k+1): if i==0 and j==0: dp[i][j]=1 break if j==0: dp[i][j]=1 continue tmp=dp[i - 1][j] + dp[i][j - 1] if tmp>=mod: tmp%=mod dp[i][j]=tmp if j-a[i-1]-1>=0: tmp=dp[i][j]-dp[i-1][j-a[i-1]-1] if tmp>=mod: tmp%=mod dp[i][j]=tmp #print(dp) print((dp[n][k])) main()
import sys from itertools import accumulate def main(): input=sys.stdin.readline n,k=list(map(int,input().split())) A=list(map(int,input().split())) mod=10**9+7 dp=[0]*(k+1) dp[0]=1 for a in A: dp=list(accumulate(dp)) #print("accumulated",dp) for j,d in enumerate(dp[:k-a],start=a+1): dp[j]-=d dp=[dpi%mod for dpi in dp] #print(dp) print((dp[k])) """ def main(): dp=[[0]*(k+1) for _ in range(n+1)] mod=1000000007 #i人目までに0個配る方法は1通り #for i in range(n+1): # dp[i][0]=1 for i in range(0,n+1): for j in range(0,k+1): if i==0 and j==0: dp[i][j]=1 break if j==0: dp[i][j]=1 continue tmp=dp[i - 1][j] + dp[i][j - 1] if tmp>=mod: tmp%=mod dp[i][j]=tmp if j-a[i-1]-1>=0: tmp=dp[i][j]-dp[i-1][j-a[i-1]-1] if tmp>=mod: tmp%=mod dp[i][j]=tmp #print(dp) print(dp[n][k])""" main()
p03172
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n,k = list(map(int, input().split())) a = list(map(int, input().split())) dp = [[0]*(k+1) for _ in range(n+1)] cum = [1]*(k+2) M = 10**9+7 for i in range(n+1): dp[i][0] = 1 cum[0] = 0 for i in range(1,n+1): current = [0]*(k+2) for j in range(k+1): # print(i,j,k,a[i]) dp[i][j] = (cum[j+1] - cum[max(0, j-min(k,a[i-1]))]) % M current[j+1] = (current[j] + dp[i][j])%M cum = current print((dp[n][k]))
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") n,k = list(map(int, input().split())) a = list(map(int, input().split())) dp = [0]*(k+1) cum = [1]*(k+2) M = 10**9+7 dp[0] = 1 cum[0] = 0 for i in range(1,n+1): current = [0]*(k+2) for j in range(k+1): dp[j] = (cum[j+1] - cum[max(0, j-min(k,a[i-1]))]) % M current[j+1] = (current[j] + dp[j])%M cum = current print((dp[k]))
p03172
# -*- coding: utf-8 -*- """ 参考:https://kyopro-friends.hatenablog.com/entry/2019/01/12/231035    https://www.hamayanhamayan.com/entry/2019/01/10/235150    http://anctgcc.hatenablog.com/entry/2014/12/03/194140    https://ikatakos.com/pot/programming_algorithm/data_structure/binary_indexed_tree ・数え上げDP ・累積和の代わりにBIT(Binary Indexed Tree)を使ってみる。 ・添字の使い方を累積和の方と合わせた版 """ import sys, re from collections import deque, defaultdict, Counter from math import sqrt, hypot, factorial, pi, sin, cos, radians if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd from heapq import heappop, heappush, heapify, heappushpop from bisect import bisect_left, bisect_right from itertools import permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from functools import reduce, partial from fractions import Fraction from string import ascii_lowercase, ascii_uppercase, digits def input(): return sys.stdin.readline().strip() def ceil(a, b=1): return int(-(-a // b)) def round(x): return int((x*2+1) // 2) def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD def lcm(x, y): return (x * y) // gcd(x, y) def lcm_list(nums): return reduce(lcm, nums, 1) def gcd_list(nums): return reduce(gcd, nums, nums[0]) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class BIT: def __init__(self, n): # 0-indexed self.size = n self.tree = [0] * n def sum(self, i): s = 0 i += 1 while i > 0: s += self.tree[i-1] i -= i & -i return s # 区間和の取得 [l, r) def get(self, l, r): res = 0 if r: res += self.sum(r-1) if l: res -= self.sum(l-1) return res # 値の追加:添字, 値 def add(self, i, x): i += 1 while i <= self.size: self.tree[i-1] += x i += i & -i N, K = MAP() aN = LIST() # dp[この子までの範囲で][飴を配る総数] = 場合の数 # dpの2次元目をBITで構築する dp = [BIT(K+1) for i in range(N+1)] dp[0].add(0, 1) for i in range(N): for j in range(K+1): # iが送る予定の長さに応じた区間和を取る sm = dp[i].get(max(j-aN[i], 0), j+1) % MOD dp[i+1].add(j, sm) print((dp[N].get(K, K+1) % MOD))
# -*- coding: utf-8 -*- import sys, re from collections import deque, defaultdict, Counter from math import sqrt, hypot, factorial, pi, sin, cos, radians if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd from heapq import heappop, heappush, heapify, heappushpop from bisect import bisect_left, bisect_right from itertools import permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from functools import reduce, partial from fractions import Fraction from string import ascii_lowercase, ascii_uppercase, digits def input(): return sys.stdin.readline().strip() def ceil(a, b=1): return int(-(-a // b)) def round(x): return int((x*2+1) // 2) def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD def lcm(x, y): return (x * y) // gcd(x, y) def lcm_list(nums): return reduce(lcm, nums, 1) def gcd_list(nums): return reduce(gcd, nums, nums[0]) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N, K = MAP() aN = LIST() # dp[この子までの範囲で][飴を配る総数] = 場合の数 dp = [[0] * (K+1) for i in range(N+1)] # 0番目まで(誰もいない)の範囲で0個配る場合の数は1通り dp[0][0] = 1 for i in range(N): sm = [0] * (K+2) # 今回送るi行について累積和を取る for j in range(1, K+2): sm[j] = sm[j-1] + dp[i][j-1] sm[j] %= MOD for j in range(K+1): # iが送る予定の長さに応じた区間和を取る dp[i+1][j] = sm[j+1] - sm[max(j-aN[i], 0)] dp[i+1][j] %= MOD # for k in range(aN[i]+1): # dp[i+1][j+k] += dp[i][j] print((dp[N][K]))
p03172
# -*- coding: utf-8 -*- import sys, re from collections import deque, defaultdict, Counter from math import sqrt, hypot, factorial, pi, sin, cos, radians if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd from heapq import heappop, heappush, heapify, heappushpop from bisect import bisect_left, bisect_right from itertools import permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from functools import reduce, partial from fractions import Fraction from string import ascii_lowercase, ascii_uppercase, digits def input(): return sys.stdin.readline().strip() def ceil(a, b=1): return int(-(-a // b)) def round(x): return int((x*2+1) // 2) def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD def lcm(x, y): return (x * y) // gcd(x, y) def lcm_list(nums): return reduce(lcm, nums, 1) def gcd_list(nums): return reduce(gcd, nums, nums[0]) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N, K = MAP() aN = LIST() # dp[この子までの範囲で][飴を配る総数] = 場合の数 dp = [[0] * (K+1) for i in range(N+1)] # 0番目まで(誰もいない)の範囲で0個配る場合の数は1通り dp[0][0] = 1 for i in range(N): sm = [0] * (K+2) # 今回送るi行について累積和を取る for j in range(1, K+2): sm[j] = sm[j-1] + dp[i][j-1] sm[j] %= MOD for j in range(K+1): # iが送る予定の長さに応じた区間和を取る dp[i+1][j] = sm[j+1] - sm[max(j-aN[i], 0)] dp[i+1][j] %= MOD # for k in range(aN[i]+1): # dp[i+1][j+k] += dp[i][j] print((dp[N][K]))
# -*- coding: utf-8 -*- import sys from functools import reduce def input(): return sys.stdin.readline().strip() def ceil(a, b=1): return int(-(-a // b)) def round(x): return int((x*2+1) // 2) def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD def lcm(x, y): return (x * y) // gcd(x, y) def lcm_list(nums): return reduce(lcm, nums, 1) def gcd_list(nums): return reduce(gcd, nums, nums[0]) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N, K = MAP() aN = LIST() # dp[この子までの範囲で][飴を配る総数] = 場合の数 dp = [[0] * (K+1) for i in range(N+1)] # 0番目まで(誰もいない)の範囲で0個配る場合の数は1通り dp[0][0] = 1 for i in range(N): sm = [0] * (K+2) # 今回送るi行について累積和を取る for j in range(1, K+2): sm[j] = sm[j-1] + dp[i][j-1] sm[j] %= MOD for j in range(K+1): # iが送る予定の長さに応じた区間和を取る dp[i+1][j] = sm[j+1] - sm[max(j-aN[i], 0)] dp[i+1][j] %= MOD # for k in range(aN[i]+1): # dp[i+1][j+k] += dp[i][j] print((dp[N][K]))
p03172
import sys def solve(): input = sys.stdin.readline N, K = list(map(int, input().split())) A = [int(a) for a in input().split()] mod = 7 + 10 ** 9 DP = [[0 for j in range(K + 1)] for i in range(N)] for i in range(A[0] + 1): DP[0][i] = 1 for i in range(1, N): cum = [0] * (K + 2) for j in range(1, K + 2): cum[j] = (cum[j-1] + DP[i-1][j-1]) % mod #print(cum) for j in range(K + 1): DP[i][j] = (cum[j + 1] - cum[max(0, j - A[i])]) % mod print((DP[N-1][K])) #print(DP) return 0 if __name__ == "__main__": solve()
import sys def solve(): input = sys.stdin.readline N, K = list(map(int, input().split())) A = [int(a) for a in input().split()] mod = 7 + 10 ** 9 DP = [[0 for j in range(K + 1)] for i in range(N)] for i in range(A[0] + 1): DP[0][i] = 1 cum = [0] * (K + 2) for i in range(1, N): for j in range(1, K + 2): cum[j] = (cum[j-1] + DP[i-1][j-1]) % mod for j in range(K + 1): DP[i][j] = (cum[j + 1] - cum[max(0, j - A[i])]) % mod print((DP[N-1][K])) return 0 if __name__ == "__main__": solve()
p03172
# -*- coding: utf-8 -*- import sys N,K=list(map(int, sys.stdin.readline().split())) A=list(map(int, sys.stdin.readline().split())) dp=[ [] for i in range(N+1) ] dp[0]=[1] mod=10**9+7 for n in range(N): W=[0] #WA:累積和 L=dp[n] l=len(L) a=A[n] for j,x in enumerate(L): if j==0: W.append(x) else: W.append( W[-1]+x ) T=[] #Temporary for k in range( min (K+1,a+l) ): #print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)] T.append( W[min( k+1, l)]-W[max(0,k-a)] ) dp[n+1]=T try: print(dp[N][K]%mod) except IndexError: print(0)
# -*- coding: utf-8 -*- import sys N,K=list(map(int, sys.stdin.readline().split())) A=list(map(int, sys.stdin.readline().split())) mod=10**9+7 T=[1] for n in range(N): W=[0] #WA:累積和 L=T l=len(L) a=A[n] for j,x in enumerate(L): if j==0: W.append(x) else: W.append( W[-1]+x ) T=[] #Temporary for k in range( min (K+1,a+l) ): #print k, min( k+1, l), max(0,k-a), W[min( k+1, l)]-W[max(0,k-a)] T.append( W[min( k+1, l)]-W[max(0,k-a)] ) try: print(T[K]%mod) except IndexError: print(0)
p03172
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n,k = LI() a = LI() t = [[0]*(k+1) for _ in range(n)] z = t[-1] for i in range(a[-1]+1): z[i] = 1 for i in range(n-2,-1,-1): s = 0 z = t[i] y = t[i+1] c = a[i] + 1 for j in range(c): s += y[j] s %= mod z[j] = s for j in range(c,k+1): s += y[j] - y[j-c] s %= mod z[j] = s return t[0][-1] print(main())
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n,k = LI() a = LI() l = k + 1 y = [1] * (a[-1]+1) + [0] * (l-a[-1]-1) for i in range(n-2,-1,-1): z = [None] * l s = 0 c = a[i] + 1 for j in range(c): z[j] = s = (s + y[j]) % mod for j in range(c,l): z[j] = s = (s + y[j] - y[j-c]) % mod y = z return (y[-1] + mod) % mod print(main())
p03172
n, k = list(map(int, input().split())) a = list(map(int, input().split())) mod = 10 ** 9 + 7 dp = [[0] * (k + 1) for _ in range(n + 1)] dp[0][0] = 1 for i in range(n): dp[i + 1][0] = dp[i][0] for j in range(1, k+1): dp[i + 1][j] = (dp[i + 1][j - 1] + dp[i][j]) % mod for j in range(k, a[i], -1): dp[i + 1][j] = (dp[i + 1][j] - dp[i + 1][j - a[i] - 1]) % mod print((dp[n][-1]))
n, k = list(map(int, input().split())) a = list(map(int, input().split())) mod = 10 ** 9 + 7 f = [0] * (k + 1) f[0] = 1 for i in range(n): for j in range(1, k+1): f[j] = (f[j] + f[j - 1]) % mod # 1/(1-x):累積和 for j in range(k, a[i], -1): # 1-x^(a[i]+1) a[i]+1次したの項を引く f[j] = (f[j] - f[j - a[i] - 1]) % mod print((f[-1]))
p03172
def main(): e,M=enumerate,10**9+7 n,k,*a=list(map(int,open(0).read().split())) dp=[[0]*(k+1)for _ in range(n+1)] dp[0][0]=1 for b,d,a in zip(dp,dp[1:],a): a+=1 t=0 for j,v in e(b[:a]): t+=v t%=M d[j]=t for j,(w,v)in e(list(zip(b,b[a:])),a): t+=v-w t%=M d[j]=t print(t) main()
def main(): M=10**9+7 n,k,*a=list(map(int,open(0).read().split())) dp=[[1]+[0]*k] for b,i in zip(dp,a): t,d=0,[] for v in b[:i+1]: t+=v t%=M d+=t, for w,v in zip(b,b[i+1:]): t+=v-w t%=M d+=t, dp+=d, print(t) main()
p03172
from sys import stdin from itertools import accumulate input = stdin.readline MOD = 10**9+7 N, K = list(map(int,input().split())) a = list(map(int,input().split())) p = list(accumulate(a)) dp = [0] * (K+1) dp[0] = 1 prefix = [0]+[1]*(K+1) for i in range(N): new_dp = [0]*(K+1) for j in range(min(p[i],K)+1): new_dp[j] = prefix[j+1] - prefix[max(0,j-a[i])] dp = new_dp[:] prefix = [i % MOD for i in [0]+list(accumulate(dp))] print((dp[K]%MOD))
from sys import stdin from itertools import accumulate input = stdin.readline MOD = 10**9+7 N, K = list(map(int,input().split())) *a, = list(map(int,input().split())) p = list(accumulate(a)) dp = [0] * (K+1) dp[0] = 1 prefix = [0]+[1]*(K+1) for i in range(N): new_dp = [0]*(K+1) for j in range(min(p[i],K)+1): new_dp[j] = prefix[j+1] - prefix[max(0,j-a[i])] dp = new_dp[:] prefix = [i % MOD for i in [0]+list(accumulate(dp))] print((dp[K]%MOD))
p03172
def main(): N, K = list(map(int, input().split())) a = list(map(int, input().split())) dp = [[0]*(K+1) for i in range(N+1)] import collections mod = 10**9 + 7 # dp[i][j] i人目まで配って、残りがj個の方法の数 s = [1]*(K+2) s[-1] = 0 tmp = 0 for i in range(1, N+1): next = [0]*(K+2) for j in range(K, -1, -1): tmp = s[j] - s[min(K, j+a[i-1]) + 1] tmp %= mod dp[i][j] = tmp next[j] = (next[j+1]+tmp) % mod s = list(next) print(tmp) main()
def main(): N, K = list(map(int, input().split())) a = list(map(int, input().split())) dp = [[0]*(K+1) for i in range(N+1)] import collections dp[0][K] = 1 mod = 10**9 + 7 # dp[i][j] i人目まで配って、残りがj個の方法の数 s = [0]*(K+2) for i in range(1, N+1): tmp = 0 for j in range(K, -1, -1): s[j] = (s[j+1] + dp[i-1][j]) % mod for j in range(K, -1, -1): tmp = s[j] - s[min(K, j+a[i-1]) + 1] tmp %= mod dp[i][j] = tmp print((dp[-1][0])) main()
p03172
def main(): N, K = list(map(int, input().split())) A = list(map(int, input().split())) u = [1] + [0] * K v = [1] + [0] * K s = 0 for i in A: u, v = v, u s += i t = 0 for k in range(i): t += u[k] v[k] = t for k in range(i, min(s, K) + 1): t += u[k] v[k] = t t -= u[k - i] print((v[K] % (10 ** 9 + 7))) main()
def main(): N, K = list(map(int, input().split())) A = list(map(int, input().split())) u = [1] + [0] * K v = [1] + [0] * K s = 1 for a in A: u, v = v, u s += a c = 0 for i, t in enumerate(u[:a + 1]): c += t v[i] = c for i, t, tt in zip(list(range(a + 1, K + 1)), u[a + 1:s], u): c += t - tt v[i] = c print((v[K] % (10 ** 9 + 7))) main()
p03172
import sys readline = sys.stdin.readline N,K = list(map(int, readline().split())) A = list(map(int,readline().split())) DIV = 10 ** 9 + 7 # dp[i][j] : i人目までにj個配るときの場合の数 # 数式変形で以下 # dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - Ai-1 - 1] dp = [[0] * (K + 1) for i in range(N + 1)] for i in range(N + 1): dp[i][0] = 1 # 0個配る方法は常に1通り for i in range(1, N + 1): for j in range(1, K + 1): if j - A[i - 1] - 1 >= 0: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - A[i - 1] - 1]) % DIV else: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % DIV print((dp[N][K]))
import sys readline = sys.stdin.readline N,K = list(map(int, readline().split())) A = list(map(int,readline().split())) DIV = 10 ** 9 + 7 # dp[i][j] : i人目までにj個配るときの場合の数 # dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + ... + dp[i - 1][j - A[i - 1]] # j に j - 1を代入 # dp[i][j - 1] = dp[i - 1][j - 1] + dp[i - 1][j - 2] + ... + dp[i - 1][j - A[i - 1] - 1] # 上から下を引く # dp[i][j] - dp[i][j - 1] = dp[i - 1][j] - dp[i - 1][j - A[i - 1] - 1] # dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - Ai-1 - 1] dp = [[0] * (K + 1) for i in range(N + 1)] for i in range(N + 1): dp[i][0] = 1 # 0個配る方法は常に1通り for i in range(1, N + 1): for j in range(1, K + 1): if j - A[i - 1] - 1 >= 0: # j個配れる場合 (j >= A[i - 1] + 1) dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - A[i - 1] - 1]) % DIV else: dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % DIV print((dp[N][K]))
p03172
import sys input = sys.stdin.readline n,k=list(map(int, input().split())) a=list(map(int, input().split())) mod=10**9+7 dp=[[0]*(k+1) for i in range(n+1)] for i in range(n+1): dp[i][0]=1 for i in range(1,n+1): for j in range(1,k+1): if j-1-a[i-1]>=0: dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1-a[i-1]] dp[i][j]%mod else: dp[i][j]=dp[i][j-1]+dp[i-1][j] dp[i][j]%mod print((dp[-1][-1]%mod))
import sys input = sys.stdin.readline n,k=list(map(int, input().split())) a=list(map(int, input().split())) mod=10**9+7 dp=[[0]*(k+1) for i in range(n+1)] for i in range(n+1): dp[i][0]=1 for i in range(1,n+1): for j in range(1,k+1): if j-1-a[i-1]>=0: dp[i][j]=(dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1-a[i-1]])%mod else: dp[i][j]=(dp[i][j-1]+dp[i-1][j])%mod print((dp[-1][-1]%mod))
p03172
import collections def solve(): A, B = list(map(int, input().split())) print((A*B-(A+B-1))) if __name__ == "__main__": solve()
A, B = list(map(int, input().split())) print(((A-1)*(B-1)))
p03280
a,b=list(map(int, input().split())) print(((a-1)*(b-1)))
a,b=list(map(int,input().split())) print((~-a*~-b))
p03280
import subprocess A,B = list(map(int, subprocess.check_output("dd").split())) print((A-1)*(B-1))
import sys A,B = list(map(int, sys.stdin.read().split())) print((A-1)*(B-1))
p03280
a,b=list(map(int,input().split())) print((~-a*~-b))
A, B = list(map(int, input().split())) print((A*B-(A+B-1)))
p03280
import sys # 関数 solve は,もちろん,問題に応じて書き換える def solve(a, b): s=int(a)-1 r=int(b)-1 return s*r # ここから下は,入力・出力形式が同じであれば,変えなくて良い. def readQuestion(): line = sys.stdin.readline().rstrip() [str_a, str_b] = line.split(' ') return (int(str_a), int(str_b)) def main(): a, b = readQuestion() answer = solve(a, b) print(answer) if __name__ == '__main__': main()
import sys def solve(a, b): # Uncomment the following if you want to check the input. # print('a =', a) # print('b =', b) s=int(a)-1 r=int(b)-1 return s*r def readQuestion(): ws = sys.stdin.readline().strip().split() a = int(ws[0]) b = int(ws[1]) return (a, b) def main(): print((solve(*readQuestion()))) # Uncomment before submission main()
p03280
a,b=list(map(int,input().split())) c=(a*b)-(b+a-1) print(c)
a,b=list(map(int,input().split())) print((a*b-a-b+1))
p03280
eval('print(('+input().replace(' ','-1)*(')+'-1))')
a,b=list(map(int,input().split()));print((~-a*~-b))
p03280
print((eval('~-'+'*~-'.join(input().split()))))
a,b=list(map(int,input().split())) print((~-a*~-b))
p03280
A,B=list(map(int,input().split())) print(((A-1) * (B-1)))
print((eval('('+input().replace(' ','-1)*(')+'-1)')))
p03280
a, b = list(map(int, input().split())) print(((a-1) * (b-1)))
a,b=list(map(int,input().split()));print((a*b-a-b+1))
p03280
a,b=list(map(int,input().split())) print((a*b-a-b+1))
A,B=list(map(int,input().split())) print((A*B-A-B+1))
p03280
a, b = list(map(int, input().split())) print(((a - 1) * (b - 1)))
A, B = list(map(int, input().split())) print(((A - 1) * (B - 1)))
p03280
import sys sys.setrecursionlimit(100000) S = input() T = "" t = ['dream','dreamer','erase','eraser'] def rec(T,S): if len(S) < len(T): return False if S==T: return True elif S[len(T)] == "e": return rec(T+t[2],S) or rec(T+t[3],S) elif S[len(T)] == "d": return rec(T+t[0],S) or rec(T+t[1],S) else: return False if rec(T,S): print("YES") else: print("NO")
S = input().split()[0] t = ['dreameraser','dreamerase','dreamer','dream','eraser','erase'] while len(S) > 0: for i in t: if S[-len(i):]==i: S = S[:-len(i)] break elif i == t[-1]: print("NO") exit() print("YES")
p03856
N=int(input()) A=list(map(int,input().split())) B=[] for i in range(N): if(N%2==0): if(i%2==0): B.append(A[i]) else: B.insert(0,A[i]) else: if(i%2==0): B.insert(0,A[i]) else: B.append(A[i]) for i in range(N): print(B[i] if i==0 else " "+str(B[i]),end="") print()
N=int(input()) A=list(map(int,input().split())) B=[] C=[] for i in range(N): if(N%2==0): if(i%2==0): B.append(A[i]) else: C.append(A[i]) else: if(i%2==0): C.append(A[i]) else: B.append(A[i]) C.reverse() for i in range(len(B)): C.append(B[i]) for i in range(N): print(C[i] if i==0 else " "+str(C[i]),end="") print()
p03675
N = int(eval(input())) A = list(map(int, input().split())) ans = [A[0]] for i in range(1, N): if i % 2 == 0: ans.append(A[i]) else: ans.insert(0, A[i]) if N % 2 == 1: ans = ans[::-1] print((" ".join(list(map(str, ans)))))
N = int(eval(input())) A = list(map(int, input().split())) ans = [] if N % 2 == 0: l1 = A[0::2] l2 = A[1::2] l2 = l2[::-1] ans = l2+l1 else: l1 = A[0::2] l2 = A[1::2] l1 = l1[::-1] ans = l1+l2 print((" ".join(list(map(str, ans)))))
p03675
N = int(eval(input())) A = list(map(int, input().split())) ans = [] if N % 2 == 0: l1 = A[0::2] l2 = A[1::2] l2 = l2[::-1] ans = l2+l1 else: l1 = A[0::2] l2 = A[1::2] l1 = l1[::-1] ans = l1+l2 print((" ".join(list(map(str, ans)))))
N = int(eval(input())) A = list(map(int, input().split())) ans = [] l1 = A[0::2] l2 = A[1::2] if N % 2 == 0: l2 = l2[::-1] ans = l2+l1 else: l1 = l1[::-1] ans = l1+l2 print((" ".join(list(map(str, ans)))))
p03675
N = int(eval(input())) A = list(map(int, input().split())) ans = [] l1 = A[0::2] l2 = A[1::2] if N % 2 == 0: l2 = l2[::-1] ans = l2+l1 else: l1 = l1[::-1] ans = l1+l2 print((" ".join(list(map(str, ans)))))
N = int(eval(input())) A = list(input().split()) ans = [] l1 = A[0::2] l2 = A[1::2] if N % 2 == 0: l2 = l2[::-1] ans = l2+l1 else: l1 = l1[::-1] ans = l1+l2 print((" ".join(ans)))
p03675
N = int(eval(input())) a = list(map(int,input().split())) ans = [a[0]] for i,j in enumerate(a[1:]): if(N%2==0): if(i%2==0): ans.insert(0,j) else: ans.append(j) else: if(i%2==1): ans.insert(0,j) else: ans.append(j) print((" ".join(map(str,ans))))
N = int(eval(input())) a = list(map(int,input().split())) ans = [a[0]] check = N%2==0 for i,j in enumerate(a[1:]): if(check): if(i%2==0): ans.insert(0,j) else: ans.append(j) else: if(i%2==1): ans.insert(0,j) else: ans.append(j) print((" ".join(map(str,ans))))
p03675
import sys import collections import math n = int(eval(input())) A = [int(x) for x in input().split()] R = [0] * n idx = 0 for i in range(n-1, -1, -2): R[idx] = A[i] idx += 1 for i in range(n % 2, n, +2): R[idx] = A[i] idx += 1 print((" ".join([str(i) for i in R]))) sys.exit(0)
import sys import collections import math from collections import Counter from collections import deque n = int(eval(input())) A = [int(x) for x in input().split()] Q = deque() f = True if n % 2 == 0: f = not f for a in A: if f: Q.appendleft(a) else: Q.append(a) f = not f print((" ".join([str(q) for q in Q]))) sys.exit(0)
p03675
n = int(eval(input())) a = list(map(int,input().split())) even = [] odd = [] for i in range(0,n): if i % 2 == 0: odd.append(a[i]) else: even.append(a[i]) res = [] if n % 2 == 0: even = reversed(even) res.extend(even) res.extend(odd) else: odd = reversed(odd) res.extend(odd) res.extend(even) print((" ".join(map(str,res))))
n = int(eval(input())) a = list(map(int,input().split())) if n == 1: print((a[0])) elif n % 2 == 0: print((" ".join(map(str, a[1::2][::-1] + a[0::2])))) else: print((" ".join(map(str, a[0::2][::-1] + a[1::2]))))
p03675
n = int(eval(input())) *a, = list(map(int, input().split())) b = [] for i in range(n): b.append(a[i]) b.reverse() print((" ".join(map(str, b))))
n = int(eval(input())) *a, = list(map(int, input().split())) b = list(reversed(a[1::2]))+a[0::2] print((" ".join(map(str, b if n%2 == 0 else reversed(b)))))
p03675
N=input() b=[] P=input().split() for i in P: b.append(i) b=b[::-1] print(" ".join(b))
N=int(input()) P=input().split() b=P[-1::-2]+P[N%2::2] print(" ".join(b))
p03675
a = int(input()) ar = list(map(int,input().split(" "))) br = [] if a % 2 == 0: for i in range(len(ar)-1,0,-2): br.append(ar[i]) for i in range(0,len(ar)-1,+2): br.append(ar[i]) else: for i in range(len(ar)-1,-1,-2): br.append(ar[i]) for i in range(1,len(ar)-1,+2): br.append(ar[i]) for i in range(a-1): print(str(br[i]),end = " ") print(str(br[a-1]))
a = int(input()) ar = list(map(int,input().split(" "))) br = [] for i in range(a-1,-1,-2): br.append(ar[i]) if a % 2 == 0: n = 0 else: n = 1 for i in range(n,a-1,2): br.append(ar[i]) for i in range(a-1): print(br[i],end=" ") print(br[a-1])
p03675
n = int(input()) a = input().split() b = [] for i in range(n): if i % 2: b.append(a[i]) else: b.insert(0, a[i]) if n % 2: print(" ".join(b)) else: print(" ".join(b[::-1]))
n = int(input()) a = input().split() b = [] c = [] for i in range(n): if i % 2: b.append(a[i]) else: # b.insert(0, a[i]) c.append(a[i]) b = c[::-1] + b if n % 2: print(" ".join(b)) else: print(" ".join(b[::-1]))
p03675
from itertools import count def get_weed(h): d10 = h[1] - h[0] d21 = h[2] - h[1] d32 = h[3] - h[2] if d10 == d21 == d32: for hi, expect in zip(h, count(h[0], d10)): if hi != expect: return hi d43 = h[4] - h[3] if d21 == d32 == d43: return h[0] elif h[2] - h[0] == d32 == d43: return h[1] elif d10 == h[3] - h[1] == d43: return h[2] elif d10 == d21 == h[4] - h[2]: return h[3] import sys f = sys.stdin while True: n = int(f.readline()) if n == 0: break h = list(map(int, f.readline().split())) print((get_weed(h)))
from itertools import count def get_weed(h): d = [h[i + 1] - h[i] for i in range(3)] if d[0] == d[1] == d[2]: for hi, expect in zip(h, count(h[0], d[0])): if hi != expect: return hi for i in range(4): hh = h[:i] + h[i + 1:] d = [hh[i + 1] - hh[i] for i in range(3)] if d[0] == d[1] == d[2]: return h[i] import sys f = sys.stdin while True: n = int(f.readline()) if n == 0: break h = list(map(int, f.readline().split())) print((get_weed(h)))
p00253
from collections import defaultdict from heapq import * from array import * def read(): return int(eval(input())) def reads(sep=None): return list(map(int, input().split(sep))) def mst_prim(adj, n): pred = defaultdict(lambda: -1) key = defaultdict(lambda: float('inf')) key[0] = 0 pq = [(key[v], v) for v in range(n)] heapify(pq) in_pq = array('B', (True for _ in range(n))) while pq: _,u = heappop(pq) in_pq[u] = False for v, v_cost in adj[u]: if in_pq[v]: weight = v_cost if weight < key[v]: pred[v] = u key[v] = weight heappush(pq, (weight, v)) in_pq[v] = True return (pred, key) def main(): w, h = reads() p = [read() for _ in range(w)] q = [read() for _ in range(h)] adj = defaultdict(set) for x in range(w+1): for y in range(h+1): if x < w: adj[x+y*(w+1)].add((x+1+y*(w+1), p[x])) adj[x+1+y*(w+1)].add((x+y*(w+1), p[x])) if y < h: adj[x+y*(w+1)].add((x+(y+1)*(w+1), q[y])) adj[x+(y+1)*(w+1)].add((x+y*(w+1), q[y])) print((sum(mst_prim(adj, (w+1)*(h+1))[1].values()))) main()
def read(): return int(eval(input())) def reads(sep=None): return list(map(int, input().split(sep))) def main(): w, h = reads() p = sorted([read() for _ in range(w)])[::-1] q = sorted([read() for _ in range(h)])[::-1] pi = w-1 qi = h-1 res = 0 while 0<=pi and 0<=qi: if p[pi] < q[qi]: res += p[pi] * (h+1) pi -= 1 w -= 1 else: res += q[qi] * (w+1) qi -= 1 h -= 1 res += sum(p[:pi+1]) + sum(q[:qi+1]) print(res) main()
p03972
W, H = list(map(int, input().split())) P = [(int(eval(input())), 0) for i in range(W)] Q = [(int(eval(input())), 1) for i in range(H)] E = sorted(P + Q) used = [W + 1, H + 1] ans = 0 for e, f in E: ans += e * used[1 - f] used[f] -= 1 print(ans)
from itertools import accumulate from bisect import bisect_left W, H = list(map(int, input().split())) W_cost = sorted([int(eval(input())) for i in range(W)]) H_cost = [int(eval(input())) for i in range(H)] # 横は全部辺を張り、縦は各区間1本 W_cost_sum = sum(W_cost) ans = W_cost_sum * (H + 1) + sum(H_cost) W_acc_cost = [0] + list(accumulate(W_cost)) for h in H_cost: hx = bisect_left(W_cost, h) ans -= (W_cost_sum - W_acc_cost[hx]) - ((W - hx) * h) print(ans)
p03972
w, h = list(map(int, input().split())) p = [] q = [] for i in range(w): p.append(int(input())) for i in range(h): q.append(int(input())) wl = w hl = h cost = 0 p.sort() q.sort() p.append(10 ** 8 + 1) q.append(10 ** 8 + 1) while(wl + hl > 0): if p[0] < q[0]: cost += (hl + 1) * p[0] p.pop(0) wl -= 1 else: cost += (wl + 1) * q[0] q.pop(0) hl -= 1 print(cost)
w, h = list(map(int, input().split())) p = [] q = [] for i in range(w): p.append(int(input())) for i in range(h): q.append(int(input())) wl = w hl = h cost = 0 p.sort() q.sort() p.append(10 ** 8 + 1) q.append(10 ** 8 + 1) pc = 0 qc = 0 while(wl + hl - pc - qc> 0): if p[pc] < q[qc]: cost += (hl - qc + 1) * p[pc] pc += 1 else: cost += (wl - pc + 1) * q[qc] qc += 1 print(cost)
p03972
W,H=list(map(int,input().split())) P=[int(eval(input())) for i in range(W)] Q=[int(eval(input())) for j in range(H)] P.append(float('inf')) Q.append(float('inf')) P.sort(reverse=True) Q.sort(reverse=True) now_W=W+1 now_H=H+1 cost=0 while now_W>1 or now_H>1: if P[-1]<Q[-1]: cost+=P.pop()*now_H now_W-=1 elif Q[-1]<P[-1]: cost+=Q.pop()*now_W now_H-=1 elif P[-1]==Q[-1]: if now_W<=now_H: cost+=Q.pop()*now_W now_H-=1 elif now_W>now_H: cost+=P.pop()*now_H now_W-=1 print(cost)
import sys input=sys.stdin.readline W,H=list(map(int,input().split())) P=[int(eval(input())) for _ in range(W)]+[float('inf')] Q=[int(eval(input())) for _ in range(H)]+[float('inf')] P.sort(reverse=True) Q.sort(reverse=True) ans=0 W+=1 H+=1 while True: if W==H==1: break elif Q[-1]<P[-1] or (Q[-1]==P[-1] and W<=H): ans+=W*Q[-1] H-=1 Q.pop() elif Q[-1]>P[-1] or (Q[-1]==P[-1] and W>H): ans+=H*P[-1] W-=1 P.pop() print(ans)
p03972
W,H = list(map(int,input().split())) side = [] for i in range(W): side.append({"cost":int(eval(input())),"num":i,"dir":0}) for i in range(H): side.append({"cost":int(eval(input())),"num":i,"dir":1}) side = sorted(side,key = lambda x:x["cost"]) node =[[{"v":j,"men":[j]} for j in range(i*(W+1),(i+1)*(W+1))]for i in range(H+1)] ans = 0 for s in side: cost,num,di = s["cost"],s["num"],s["dir"] if di == 0 : for i in range(H+1): if node[i][num]["v"] != node[i][num+1]["v"]: # 親が違ったら # print(i,num,0) ans += cost v1 = node[i][num]["v"] v2 = node[i][num+1]["v"] for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え node[j//(W+1)][j%(W+1)]["v"] = v1 node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"] else: for i in range(W+1): if node[num][i]["v"] != node[num+1][i]["v"]: # 親が違ったら # print(num,i,1) ans += cost v1 = node[num][i]["v"] v2 = node[num+1][i]["v"] for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え node[j//(W+1)][j%(W+1)]["v"] = v1 node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"] print(ans)
W, H = list(map(int, input().split())) side = [{"cost":int(eval(input())), "di":0}for i in range(W)] + [{"cost":int(eval(input())), "di":1}for i in range(H)] side = sorted(side, key=lambda x:x["cost"]) ans = 0 ty = [H+1,W+1] for s in side: cost, di = s["cost"], s["di"] ans += ty[di] * cost ty[(di+1)%2] -= 1 print(ans)
p03972
def main(): import sys input = sys.stdin.readline from bisect import bisect_left, bisect_right W, H = list(map(int, input().split())) P = [int(eval(input())) for _ in range(W)] P.sort() P_count = [0] * (W + 2) Q = [int(eval(input())) for _ in range(H)] Q.sort() ans = 0 for q in Q: tmp = bisect_right(P, q) P_count[tmp] += 1 ans += q * (1 + W - tmp) for i in range(W, - 1, -1): P_count[i] += P_count[i + 1] # print ('途中', ans) # for p in P: # tmp = bisect_left(Q, p) # ans += p * (1 + H - tmp) for index, p in enumerate(P): ans += p * (P_count[index + 1] + 1) print (ans) # print ('P:', P) # print ('Q:', Q) if __name__ == '__main__': main()
def main(): import sys input = sys.stdin.readline from bisect import bisect_right W, H = list(map(int, input().split())) P = [int(eval(input())) for _ in range(W)] P.sort() P_count = [0] * (W + 2) Q = [int(eval(input())) for _ in range(H)] Q.sort() ans = 0 for q in Q: tmp = bisect_right(P, q) P_count[tmp] += 1 ans += q * (1 + W - tmp) for i in range(W, - 1, -1): P_count[i] += P_count[i + 1] for index, p in enumerate(P): ans += p * (P_count[index + 1] + 1) print (ans) if __name__ == '__main__': main()
p03972
ss=input().split() w=int(ss[0]) h=int(ss[1]) a=[] b=[] for i in range( w): p=eval(input()) a.append( p) for i in range( h): p=eval(input()) b.append( p) a.sort() b.sort() h1=0 h2=0 ans=0 while ((h1<w) and (h2<h)): if a[h1]<b[h2]: ans+=a[h1]*(h - h2+1) h1+=1 else : ans += b[h2] * (w-h1+1) h2 += 1 while (h1<w): ans+=a[h1] h1+=1 while (h2<h): ans+=b[h2] h2+=1 print(ans)
ss=input().split() w=int(ss[0]) h=int(ss[1]) a=[] b=[] for i in range(w): p=eval(input()) a.append(p) for i in range(h): p=eval(input()) b.append(p) a.sort() b.sort() h1=0 h2=0 res=0 while h1 < w and h2 < h: if a[h1]<b[h2]: res += a[h1] * (h - h2 + 1) h1 += 1 else: res += b[h2] * (w - h1 + 1) h2 += 1 while (h1<w): res += a[h1] h1 += 1 while (h2<h): res += b[h2] h2 += 1 print(res)
p03972
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline INF = 2**62-1 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(W, H, P, Q): W += 1 H += 1 ans = 0 for i, p in enumerate(P): ans += p for i, q in enumerate(Q): ans += q for i in range(1, H): for j in range(1, W): if P[j-1] < Q[i-1]: ans += P[j-1] else: ans += Q[i-1] return ans def main(): W, H = read_int_n() P = [read_int() for _ in range(W)] Q = [read_int() for _ in range(H)] print(slv(W, H, P, Q)) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline INF = 2**62-1 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(W, H, P, Q): W += 1 H += 1 ans = sum(P) + sum(Q) P.sort() Q.sort() # print(P) for q in Q: i = bisect.bisect_left(P, q) # print(i, q) ans += q * (len(P)-i) for p in P: i = bisect.bisect_right(Q, p) # print(i, p) ans += p * (len(Q)-i) return ans def main(): W, H = read_int_n() P = [read_int() for _ in range(W)] Q = [read_int() for _ in range(H)] print(slv(W, H, P, Q)) if __name__ == '__main__': main()
p03972
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline INF = 2**62-1 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(W, H, P, Q): W += 1 H += 1 ans = sum(P) + sum(Q) P.sort() Q.sort() # print(P) for q in Q: i = bisect.bisect_left(P, q) # print(i, q) ans += q * (len(P)-i) for p in P: i = bisect.bisect_right(Q, p) # print(i, p) ans += p * (len(Q)-i) return ans def main(): W, H = read_int_n() P = [read_int() for _ in range(W)] Q = [read_int() for _ in range(H)] print(slv(W, H, P, Q)) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline INF = 2**62-1 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(W, H, P, Q): ans = sum(P) + sum(Q) P.sort() Q.sort() for q in Q: i = bisect.bisect_left(P, q) ans += q * (len(P)-i) for p in P: i = bisect.bisect_right(Q, p) ans += p * (len(Q)-i) return ans def main(): W, H = read_int_n() P = [read_int() for _ in range(W)] Q = [read_int() for _ in range(H)] print(slv(W, H, P, Q)) if __name__ == '__main__': main()
p03972
import itertools import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 class UnionFind: def __init__(self, size=None, nodes=None): """ size か nodes どっちか指定。 nodes は set、size は list を使う。 set の最悪計算量は O(N) なので size 指定のほうが若干速い :param int size: :param collections.Iterable nodes: """ assert size is not None or nodes is not None if size is not None: self._parents = [i for i in range(size)] self._ranks = [0 for _ in range(size)] self._sizes = [1 for _ in range(size)] else: self._parents = {k: k for k in nodes} self._ranks = {k: 0 for k in nodes} self._sizes = {k: 1 for k in nodes} def unite(self, x, y): """ x が属する木と y が属する木を併合 :param x: :param y: :return: """ x = self.root(x) y = self.root(y) if x == y: return # rank が小さい方が下 if self._ranks[x] > self._ranks[y]: # x が root self._parents[y] = x self._sizes[x] += self._sizes[y] else: # y が root self._parents[x] = y self._sizes[y] += self._sizes[x] if self._ranks[x] == self._ranks[y]: self._ranks[y] += 1 def root(self, x): """ x が属する木の root :param x: :return: """ if self._parents[x] == x: return x self._parents[x] = self.root(self._parents[x]) return self._parents[x] def size(self, x): """ x が属する木のノード数 :param x: :return: """ return self._sizes[self.root(x)] H, W = list(map(int, sys.stdin.readline().split())) P = [int(sys.stdin.readline()) for _ in range(W)] Q = [int(sys.stdin.readline()) for _ in range(H)] def test(): edges = [] vs = [] for h, w in itertools.product(list(range(H + 1)), list(range(W + 1))): if w < W: edges.append((P[w], (w, h), (w + 1, h))) if h < H: edges.append((Q[h], (w, h), (w, h + 1))) for h, w in itertools.product(list(range(H + 1)), list(range(W + 1))): vs.append((w, h)) edges.sort() # kruskal uf = UnionFind(nodes=vs) ret = 0 for s, v, u in edges: if uf.root(v) != uf.root(u): uf.unite(v, u) ret += s return ret # 横棒 hs = [] for w in range(W): hs.append((P[w], w, w + 1)) # 縦棒 vs = [] for h in range(H): vs.append((Q[h], h, h + 1)) hs.sort(reverse=True) vs.sort(reverse=True) ans = 0 v_nodes = W + 1 h_nodes = H + 1 while hs or vs: if not vs or (hs and hs[-1] < vs[-1]): # 横棒を使う s, i, j = hs.pop() # まだくっつけてない横軸の数だけ辺を張る ans += s * h_nodes v_nodes -= 1 else: # 縦棒のが短い s, i, j = vs.pop() ans += s * v_nodes h_nodes -= 1 # print(ans) print((test()))
import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 # あああああああああああくそHとWぎゃくだ W,H = list(map(int, sys.stdin.readline().split())) P = [int(sys.stdin.readline()) for _ in range(W)] Q = [int(sys.stdin.readline()) for _ in range(H)] edges = [] for p in P: edges.append((p, 'P')) for q in Q: edges.append((q, 'Q')) edges.sort() ans = 0 a = W + 1 b = H + 1 for e, pq in edges: if pq == 'P': ans += e * b a -= 1 else: ans += e * a b -= 1 print(ans)
p03972
W,H = list(map(int,input().split())) PQ = [(int(eval(input())),0,i) for i in range(W)] + [(int(eval(input())),1,i) for i in range(H)] PQ.sort() import sys sys.setrecursionlimit(10**7) class UnionFind: def __init__(self): self.Parent = {} def add(self,k): self.Parent[k] = k def get_Parent(self,n): if self.Parent[n] == n:return n p = self.get_Parent(self.Parent[n]) self.Parent[n] = p return p def merge(self,x,y): x = self.get_Parent(x) y = self.get_Parent(y) if x!=y: self.Parent[y] = x return def is_united(self,x,y): return self.get_Parent(x)==self.get_Parent(y) ans = 0 T = UnionFind() for i in range(W+1): for j in range(H+1): T.add((i,j)) for c,pq,ij in PQ: if pq==0: i = ij for j in range(H+1): if not T.is_united((i,j),(i+1,j)): T.merge((i,j),(i+1,j)) ans += c else: j = ij for i in range(W+1): if not T.is_united((i,j),(i,j+1)): T.merge((i,j),(i,j+1)) ans += c print(ans)
W,H = list(map(int,input().split())) PQ = [(int(eval(input())),0) for _ in [0]*W] + [(int(eval(input())),1) for _ in [0]*H] PQ.sort() r = H+1 c = W+1 ans = 0 for d,pq in PQ: if pq==0: ans += d*r c -=1 else: ans += d*c r -=1 print(ans)
p03972
t = input() W, H = list(map(int, t.split(' '))) p, q = [], [] for i in range(W): p.append(int(input())) for j in range(H): q.append(int(input())) node = [ (i, j, min(p[i-1] if i > 0 else 100000005, q[j-1] if j > 0 else 100000005)) for i in range(W+1) for j in range(H+1) ] cost = 0 for i in range(W*H+W+H): cost += node[i+1][2] print(cost)
t = input() W, H = list(map(int, t.split(' '))) p, q = [], [] for i in range(W): p.append(int(input())) for j in range(H): q.append(int(input())) cost = 0 for i in range(W+1): for j in range(H+1): if i == 0 and j == 0: continue cost += min(p[i-1] if i > 0 else 100000005, q[j-1] if j > 0 else 100000005) print(cost)
p03972
t,*p=open(0) w,h=list(map(int,t.split())) c=0 for v,f in sorted((t,i<w)for i,t in enumerate(map(int,p))): c-=~h*v*f or~w*v w-=f h-=1^f print(c)
t,*p=open(0) w,h=list(map(int,t.split())) c=0 for v,f in sorted((int(t),i<w)for i,t in enumerate(p)): c-=~h*v*f or~w*v w-=f h-=1^f print(c)
p03972
W, H = list(map(int, input().split())) p = sorted([(i, int(input())) for i in range(W)], key=lambda x:-x[1]) q = sorted([(i, int(input())) for i in range(H)], key=lambda x:-x[1]) class UnionFind(object): def __init__(self, n): self.uf = [-1 for _ in range(n)] def union(self, x, y): x = self.find(x[0] + x[1]*(W+1)) y = self.find(y[0] + y[1]*(W+1)) if x == y: return False if self.uf[x] >= self.uf[y]: self.uf[y] += self.uf[x] self.uf[x] = y else: self.uf[x] += self.uf[y] self.uf[y] = x return True def find(self, x): leaves = [] while self.uf[x] >= 0: leaves.append(x) x = self.uf[x] for lf in leaves: self.uf[lf] = x return x uf = UnionFind((W+1)*(H+1)) ans = 0 while len(p) > 0 or len(q) > 0: if len(q) == 0 or (len(p) > 0 and p[-1][1] <= q[-1][1]): i, cost = p.pop() for j in range(H+1): if uf.union((i, j), (i+1, j)): ans += cost else: j, cost = q.pop() for i in range(W+1): if uf.union((i, j), (i, j+1)): ans += cost print(ans)
W, H = list(map(int, input().split())) p = sorted([(i, int(input())) for i in range(W)], key=lambda x:-x[1]) q = sorted([(i, int(input())) for i in range(H)], key=lambda x:-x[1]) ans = 0 pe, qe, ae = 0, 0, (W+1)*(H+1)-1 while len(p)> 0 or len(q) > 0: if len(q) == 0 or (len(p) > 0 and p[-1][1] <= q[-1][1]): i, cost = p.pop() pe += 1 ans += cost*(H+1-qe) else: j, cost = q.pop() qe += 1 ans += cost*(W+1-pe) print(ans)
p03972
n,q=list(map(int,input().split())) exist=set([1]) cup=list(range(n+2)) now=1 exist.add(cup[now-1]) exist.add(cup[now+1]) for i in range(q): a,b=list(map(int,input().split())) if now==a:now=b elif now==b:now=a tmp=cup[a] cup[a]=cup[b] cup[b]=tmp exist.add(cup[now-1]) exist.add(cup[now+1]) exist=list(exist) ans=0 for i in range(len(exist)): if exist[i]!=0 and exist[i]!=n+1:ans+=1 print(ans)
n,q=list(map(int,input().split())) exist=[False]*(n+2) exist[1]=True cup=list(range(n+2)) now=1 exist[cup[now-1]]=True exist[cup[now+1]]=True for i in range(q): a,b=list(map(int,input().split())) if now==a:now=b elif now==b:now=a tmp=cup[a] cup[a]=cup[b] cup[b]=tmp exist[cup[now-1]]=True exist[cup[now+1]]=True ans=0 for i in range(1,n+1): if exist[i]:ans+=1 print(ans)
p03894
j=lambda:list(map(int,input().split())) n,q=j() c=[1,-1,1]+[0]*(n-2)+[1] for p in[-1]*q:a,b=j();c[a],c[b]=c[b],c[a];i=c.index(p);c[i-1]=c[i+1]=1 print(sum(c))
j=lambda:list(map(int,input().split())) n,q=j() u=1 c=[1]*3+[0]*(n-2)+[1] for p in[-1]*q:a,b=d=j();u+=u in d and a+b-2*u;c[a],c[b]=c[b],c[a];c[u-1]=c[u+1]=1 print(sum(c)-2)
p03894
#!/usr/bin/env python from collections import deque import itertools as it import sys sys.setrecursionlimit(1000000) while True: S_ = input() if S_ == '.': break ans = 0 for p, q, r in it.product([0, 1, 2], repeat = 3): S = S_ S = S.replace("P", str(p)).replace("Q", str(q)).replace("R", str(r)) for loop in range(100): S = S.replace("-0", "2") S = S.replace("-1", "1") S = S.replace("-2", "0") for a, b in it.product([0, 1, 2], repeat = 2): S = S.replace("(%d*%d)" % (a, b), str(min(a, b))) S = S.replace("(%d+%d)" % (a, b), str(max(a, b))) if S == "2": ans += 1 print(ans)
#!/usr/bin/env python from collections import deque import itertools as it import sys sys.setrecursionlimit(1000000) while True: S_ = input() if S_ == '.': break ans = 0 for p, q, r in it.product([0, 1, 2], repeat = 3): S = S_ S = S.replace("P", str(p)).replace("Q", str(q)).replace("R", str(r)) while len(S) != 1: S = S.replace("-0", "2") S = S.replace("-1", "1") S = S.replace("-2", "0") for a, b in it.product([0, 1, 2], repeat = 2): S = S.replace("(%d*%d)" % (a, b), str(min(a, b))) S = S.replace("(%d+%d)" % (a, b), str(max(a, b))) if S == "2": ans += 1 print(ans)
p00736
import sys import itertools input = sys.stdin.readline sys.setrecursionlimit(100000) mod = 10 ** 9 + 7 def read_values(): return list(map(int, input().split())) def read_index(): return [int(x) - 1 for x in input().split()] def read_list(): return list(read_values()) def read_lists(N): return [read_list() for n in range(N)] class V: def __init__(self, f, v=None): self.f = f self.v = v def __str__(self): return str(self.v) def ud(self, n): if self.v is None: self.v = n else: self.v = self.f(self.v, n) def main(): N = int(eval(input())) S = read_list() res = V(max, 0) for c in range(1, N): r = 0 t1 = 0 t2 = N - 1 a = t2 - c while True: if a < c: break if (N - 1) % c == 0 and 2 * a <= N - 1: break t1 += c t2 -= c a -= c r += S[t1] + S[t2] res.ud(r) print(res) if __name__ == "__main__": main()
import sys import itertools input = sys.stdin.readline sys.setrecursionlimit(100000) mod = 10 ** 9 + 7 def read_values(): return list(map(int, input().split())) def read_index(): return [int(x) - 1 for x in input().split()] def read_list(): return list(read_values()) def read_lists(N): return [read_list() for n in range(N)] class V: def __init__(self, f, v=None): self.f = f self.v = v def __str__(self): return str(self.v) def ud(self, n): if self.v is None: self.v = n else: self.v = self.f(self.v, n) def main(): N = int(eval(input())) S = read_list() res = V(max, 0) for c in range(1, N): r = 0 t1 = 0 t2 = N - 1 while True: t1 += c t2 -= c if t2 < c: break if (N - 1) % c == 0 and 2 * t2 <= N - 1: break r += S[t1] + S[t2] res.ud(r) print(res) if __name__ == "__main__": main()
p03034
N = int(eval(input())) *S, = list(map(int, input().split())) ans = 0 for a in range(1, N): d = 1 while d*d <= N-1-a: if (N-1-a) % d == 0: if a > d and (a % d > 0 or N-1-a < a): k = (N-1-a) // d s = 0 for i in range(k): s += S[i*d+d] + S[i*d+a] ans = max(ans, s) e = (N-1-a) // d if a > e and (a % e > 0 or N-1-a < a): k = (N-1-a) // e s = 0 for i in range(k): s += S[i*e+e] + S[i*e+a] ans = max(ans, s) d += 1 print(ans)
N = int(eval(input())) *S, = list(map(int, input().split())) ans = 0 for d in range(1, N): r = k = 0 while k*d <= N-1: a = N-1-k*d if a <= d or (a <= k*d and a % d == 0): break r += S[N-1-k*d] + S[k*d] k += 1 ans = max(ans, r) print(ans)
p03034
import sys sys.setrecursionlimit(10**7) INF = 10 ** 18 MOD = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def II(): return int(sys.stdin.readline()) def SI(): return eval(input()) def main(): N = II() S = LI() ans = 0 for C in range(1, N): tmp = 0 L = 0 lim = (N - 1 - C) if (N - 1) % C else (N // 2) while L < lim: tmp += S[L] + S[-1 - L] ans = max(ans, tmp) L += C return ans print((main()))
import sys sys.setrecursionlimit(10**7) INF = 10 ** 18 MOD = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def II(): return int(sys.stdin.readline()) def SI(): return eval(input()) def main(): N = II() S = LI() S = S[:-1] ans = 0 for C in range(1, N): tmp = 0 L = 0 lim = (N - 1 - C) if (N - 1) % C else (N // 2) while L < lim: tmp += S[L] + S[-L] ans = max(ans, tmp) L += C return ans print((main()))
p03034
N = int(eval(input())) s = list(map(int, input().split())) ans = 0 for C in range(1,N): t = 0 for x in range(C, N-1-C if (N-1)%C != 0 else N//2, C): t += s[x] + s[-x-1] ans = max(ans, t) print(ans)
n,*s=list(map(int,open(0).read().split()));a=0 for i in range(1,n): t=0 for j in range(0,[n//2,n+~i][(n-1)%i>0],i):t+=s[j]+s[~j];a=max(a,t) print(a)
p03034
N = int(eval(input())) s = [int(i) for i in input().split()] ret = 0 for i in range(2, N - 1) : for j in range(1, i) : if (i % (i - j) == 0 and 2 * i < N) or (N - 1 - i) % (i - j) != 0 : continue ret = max(ret, sum(s[i::i-j]) + sum(s[i-j:-j-1:i-j])) print((max(ret, 0)))
N = int(eval(input())) s = [int(i) for i in input().split()] ret = 0 for c in range(1, N) : k = 1 tmp = 0 flag = (N - 1) % c == 0 while c * k < N - 1 : l = c * k r = N - 1 - l if (flag and r <= l) or r <= c: break tmp += s[l] + s[r] ret = max(ret, tmp) k += 1 print(ret)
p03034
N = int(eval(input())) slist = list(map(int, input().split())) ans = 0 for C in range(1,N-1,1): flg = [False for i in range(N)] score = 0 for k in range((N-1)//C): if (k-1)*C == N-1-k*C: #一つ前に選んだところ break if 2*k*C == N-1: #今から選ぶ二つが同じところ break score += slist[k*C]+slist[N-1-k*C] ans = max(ans, score) print(ans)
N = int(eval(input())) slist = list(map(int, input().split())) ans = 0 for C in range(1,N-1,1): score = 0 for k in range((N-1)//C): if (k-1)*C == N-1-k*C: #一つ前に選んだところ break if 2*k*C == N-1: #今から選ぶ二つが同じところ break score += slist[k*C]+slist[N-1-k*C] ans = max(ans, score) print(ans)
p03034
n = int(eval(input())) s = list(map(int, input().split())) ans = s[n-1] for c in range(1, n-1): dp = s[n-1] for k in range(n-1): if (k+1) * c >= n-1: break else: dp += s[n - 1 - k * c] + s[k * c] if dp > ans: if k <= 1: ans = dp else: a = n - 1 - k * c b = a - c if a > 2 * b: ans = dp elif a < 2 * b: d = [0] * n t = 0 while 1: t += a if t == n-1: ans = dp break elif t > n-1: break d[t] = 1 t -= b if d[t] == 1: break else: d[t] = 1 print(ans)
n = int(eval(input())) s = list(map(int, input().split())) ans = 0 for d in range(1, n): temp = 0 L = 0 R = n-1 if (n-1) % d == 0: while L < R: temp += s[L] + s[R] ans = max(ans, temp) L += d R -= d else: while L + d < n-1: temp += s[L] + s[R] ans = max(ans, temp) L += d R -= d print(ans)
p03034
N, = list(map(int, input().split())) xs = list(map(int, input().split())) r = 0 for a in range(1, N-1): for b in range(1, N-2): if a <= b: continue if (N-1-a)%(a-b): continue n = (N-1-a)//(a-b) if a%(a-b) == 0: if a <= (a-b)*n: continue s = a t = xs[a] vs = set([a]) for i in range(n): #t = max(0, t) s -= b t += xs[s] vs.add(s) s += a vs.add(s) t += xs[s] #print("k:%d, A:%d, B:%d"%(a-b,a,b)) #print(vs, t) r = max(r, t) print(r)
N, = list(map(int, input().split())) xs = list(map(int, input().split())) r = 0 for k in range(1, N): a = (N-1) % k b = a - k n = (N-1-a)//k if a <= k: a += k if a <= k: a += k for _ in range((N-1-a)//k): b = a - k n = (N-1-a)//k if a <= b: a += k continue if a%(a-b) == 0: if a <= (a-b)*n: a += k continue if a <= b or a >= N-1: continue s1 = a s2 = n * (a-b) t = xs[s1] ps = [(s1,t)] for i in range(n): s1 += a-b t += xs[s2] t = max(t,0) t += xs[s1] ps.append((s2,xs[s2])) ps.append((s1,xs[s1])) s2 -= a-b #print(ps, t) r = max(r, t) print(r)
p03034
N, = list(map(int, input().split())) xs = list(map(int, input().split())) r = 0 for k in range(1, N): a = (N-1) % k b = a - k n = (N-1-a)//k if a <= k: a += k if a <= k: a += k for _ in range((N-1-a)//k): b = a - k n = (N-1-a)//k if a <= b: a += k continue if a%(a-b) == 0: if a <= (a-b)*n: a += k continue if a <= b or a >= N-1: continue s1 = a s2 = n * (a-b) t = xs[s1] ps = [(s1,t)] for i in range(n): s1 += a-b t += xs[s2] t = max(t,0) t += xs[s1] ps.append((s2,xs[s2])) ps.append((s1,xs[s1])) s2 -= a-b #print(ps, t) r = max(r, t) print(r)
N, = list(map(int, input().split())) xs = list(map(int, input().split())) r = 0 for k in range(1, N): a = (N-1) % k for _ in range(1+(N-1-a)//k): b = a - k n = (N-1-a)//k if a <= b or a <= k: a += k continue if a%(a-b) == 0: if a <= (a-b)*n: a += k continue if a >= N-1: continue s1 = a s2 = n * (a-b) t = xs[s1] for i in range(n): s1 += a-b t += xs[s2] t = max(t,0) t += xs[s1] s2 -= a-b r = max(r, t) print(r)
p03034
import bisect n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) a.sort() b.sort() c.sort() bi = bisect.bisect_right result = 0 for i in range(n): a_bi_in_b = bi(b, a[i]) for j in range(a_bi_in_b, n): b_bi_in_c = bi(c, b[j]) result += n - b_bi_in_c print(result)
import bisect n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) a.sort() b.sort() c.sort() result = 0 for i in range(n): b_bisect_in_a = bisect.bisect_left(a, b[i]) b_bisect_in_c = bisect.bisect_right(c, b[i]) result += b_bisect_in_a * (n - b_bisect_in_c) print(result)
p03557
import bisect def main(): N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) # print(A,B,C) A.sort() B.sort() C.sort() def f(b): """ bより大きい数がいくつあるか """ p = bisect.bisect_right(C, b) return N - p cnt = 0 for ai in A: for bi in B: if bi <= ai: continue x = f(bi) cnt += x print(cnt) if __name__ == "__main__": main()
import bisect def main(): N = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) # print(A,B,C) A.sort() B.sort() C.sort() def get_a(b): p = bisect.bisect_left(A, b) return p def get_c(b): """ bより大きい数がいくつあるか """ p = bisect.bisect_right(C, b) return N - p cnt = 0 for bi in B: x = get_a(bi) * get_c(bi) cnt += x print(cnt) if __name__ == "__main__": main()
p03557
inf=10**9+1 n=int(eval(input())) array_a=sorted([a for a in map(int, input().split())])+[inf] array_b=sorted([a for a in map(int, input().split())])+[inf] array_c=sorted([a for a in map(int, input().split())]) nums_b=[0 for _ in range(n)] cur=0 for (idx,b) in enumerate(array_b[:-1]): while(array_a[cur]<b): cur+=1 nums_b[idx]=cur nums_c=[0 for _ in range(n)] cur=0 for (idx,c) in enumerate(array_c): while(array_b[cur]<c): cur+=1 nums_c[idx]=sum(nums_b[:cur]) print((sum(nums_c)))
inf=10**9+1 n=int(eval(input())) array_a=sorted([a for a in map(int, input().split())])+[inf] array_b=sorted([a for a in map(int, input().split())])+[inf] array_c=sorted([a for a in map(int, input().split())]) nums_b=[0 for _ in range(n)] cur=0 for (idx,b) in enumerate(array_b[:-1]): while(array_a[cur]<b): cur+=1 nums_b[idx]=cur cur=0 res=0 for (idx,c) in enumerate(array_c): while(array_b[cur]<c): res+=nums_b[cur]*(n-idx) cur+=1 print(res)
p03557
import bisect n = int(eval(input())) list_A = list(map(int, input().split())) list_B = list(map(int, input().split())) list_C = list(map(int, input().split())) list_A.sort() list_B.sort() list_C.sort() ans = 0 list_U = [] list_D = [] for i in range(n): b = list_B[i] index = bisect.bisect_left(list_A, b) list_U.append(index) for i in range(n): c = list_C[i] index = bisect.bisect_left(list_B, c) list_D.append(index) for i in range(n): j = list_D[i] for k in range(j): ans += list_U[k] print(ans)
import bisect n = int(eval(input())) list_A = list(map(int, input().split())) list_B = list(map(int, input().split())) list_C = list(map(int, input().split())) list_A.sort() list_B.sort() list_C.sort() ans = 0 for i in range(n): b = list_B[i] left = bisect.bisect_left(list_A, b) right = n - bisect.bisect_right(list_C, b) ans += left * right print(ans)
p03557
# coding: utf-8 # Your code here! N=int(eval(input())) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list(map(int,input().split())) A.sort() B.sort() C.sort() A.reverse() B.reverse() C.reverse() ue=0 naka=0 sita=0 count=0 for i in range(N): ue=A[i] for j in range(N): naka=B[j] if ue>=naka: break for k in range(N): sita=C[k] if A[i]<B[j] and B[j]<C[k]: count+=1 if naka>=sita: break print(count)
N=int(eval(input())) S=list(map(int,input().split())) T=list(map(int,input().split())) U=list(map(int,input().split())) S.sort() T.sort() U.sort() #print(S) #print(T) #print(U) count=0 def is_ok(index,key): if key>S[index]: return True else: return False def meguru_bisect(key): ng=N ok=-1 while (abs(ok - ng) > 1): mid = (ok + ng) // 2 if is_ok(mid,key): ok = mid else: ng = mid return ok def is_ok2(index,key): if key<U[index]: return True else: return False def meguru_bisect2(key): ng=-1 ok=N while (abs(ok - ng) > 1): mid = (ok + ng) // 2 if is_ok2(mid,key): ok = mid else: ng = mid return ok ans=0 for i in range(N): ans+=(N-meguru_bisect2(T[i]))*(meguru_bisect(T[i])+1) print(ans)
p03557
def main(): import bisect N = int(eval(input())) upper = list(map(int, input().split())) middle = list(map(int, input().split())) lower = list(map(int, input().split())) upper.sort() middle.sort() lower.sort() cnt = 0 index_mid = 0 index_low = 0 for i in range(N): len_up = upper[i] index_mid = index_mid + bisect.bisect_right(middle[index_mid:], len_up) if (index_mid >= N): break index_low = index_low + bisect.bisect_right(lower[index_low:], middle[index_mid]) tmp_mid = index_mid tmp_low = index_low while (tmp_mid < N): len_mid = middle[tmp_mid] tmp_low = tmp_low + bisect.bisect_right(lower[tmp_low:], len_mid) if (tmp_low < N): cnt += N - tmp_low tmp_mid += 1 else: break print(cnt) if __name__ == '__main__': main()
def main(): import bisect N = int(eval(input())) upper = list(map(int, input().split())) middle = list(map(int, input().split())) lower = list(map(int, input().split())) upper.sort() middle.sort() lower.sort() cnt = 0 for i in range(N): mid = middle[i] index_up = bisect.bisect_left(upper, mid) index_low = bisect.bisect_right(lower, mid) cnt += index_up * (N - index_low) print(cnt) if __name__ == '__main__': main()
p03557
import bisect n = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort() B.sort() C.sort() ct = 0 for i in C: b_max = bisect.bisect_left(B, i) for j in B[: b_max]: ct += bisect.bisect_left(A, j) print(ct)
import bisect n = int(eval(input())) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort() C.sort() ct = 0 for j in B: a_max = bisect.bisect_left(A, j) c_min = bisect.bisect_right(C, j) ct += a_max * (n - c_min) print(ct)
p03557
import sys def fastio(): from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) # from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br n, = I() A = sorted(I()) B = sorted(I()) C = sorted(I()) ans = 0 for i in range(n): left = bl(A, B[i]) right = n - br(C, B[i]) ans += left * right print(ans)
inp=lambda:list(map(int,input().split())) import bisect as bi n=int(eval(input())) a=inp() b=inp() c=inp() a.sort() b.sort() c.sort() ans=0 for i in range(n): x=bi.bisect_left(a,b[i]) y=n-bi.bisect_right(c,b[i]) ans+=x*y print(ans)
p03557
n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) a.sort() b.sort() c.sort() count=0 for i in a: for j in b: for k in c: if i<j<k: count=count+1 print(count)
import bisect n = int(eval(input())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) a.sort() b.sort() c.sort() A = 0 for x in b: i = bisect.bisect_left(a, x) j = bisect.bisect_right(c, x) if i > 0 and j != len(c): A += i * (n - j) print(A)
p03557
#!/usr/bin/env python3 import sys import bisect def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"): answer = 0 A.sort() B.sort() C.sort() for i in range(N): B_index = bisect.bisect_left(B,A[i]+1) for j in range(B_index,N): C_index = bisect.bisect_left(C,B[j]+1) answer += N-C_index print(answer) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int A = [int(next(tokens)) for _ in range(N)] # type: "List[int]" B = [int(next(tokens)) for _ in range(N)] # type: "List[int]" C = [int(next(tokens)) for _ in range(N)] # type: "List[int]" solve(N, A, B, C) if __name__ == '__main__': main()
#!/usr/bin/env python3 import sys import bisect def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"): A.sort() B.sort() C.sort() answer = 0 for i in range(N): A_index = bisect.bisect_left(A,B[i]) C_index = bisect.bisect_left(C,B[i]+1) answer += (N-C_index)*A_index print(answer) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() N = int(next(tokens)) # type: int A = [int(next(tokens)) for _ in range(N)] # type: "List[int]" B = [int(next(tokens)) for _ in range(N)] # type: "List[int]" C = [int(next(tokens)) for _ in range(N)] # type: "List[int]" solve(N, A, B, C) if __name__ == '__main__': main()
p03557
n=int(eval(input())) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list(map(int,input().split())) A.sort() ; B.sort() ; C.sort() def isOK(A,mid,target): #条件を満たす領域とそうでない領域の境目! return target<A[mid] #条件を満たす中で最小 #return A[mid]<=target #条件を満たす中で最大 def aa(A,target): ng=-1 ; ok=n #okはその条件を満たしていることが大前提 while abs(ok-ng)>1: mid=(ok+ng)//2 if isOK(A,mid,target): ok=mid else: ng=mid return ok ans=0 for i in A: t=aa(B,i) for q in range(t,n): ans+=n-aa(C,B[q]) print(ans)
n=int(eval(input())) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list(map(int,input().split())) A.sort() ; B.sort() ; C.sort() import bisect as bi ans=0 for i in B: ans+= bi.bisect_left(A,i) * (n-bi.bisect_right(C,i)) print(ans)
p03557
n = int(eval(input())) a = sorted(list(map(int, input().split()))) b = sorted(list(map(int, input().split()))) c = sorted(list(map(int, input().split()))) x = 0 for i in range(n): for j in range(n): for k in range(n): if a[i] < b[j] < c[k]: x += 1 print(x)
import bisect n = int(eval(input())) a = sorted(list(map(int, input().split()))) b = sorted(list(map(int, input().split()))) c = sorted(list(map(int, input().split()))) ans = 0 for i in b: aa = bisect.bisect_left(a, i) cc = n-bisect.bisect_right(c, i) ans += aa*cc print(ans)
p03557