code1
stringlengths
16
24.5k
code2
stringlengths
16
24.5k
similar
int64
0
1
pair_id
int64
2
181,637B
question_pair_id
float64
3.71M
180,628B
code1_group
int64
1
299
code2_group
int64
1
299
import sys # import re # import math import collections # import decimal # import bisect # import itertools # import fractions # import functools import copy # import heapq # import decimal # import statistics import queue sys.setrecursionlimit(10000001) INF = 10 ** 16 MOD = 10 ** 9 + 7 MOD2 = 998244353 ni = lambda: int(sys.stdin.readline()) ns = lambda: map(int, sys.stdin.readline().split()) na = lambda: list(map(int, sys.stdin.readline().split())) # ===CODE=== def main(): n = ni() d = na() c = collections.Counter(d) lim = max(c) flg = False if d[0] != 0: flg = True if c[0] != 1: flg = True for i in range(lim + 1): if i not in c.keys(): flg = True break if flg: print(0) exit(0) ans = 1 for i in range(2, lim + 1): ans *= pow(c[i - 1], c[i], MOD2) ans %= MOD2 print(ans) if __name__ == '__main__': main()
from collections import Counter n = int(input()) d = list(map(int, input().split())) mod = 998244353 if d[0] != 0 or 0 in d[1:]: print(0) else: c = Counter(d) ans = 1 for i in range(1, len(c)): ans *= c[i-1] ** c[i] ans %= mod print(ans % mod)
1
154,765,034,579,850
null
284
284
x = input() s = 0 for i in range(len(x)): s = s + int(x[i]) if s % 9 == 0: print("Yes") else: print("No")
def main() : N = input() sum = 0 for i in range(len(N)): sum += int(N[i]) if sum % 9 == 0: print("Yes") else: print("No") if __name__ == "__main__": main()
1
4,414,824,217,190
null
87
87
if int(input()) >= 30: print('Yes') else: print('No')
s = int(input()) if s >= 30: print("Yes") else: print("No")
1
5,715,274,336,418
null
95
95
from collections import Counter n=input()[::-1] A=[0] num,point=0,1 for i in n: num +=int(i)*point num %=2019 A.append(num) point *=10 point %=2019 count=Counter(A) ans=0 for i,j in count.items(): if j>=2:ans +=j*(j-1)//2 print(ans)
S=list(input()) P=0 M=2019 D=[0]*M D[0]=1 X,Y=0,1 for i in range(len(S)): X+=Y*int(S[-i-1]) X%=M P+=D[X] D[X]+=1 Y=Y*10%M print(P)
1
30,953,079,598,408
null
166
166
N = int(input()) for i in range(1, 10): if N % i == 0: j = N // i if 1 <= j and j <= 9: print("Yes") exit() print("No")
i = int(input()) print(i*i*i)
0
null
79,737,368,764,452
287
35
x1,y1,x2,y2=map(float,input().split()) a=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) print(a**(1/2))
import math a,b,c,d = map(float,input().split()) print(math.sqrt(((c-a)**2) + ((d-b)**2)))
1
161,523,301,030
null
29
29
x = input() a = int(x) print(a+a**2+a**3)
a = int(input()) N = a + a * a + a * a * a print(N)
1
10,184,043,367,520
null
115
115
X = int(input()) prime = [] for n in range(3, X, 2): lim = int(n ** 0.5) f = True for p in prime: if p > lim: break if n % p == 0: f = False break if f: prime.append(n) if X >= 3: prime = [2] + prime for n in range(X, X+10**5): for p in prime: if n % p == 0: break else: print(n) break
# 素因数分解 def prime_factors(n): i = 2 while i * i <= n: if n % i: i += 1 else: n //= i yield i if n > 1: yield n from itertools import count X = int(input()) for x in count(X): if len(tuple(prime_factors(x))) == 1: print(x) break
1
105,646,136,099,130
null
250
250
import sys INF = 1 << 60 MOD = 10**9 + 7 # 998244353 sys.setrecursionlimit(2147483647) input = lambda:sys.stdin.readline().rstrip() def resolve(): A = [] for _ in range(int(input())): x, l = map(int, input().split()) A.append((x - l, x + l)) A.sort(key = lambda x : x[1]) ans = 0 now = -INF for l, r in A: if now <= l: now = r ans += 1 print(ans) resolve()
import sys input = sys.stdin.readline n = int(input()) robots = [tuple(map(int, input().split())) for _ in range(n)] robots.sort(key=(lambda robo: robo[0] + robo[1])) z = -float('inf') ans = 0 for x, l in robots: if z <= x - l: ans += 1 z = x + l print(ans)
1
90,255,193,817,370
null
237
237
from decimal import Decimal x = int(input()) t = 100 n = 0 while t < x: t += t // 100 n += 1 print(n)
S = input() if S.isupper(): print("A") else: print("a")
0
null
19,113,887,066,600
159
119
s = input() l = [] for i in range(len(s)): l.append(s[i]) if l[len(l)-1] == "s": print(s+str("es")) else: print(s+str("s"))
import heapq from sys import stdin input = stdin.readline #入力 # s = input() n = int(input()) # n,m = map(int, input().split()) # a = list(map(int,input().split())) # a = [int(input()) for i in range()] st=[] for i in range(n): s,t = map(str, input().split()) t = int(t) st.append((s,t)) x = input()[0:-1] def main(): ans = 0 flag = False for i in st: s,t = i if flag: ans+=t if s == x: flag = True print(ans) if __name__ == '__main__': main()
0
null
49,474,776,324,698
71
243
k, n = map(int, input().split()) a_list = list(map(int, input().split())) longest = 0 for i in range(n-1): distance = a_list[i+1] - a_list[i] longest = max(longest, distance) print(k-max(longest, k-a_list[n-1]+a_list[0]))
K, N = [int(x) for x in input().split()] A = [int(x) for x in input().split()] ans = 0 del_A = [] for i in range(N): if(i==(N-1)): del_A.append(A[0]+K-A[i]) else: del_A.append(A[i+1]-A[i]) print(sum(del_A)-max(del_A))
1
43,473,196,488,992
null
186
186
import itertools N = int(input()) cities = [list(map(int, input().split())) for _ in range(N)] patterns = itertools.permutations(cities, N) result = 0 count = 0 for ptn in patterns: count += 1 dis = 0 for i in range(N-1): dis += ((ptn[i][0]-ptn[i+1][0])**2 + (ptn[i][1]-ptn[i+1][1])**2)**0.5 result += dis print(result/count)
import itertools n=int(input()) ab = [] for _ in range(n): a, b = (int(x) for x in input().split()) ab.append([a, b]) memo = [] for i in range(n): memo.append(i) ans = 0 count = 0 for v in itertools.permutations(memo, n): count += 1 tmp_root = 0 for i in range(1, n): tmp_root += ((ab[v[i-1]][0]-ab[v[i]][0])**2+(ab[v[i-1]][1]-ab[v[i]][1])**2)**0.5 ans += tmp_root print(ans/count)
1
148,197,460,417,892
null
280
280
n,k = map(int, input().split()) al = list(map(int, input().split())) for _ in range(min(100,k)): imos = [0]*(n+1) for i,a in enumerate(al): l = max(0,i-a) r = min(n,i+a+1) imos[l] += 1 imos[r] -= 1 new_al = [] curr_val = 0 for im in imos[:-1]: curr_val += im new_al.append(curr_val) al = new_al[:] print(*al)
from itertools import accumulate N, K = map(int, input().split()) A = list(map(int, input().split())) def calc_imos(arr): imos = [0] * (N + 1) for i, a in enumerate(arr): l = max(0, i - a) r = min(N - 1, i + a) imos[l] += 1 imos[r + 1] -= 1 imos = list(accumulate(imos)) return imos[:-1] for k in range(min(50, K)): A = calc_imos(A) print(*A, sep=' ')
1
15,353,733,127,292
null
132
132
n = int(input()) a = list(map(int, input().split())) p = 1000 k = 0 for i in range(n-1): if k>0 and a[i]>a[i+1]: p += k*a[i] k = 0 elif k==0 and a[i]<a[i+1]: k = p//a[i] p %= a[i] if k>0: p += k*a[-1] print(p)
N = int(input()) A = list(map(int, input().split())) m=1000 s=0 if A[0]<A[1]: s=int(1000/A[0]) m=m-s*A[0] for i in range(1,N-1,1): m=m+s*A[i] s=0 if A[i+1]>A[i]: s=int(m/A[i]) m=m-s*A[i] m=m+max(A[N-1],A[N-2])*s print(m)
1
7,273,999,250,888
null
103
103
import sys def input(): return sys.stdin.readline().strip() def resolve(): a=int(input()) print(a+a**2+a**3) resolve()
N = int(input()) print(N + N**2 + N**3)
1
10,180,628,593,188
null
115
115
l=map(int,raw_input().split()) W=l[0] H=l[1] x=l[2] y=l[3] r=l[4] if x>=r and y>=r and x+r<=W and y+r<=H: print 'Yes' else: print 'No'
import sys sys.setrecursionlimit(10**8) def line_to_int(): return int(sys.stdin.readline()) def line_to_each_int(): return map(int, sys.stdin.readline().split()) def line_to_list(): return list(map(int, sys.stdin.readline().split())) def line_to_list_in_iteration(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)] # def dp(init, i, j): return [[init]*i for i2 in range(j)] #from collections import defaultdict #d = defaultdict(int) d[key] += value #from collections import Counter # a = Counter(A).most_common() # from itertools import accumulate #A = [0]+list(accumulate(A)) # import bisect #bisect.bisect_left(B, a), bisect.bisect_right(B,a) a, b = line_to_each_int() print(a*b)
0
null
8,066,851,526,340
41
133
while True: H, W = [int(i) for i in input().split()] if H == W == 0: break for h in range(H): for w in range(W): if (w + h) % 2 == 0 : print('#', end='') else: print('.', end='') print() print()
import sys for i in sys.stdin.readlines()[:-1]: h,w = map(int,i.strip().split()) for i in range(h): for j in range(w): if (i+j) % 2 == 0: print('#',end='') else: print('.',end='') print() print()
1
893,645,287,810
null
51
51
while True: s = raw_input() if '-' == s: break m = input() for a in range(m): h = input() s = s[h:len(s)]+s[:h] print s
while 1: c=list(raw_input()) o="" if c[0]=="-" and len(c)==1: break m=int(raw_input()) for i in range(m): h=int(raw_input()) c=list(c[h:]+c[:h]) for i in range(len(c)): o+=c[i] print o
1
1,907,407,289,532
null
66
66
n = int(input()) ANS = [0] * (n+1) for x in range(1, 101): for y in range(1, 101): for z in range(1, 101): tmp = (x + y + z)**2 - z*(x + y) - (x*y) if tmp > n: continue ANS[tmp] += 1 for ans in ANS[1:]: print(ans)
def main(): height, width, strawberries = map(int, input().split()) cake = [list(input()) for _ in range(height)] answer = [[0 for _ in range(width)] for _ in range(height)] strawberry_num = 0 for i in range(height): is_exist_strawberry = False for j in range(width): if cake[i][j] == "#": is_exist_strawberry = True strawberry_num += 1 answer[i][j] = strawberry_num elif is_exist_strawberry: answer[i][j] = strawberry_num row_with_strawberry = -1 for i in range(height): before_index = 0 for j in range(-1, -width - 1, -1): if answer[i][j] == 0: answer[i][j] = before_index else: before_index = answer[i][j] if row_with_strawberry == -1: row_with_strawberry = i for i in range(row_with_strawberry): answer[i] = answer[row_with_strawberry] for i in range(row_with_strawberry, height): if answer[i][0] == 0: answer[i] = answer[i - 1] for ans in answer: print(" ".join(map(str, ans))) if __name__ == '__main__': main()
0
null
75,877,502,297,220
106
277
from math import ceil H,W=map(int,input().split()) print(ceil(H*W/2) if H!=1 and W!=1 else 1)
H,W = map(int,input().split()) if H == 1 or W == 1: print(1) exit(0) else: result = H * W if result % 2 == 0: print(result // 2) else: print(result // 2 + 1)
1
50,547,461,030,840
null
196
196
#!/usr/bin/env python3 n = int(input()) print(int((n - 1) / 2) if n % 2 == 1 else int(n / 2) - 1)
# Card Game cardAmount = int(input()) taro = 0 hanako = 0 for i in range(cardAmount): cards = input().rstrip().split() sortedCards = sorted(cards) if cards[0] == cards[1]: taro += 1 hanako += 1 elif cards == sortedCards: hanako += 3 else: taro += 3 print(taro, hanako)
0
null
77,777,673,116,540
283
67
N = int(input()) sum = 0 for i in range(1,N+1): if i % 3 == 0 and i % 5 == 0: i = ["FizzBuzz"] elif i % 3 == 0: i = ["Fizz"] elif i % 5 == 0: i = ["Buzz"] # if i % 3 == 0 and i % 5 == 0: else: sum += i print(sum)
n = (int)(input()) ret = 0 for i in range(n+1): if i % 3 != 0 and i % 5 != 0: ret += i print("{}".format(ret))
1
34,754,149,896,040
null
173
173
number = int(input()) list1,list2 = input().split(" ") str="" for i in range(number): str=str+list1[i]+list2[i] print(str)
#(身長)ー(番号)=ー(身長)ー(番号)はありえない、身長=0にならないから N = int(input()) A = list(map(int,input().split())) from collections import defaultdict AB = defaultdict(int) _AB = defaultdict(int) for i,a in enumerate(A): AB[i+1+A[i]]+=1 _AB[-A[i]+i+1]+=1 ans = 0 for key,val in AB.items(): ans += val*_AB[key] print(ans)
0
null
69,053,468,662,948
255
157
##C - Walking Takahash ##正負が反転しないなら単調増加、減少 ##反転するなら、反転した瞬間で残りの回数が偶数なら、その値、奇数なら、一つ前にの値 X,K,D = map(int,input().split()) ##X1の正負フラグがあれば簡潔に書けるかも if X < 0: X = -X R = K - X // D if X-K*D >= 0: print(X-K*D) else: if R % 2 == 0: print(X - D*(X//D)) else: print(abs(X - D*(X//D)-D))
from collections import deque n, m, k = map(int, input().split()) #listを組むより早い friends = [set() for _ in range(n)] blocks = [set() for _ in range(n)] for i in range(m): a, b = map(int, input().split()) friends[a-1].add(b-1) friends[b-1].add(a-1) for i in range(k): c, d = map(int, input().split()) blocks[c-1].add(d-1) blocks[d-1].add(c-1) q = deque() ans = [0] * n visited = [0] * n for i in range(n): if visited[i]: continue #集合を構築していく group = {i} visited[i] = 1 q.append(i) while q: k = q.popleft() for j in friends[k]: if not visited[j]: q.append(j) group.add(j) visited[j] = 1 # できた集合内でとりあえず計算 for l in group: ans[l] = len(group) - len(blocks[l] & group) - len(friends[l] & group) - 1 print(*ans) #print(*ans,sep="\n")
0
null
33,274,697,880,678
92
209
s = raw_input().split() [N] = s N = int(N) print ((N-1)/2)
N, X, Y = map(int, input().split()) d = Y-X+1 l = [0]*(N+1) for i in range(1, N+1): for j in range(i, N+1): m = min(j-i, abs(X-i)+1+abs(Y - j)) l[m] += 1 print(*l[1:-1], sep="\n")
0
null
98,380,150,333,984
283
187
N = int(input()) a = N // 500 N -= 500 * a b = N // 5 print(a * 1000 + b * 5)
# import itertools # import math import sys # sys.setrecursionlimit(500*500) # import numpy as np N = int(input()) # S = input() # n, *a = map(int, open(0)) # X, K, D = map(int, input().split()) # A = list(map(int, input().split())) # C = list(map(int, input().split())) # tree = [[] for _ in range(N + 1)] # B_C = [list(map(int,input().split())) for _ in range(M)] C = input() # B_C = sorted(B_C, reverse=True, key=lambda x:x[1]) # all_cases = list(itertools.permutations(P)) # a = list(itertools.combinations_with_replacement(range(1, M + 1), N)) # itertools.product((0,1), repeat=n) # A = np.array(A) # cum_A = np.cumsum(A) # cum_A = np.insert(cum_A, 0, 0) # def dfs(tree, s): # for l in tree[s]: # if depth[l[0]] == -1: # depth[l[0]] = depth[s] + l[1] # dfs(tree, l[0]) # dfs(tree, 1) a = C.count("R") b = C[:a].count("R") print(a - b)
0
null
24,662,557,379,810
185
98
X = int(input()) num1000 = X // 500 r500 = X % 500 num5 = r500 // 5 print(num1000 * 1000 + num5 * 5)
x=int(input()) hundred=x//500 x-=hundred*500 five=x//5 print(hundred*1000+five*5)
1
42,819,721,283,380
null
185
185
# coding:utf-8 while True: array = map(int, list(raw_input())) if array[0] == 0: break print sum(array)
import sys def input(): return sys.stdin.readline()[:-1] N = int(input()) plus = [] minus = [] for _ in range(N): x, y = map(int, input().split()) plus.append(x+y) minus.append(x-y) print(max(max(plus)-min(plus), max(minus)-min(minus)))
0
null
2,518,338,559,072
62
80
import collections h,w,k = [int(x) for x in input().split()] s = [list(input()) for _ in range(h)] a = [[0]*w for _ in range(h)] cnt= 1 for i in range(h): for j in range(w): if s[i][j]=="#": a[i][j]=cnt cnt+=1 for i in range(h): for j in range(1,w): if a[i][j]==0: a[i][j]=a[i][j-1] for i in range(h): for j in range(w-2,-1,-1): if a[i][j]==0: a[i][j]=a[i][j+1] for j in range(w): for i in range(1,h): if a[i][j]==0: a[i][j]=a[i-1][j] for j in range(w): for i in range(h-2,-1,-1): if a[i][j]==0: a[i][j]=a[i+1][j] for i in a: print(*i)
n = int(input()) a_ls = list(map(int, input().split())) next_number = 1 ans = 0 for i in range(n): if a_ls[i] == next_number: next_number += 1 else: ans += 1 if ans == n: ans = -1 print(ans)
0
null
129,114,536,453,120
277
257
def linear_search(num_list,target): for num in num_list: if num == target: return 1 return 0 n = input() num_list = list(map(int,input().split())) q = input() target_list = list(map(int,input().split())) ans = 0 for target in target_list: ans += linear_search(num_list,target) print(ans)
########関数部分############## def Base_10_to_n(X, n): if (int(X/n)): return Base_10_to_n(int(X/n), n)+str(X%n) return str(X%n) ############################ #####関数をつかってみる.##### ######今回は二進数に変換###### n, k = map(int, input().split()) x10 = n x2 = Base_10_to_n(x10, k) ans = str(x2) print(len(ans))
0
null
32,384,290,492,218
22
212
import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) from collections import Counter, deque from collections import defaultdict from itertools import combinations, permutations, accumulate, groupby, product from bisect import bisect_left,bisect_right from heapq import heapify, heappop, heappush from math import floor, ceil,pi,factorial from operator import itemgetter def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def LI2(): return [int(input()) for i in range(n)] def MXI(): return [[LI()]for i in range(n)] def SI(): return input().rstrip() def printns(x): print('\n'.join(x)) def printni(x): print('\n'.join(list(map(str,x)))) inf = 10**17 mod = 10**9 + 7 x=I() a=x%100 b=x//100 if a>b*5: print("0") else: print("1")
n = int(input()) a = n // 100 b = n % 100 check = a > 20 or b / 5 <= a print("1" if check else "0")
1
126,708,388,533,370
null
266
266
def selectionSort(a, n): count = 0 for i in range(0, n): minj = i for j in range(i, n): if a[j] < a[minj]: minj = j a[i], a[minj] = a[minj], a[i] if i != minj: count += 1 return count def main(): n = int(input()) a = [int(x) for x in input().split(' ')] count = selectionSort(a, n) print(' '.join([str(x) for x in a])) print(count) if __name__ == '__main__': main()
from typing import List def selection_sort(A: List[int]) -> int: cnt = 0 for i in range(len(A)): min_j = i for j in range(i, len(A)): if A[j] < A[min_j]: min_j = j if min_j != i: cnt += 1 A[i], A[min_j] = A[min_j], A[i] return cnt N = int(input()) A = [int(i) for i in input().split()] cnt = selection_sort(A) print(' '.join(map(str, A))) print(cnt)
1
20,302,686,952
null
15
15
k = int(input()) ok = False for i in range(1, k+1): if i == 1: a = 7 % k else: anext = (a * 10 + 7) % k a = anext if a == 0: print(i) ok = True break if not ok: print(-1)
import sys input = sys.stdin.readline #n = int(input()) #l = list(map(int, input().split())) ''' a=[] b=[] for i in range(): A, B = map(int, input().split()) a.append(A) b.append(B)''' k=int(input()) if k%2==0: print("-1") sys.exit() flg=False cnt=0 a=7 for i in range(k): cnt+=1 a%=k if a%k==0: print(cnt) sys.exit() a*=10 a+=7 if not flg: print(-1)
1
6,073,957,877,570
null
97
97
n=int(input()) def m_div(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) divisors.sort() return divisors def check(num,k): if k==1: return False while num>=k: if num%k==0: num=num//k else: break num%=k return num==1 n1divs = m_div(n-1) ndivs = m_div(n) ans=[] for num in n1divs: if check(n,num): ans.append(num) for num in ndivs: if check(n,num): ans.append(num) print(len(set(ans)))
import sys input = sys.stdin.readline from collections import * def make_divs(n): divs = [] i = 1 while i*i<=n: if n%i==0: divs.append(i) if i!=n//i: divs.append(n//i) i += 1 return divs N = int(input()) ds = make_divs(N) ans = 0 for d in ds: if d==1: continue N2 = N while N2%d==0: N2 //= d if (N2-1)%d==0: ans += 1 ans += len(make_divs(N-1))-1 print(ans)
1
41,286,021,803,712
null
183
183
h,w,k=map(int,input().split()) a=["x" for _ in range(h)] c=1 if k==0: a=[1 for _ in range(w)] for i in range(h): print(*a) import sys sys.exit() for i in range(h): s=input() sh=[] nc=0 if s=="."*w:continue for j in range(w): if s[j]=="#" and nc>0:c+=1 elif s[j]=="#": nc+=1 sh.append(str(c)) a[i]=sh[:] c+=1 if a[0]=="x": l=1 while a[l]=="x": l+=1 a[0]=a[l] while "x" in a: for i in range(h): if a[i]=="x": l=i-1 while a[l]=="x": l-=1 a[i]=a[l] for m in a: print(*m)
n, m = map(int, input().split()) h = list(map(int, input().split())) is_good_peak_arr = [True] * n for i in range(m): a, b = map(int, input().split()) a, b = a-1, b-1 if h[a] >= h[b]: is_good_peak_arr[b] = False if h[b] >= h[a]: is_good_peak_arr[a] = False print(is_good_peak_arr.count(True))
0
null
84,603,994,250,710
277
155
n,*a=map(int,open(0).read().split()) dp=[[0]*n for _ in range(3)] for i in range(n): if i%2: dp[1][i]=max(dp[2][i-3]+a[i],dp[1][i-2]+a[i]) elif i>1: dp[0][i]=max(dp[0][i-2]+a[i],dp[1][i-3]+a[i],dp[2][i-4]+a[i]) dp[2][i]=dp[2][i-2]+a[i] else: dp[2][i]=dp[2][i-2]+a[i] print(max(dp[2][-3],dp[1][-2],dp[0][-1]) if n%2 else max(dp[1][-1],dp[2][-2]))
n,k=map(int,input().split()) lr=[list(map(int,input().split())) for _ in range(k)] accum=[0]*k dp=[0]*(n+1) dp[1]=1 mod=998244353 for num in range(2,n+1): for i in range(k): l,r=lr[i] if l<num<=r: accum[i]+=dp[num-l] accum[i]%=mod dp[num]+=accum[i] elif r<num: accum[i]+=dp[num-l]-dp[num-r-1] accum[i]%=mod dp[num]+=accum[i] dp[num]%=mod print(dp[n])
0
null
19,906,271,368,672
177
74
from sys import stdin input = stdin.readline def solve(): n,m = map(int,input().split()) p = [tuple(map(int,inp.split())) for inp in stdin.read().splitlines()] father = [-1] * n count = n def getfather(x): if father[x] < 0: return x father[x] = getfather(father[x]) return father[x] def union(x, y): x = getfather(x) y = getfather(y) nonlocal count if x != y: count -= 1 if father[x] > father[y]: x,y = y,x father[x] += father[y] father[y] = x for a,b in p: union(a-1,b-1) print(count - 1) if __name__ == '__main__': solve()
N=int(input()) A = list(map(int, input().split())) ans = 0 for i in range(0, N, 2): ans += A[i] % 2 != 0 print(ans)
0
null
5,058,288,275,040
70
105
def main(): N, M = map( int, input().split()) S = [ int(s) for s in input()][::-1] ANS = [] t = 0 while t < N: for i in range( min(M, N-t),0,-1): if S[t+i] == 0: ANS.append(i) t += i break else: print(-1) return if sum(ANS) == N: print( " ".join( map( str,ANS[::-1]))) else: print(-1) if __name__ == '__main__': main()
#!/usr/bin python3 # -*- coding: utf-8 -*- n, m = map(int, input().split()) s = input() s = s[::-1] INF = 2*n if '1'*m in s: print(-1) exit() dp = [-1] * (n+1) nw = 0 st = 0 nst = 0 ret = [] while nw < (n+1): if s[nw]=='1': dp[nw] = INF else: dp[nw] = dp[st] + 1 nst = nw nw += 1 if nw == n+1: ret.append(nst-st) elif nw > st + m: ret.append(nst-st) st = nst print(' '.join(map(str,ret[::-1])))
1
139,081,244,175,120
null
274
274
N = input() K = int(input()) L = len(N) dp = [[[0 for j in range(L + 10)] for i in range(L + 10)] for _ in range(2)] dp[0][0][0] = 1 for i in range(L): Ni = int(N[i]) for j in range(L): if Ni == 0: dp[0][i + 1][j + 0] += dp[0][i][j + 0] * 1 dp[0][i + 1][j + 1] += dp[0][i][j + 0] * 0 dp[1][i + 1][j + 0] += dp[0][i][j + 0] * 0 + dp[1][i][j + 0] * 1 dp[1][i + 1][j + 1] += dp[0][i][j + 0] * 0 + dp[1][i][j + 0] * 9 else: dp[0][i + 1][j + 0] += dp[0][i][j + 0] * 0 dp[0][i + 1][j + 1] += dp[0][i][j + 0] * 1 dp[1][i + 1][j + 0] += dp[0][i][j + 0] * 1 + dp[1][i][j + 0] * 1 dp[1][i + 1][j + 1] += dp[0][i][j + 0] * (Ni - 1) + dp[1][i][j + 0] * 9 #print(dp[0]) #print(dp[1]) print(dp[0][L][K] + dp[1][L][K])
from collections import deque k=int(input()) que=deque() for i in range(1,10): que.append(i) for i in range(k): x=que.popleft() if list(str(x))[-1]=="0": que.append(x*10+int(list(str(x))[-1])) que.append(x*10+int(list(str(x))[-1])+1) elif list(str(x))[-1]=="9": que.append(x*10+(int(list(str(x))[-1])-1)) que.append(x*10+int(list(str(x))[-1])) else: que.append(x*10+int(list(str(x))[-1])-1) que.append(x*10+int(list(str(x))[-1])) que.append(x*10+int(list(str(x))[-1])+1) print(x)
0
null
58,029,325,591,120
224
181
x=list(map(int,input())) if x[-1]==2 or x[-1]==4 or x[-1]==5 or x[-1]==7 or x[-1]==9: print("hon") elif x[-1]==0 or x[-1]==1 or x[-1]==6 or x[-1]==8: print("pon") else: print("bon")
kazu = ['pon','pon','hon','bon','hon','hon','pon','hon','pon','hon'] n = int(input()) print(kazu[n%10])
1
19,370,782,158,688
null
142
142
N = input() print('hon') if N[-1] in ["2","4","5","7","9"] else print('bon') if N[-1]=="3" else print('pon')
import sys read = sys.stdin.read readlines = sys.stdin.readlines #import numpy as np from collections import Counter def main(): n, *a = map(int, read().split()) if 1 in a: count1 = a.count(1) if count1 == 1: print(1) sys.exit() else: print(0) sys.exit() maxa = max(a) seq = [0] * (maxa + 1) ac = Counter(a) for ae in ac.items(): if ae[1] == 1: seq[ae[0]] = 1 for ae in a: t = ae * 2 while t <= maxa: seq[t] = 0 t += ae r = sum(seq) print(r) if __name__ == '__main__': main()
0
null
16,989,083,392,830
142
129
n,k=map(int,input().split()) ans=n%k if ans>k//2: print(abs(ans-k)) elif ans<=k//2: print(ans)
import sys input = sys.stdin.readline def solve_K_1(N): S = str(N) d = len(S) return 9 * (d - 1) + int(S[0]) def solve_K_2(N): if N <= 99: return N - solve_K_1(N) S = str(N) d = len(S) res = 0 res += 81 * (d - 1) * (d - 2) // 2 res += 9 * (d - 1) * (int(S[0]) - 1) x = 0 for i in range(1, d): if S[i] != "0": x = int(S[i]) break i += 1 res += x + 9 * (d - i) return res def solve_K_3(N): if N <= 999: return N - solve_K_1(N) - solve_K_2(N) S = str(N) d = len(S) res = 0 for n in range(3, d): res += 729 * (n - 1) * (n - 2) // 2 res += 81 * (d - 1) * (d - 2) // 2 * (int(S[0]) - 1) res += solve_K_2(int(S[1:])) return res def main(): N = int(input()) K = int(input()) if K == 1: ans = solve_K_1(N) elif K == 2: ans = solve_K_2(N) elif K == 3: ans = solve_K_3(N) print(ans) if __name__ == "__main__": main()
0
null
57,851,658,151,770
180
224
n, k = map(int, input().split()) t = n // k re = n - t * k i = True while i == True: if abs(re - k) < re: re = abs(re - k) i = True else: i = False print(re)
#from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] from fractions import gcd from itertools import combinations,permutations,accumulate, product # (string,3) 3回 #from collections import deque from collections import deque,defaultdict,Counter import decimal import re #import bisect # # d = m - k[i] - k[j] # if kk[bisect.bisect_right(kk,d) - 1] == d: # # # # pythonで無理なときは、pypyでやると正解するかも!! # # # my_round_int = lambda x:np.round((x*2 + 1)//2) # 四捨五入g import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 #mod = 9982443453 def readInts(): return list(map(int,input().split())) def I(): return int(input()) n,k = readInts() print(min(n%k,k - (n%k)))
1
39,431,802,279,270
null
180
180
n=int(input()) a=list(map(int,input().split())) if n//2==1: print(max(a)) exit() if n%2==0: #iは桁数、jはそれまでに余計に飛ばした数 #dp[i][j] dp=[[-10**9]*2 for _ in range(n)] for i in range(n): if i<2: dp[i][0]=a[i] if i>=2: dp[i][0]=dp[i-2][0]+a[i] if i>=3: dp[i][1]=max(dp[i-2][1]+a[i],dp[i-3][0]+a[i]) #print(dp) print(max([dp[-1][1],dp[-1][0],dp[n-2][0]])) else: #iは桁数、jはそれまでに余計に飛ばした数 #dp[i][j] dp=[[-10**9]*3 for _ in range(n)] for i in range(n): if i<2: dp[i][0]=a[i] if i>=2: dp[i][0]=dp[i-2][0]+a[i] if i>=3: dp[i][1]=max(dp[i-2][1]+a[i],dp[i-3][0]+a[i]) if i>=4: dp[i][2]=max([dp[i-2][2]+a[i],dp[i-3][1]+a[i],dp[i-4][0]+a[i]]) #print(dp) print(max([dp[-1][2],dp[-1][1],dp[-1][0]-a[0],dp[-1][0]-a[-1]]))
import sys sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) from collections import defaultdict def resolve(): N = ir() A = lr() dp = defaultdict(lambda: -float('inf')) dp[0, 0, 0] = 0 for i in range(N): for j in range(max(i//2-1, 0), i//2+2): dp[i+1, j+1, 1] = dp[i, j, 0]+A[i] dp[i+1, j, 0] = max(dp[i, j, 0], dp[i, j, 1]) # print(dp) print(max(dp[N, N//2, 1], dp[N, N//2, 0])) resolve()
1
37,510,744,948,060
null
177
177
def is_prime(x): for i in range(2, int(x ** 0.5) + 1): if x % i == 0: return False else: return True def main(): x = int(input()) while True: if is_prime(x): ans = x break x += 1 print(ans) if __name__ == "__main__": main()
X = int(input()) prime = [] for n in range(3, X, 2): lim = int(n ** 0.5) f = True for p in prime: if p > lim: break if n % p == 0: f = False break if f: prime.append(n) if X >= 3: prime = [2] + prime for n in range(X, X+10**5): for p in prime: if n % p == 0: break else: print(n) break
1
105,409,709,622,242
null
250
250
import numpy as np def main(): N, K = [int(x) for x in input().split()] A = [float(x) for x in input().split()] F = [float(x) for x in input().split()] A = np.array(sorted(A)) F = np.array(sorted(F, reverse=True)) if K >= np.sum(A)*N: print(0) exit() min_time = 0 max_time = A[-1] * F[0] while max_time != min_time: tgt_time = (min_time + max_time)//2 ideal_a = np.floor(tgt_time*np.ones(N)/F) cost = A - ideal_a require_k = np.sum(cost[cost > 0]) if require_k <= K: max_time = tgt_time else: min_time = tgt_time+1 print(int(max_time)) if __name__ == "__main__": main()
s = input() t = input() s_l = len(s) t_l = len(t) s_l -= (t_l-1) if(t in s): print(0) exit() ma = 0 for si in range(s_l): cnt = 0 for ti in range(t_l): if(s[si+ti]==t[ti]): cnt += 1 ma = max(ma,cnt) print(t_l - ma)
0
null
84,608,985,710,496
290
82
n = int(input()) arr = [int(x) for x in input().split()] ans = 0 for i in range(n): ans += (i+1)%2 and arr[i]%2 print(ans)
word = input() if word.endswith('s'): print(word+"es") else: print(word+"s")
0
null
5,081,102,982,688
105
71
queue,decrease = (int(n) for n in input().split(' ')) targ = [] for q in range(queue): targ.append([n for n in input().split(' ')]) length = 0 time = 0 while True: if int(targ[length][1]) - decrease <= 0: time += int(targ[length][1]) print(str(targ[length][0]) + " " + str(time)) targ.pop(length) queue -= 1 if queue == 0: break length = (length) % queue else: time += decrease targ[length][1] = int(targ[length][1]) - decrease length = (length + 1) % queue
process_num, qms = map(int, input().split()) raw_procs = [input() for i in range(process_num)] if __name__ == '__main__': procs = [] for row in raw_procs: name, time = row.split() procs.append({ "name": name, "time": int(time), }) total_time = 0 current_proc = 0 while len(procs) > 0: if procs[current_proc]["time"] > qms: procs[current_proc]["time"] = procs[current_proc]["time"] - qms total_time += qms if current_proc == len(procs)-1: current_proc = 0 else: current_proc += 1 else: total_time += procs[current_proc]["time"] print("{} {}".format(procs[current_proc]["name"], total_time)) del procs[current_proc] if current_proc == len(procs): current_proc = 0
1
44,595,647,292
null
19
19
N = int(input()) A = list(map(int, input().split())) sa = sum(A) INF = 10**9+7 ans = 0 for i in range(N): sa -= A[i] ans += (A[i]*sa)%INF print(ans%INF)
c = ord(input()) c += 1 n = chr(c) print(n)
0
null
48,115,109,512,834
83
239
l = list(map(int,input().split())) l.sort() if (l[0]==l[1] and l[1] != l[2]) or (l[0] != l[1] and l[1]==l[2]): print('Yes') else: print('No')
a, b, c, = map(int, input().split()) if a == b and a == c: print('No') elif a != b and a != c and b != c: print('No') else: print('Yes')
1
68,059,101,907,342
null
216
216
N,K=map(int,input().split()) A=list(map(int,input().split())) m,p,z=[],[],0 for a in A: if a<0: m.append(a) elif a>0: p.append(a) else: z+=1 m.sort() p.sort() mod=10**9+7 from functools import reduce if (len(p) or K%2==0) and K<len(m)+len(p) or K==len(m)+len(p) and len(m)%2==0: r=1 i,j=0,0 while i+j<K: if i<len(m)-1 and i+j<K-1 and (j>=len(p)-1 or m[i]*m[i+1]>p[-j-1]*p[-j-2]): r=r*m[i]*m[i+1]%mod i+=2 else: r=r*p[-j-1]%mod j+=1 print(r) elif z: print(0) else: print(reduce(lambda x,y:x*y%mod,(m+p)[-K:]))
def e_multiplication_4(MOD=10**9 + 7): from functools import reduce N, K = [int(i) for i in input().split()] A = [int(i) for i in input().split()] sign = {-1: 0, 0: 0, 1: 0} # A の各要素の符号 for a in A: if a == 0: sign[0] += 1 continue sign[a // abs(a)] += 1 if sign[1] == 0 and K % 2 == 1: # どうやっても解は負。絶対値の小さなものから掛けていく A.sort(reverse=True) return reduce(lambda x, y: (x * y) % MOD, A[:K]) if sign[1] == 0 and K % 2 == 0: # 先程と違って絶対値の大きなものから掛けても解は正になる A.sort() return reduce(lambda x, y: (x * y) % MOD, A[:K]) A.sort() ans = 1 i, j = 0, N - 1 while K > 0: # 負の絶対値が大きいものから 2 個取るか? 正の絶対値が大きいものから 1 個取るか? # i, j がそれぞれ負の値、非負の値を指さなくなった場合、if 文のどちらかを # 確実に通らなくなるので、i, j についての条件は必要ない if K > 1 and A[i] * A[i + 1] >= A[j] * A[j - 1]: ans *= A[i] * A[i + 1] i += 2 K -= 2 else: ans *= A[j] j -= 1 K -= 1 ans %= MOD return ans print(e_multiplication_4())
1
9,485,898,197,982
null
112
112
n = input() k = int(input()) digit = len(n) dp0 = [[0 for i in range(k+1)] for j in range(digit+1)] dp1 = [[0 for i in range(k+1)] for j in range(digit+1)] dp1[1][1] = 1 dp0[1][1] = int(n[0])-1 for i in range(1, digit+1): dp0[i][0] = 1 for i in range(2, digit+1): for j in range(1, k+1): num = int(n[i-1]) if num == 0: dp0[i][j] = dp0[i-1][j-1]*9 + dp0[i-1][j] dp1[i][j] = dp1[i-1][j] else: dp0[i][j] = dp0[i-1][j-1]*9 + dp0[i-1][j] + dp1[i-1][j-1] * (num-1) + dp1[i-1][j] dp1[i][j] = dp1[i-1][j-1] print(dp0[digit][k] + dp1[digit][k])
def zeroTrim(s): while s[0] == '0': if len(s) == 1: break s = s[1:] return s N = input() K = int(input()) def calc(N, K): digit = len(N) res = 0 if K == 1: if digit > 1: res += (digit - 1) * 9 res += int(N[0]) elif K == 2: if digit <= 1: return 0 if digit > 2: res += 9 * 9 * (digit - 1) * (digit - 2) // 2 for i in range(int(N[0])-1): res += calc('9'*(digit-1), 1) res += calc(zeroTrim(N[1:]), 1) else: if digit <= 2: return 0 if digit > 3: res += 9 * 9 * 9 * (digit - 1) * (digit - 2) * (digit - 3) // 6 for i in range(int(N[0])-1): res += calc('9'*(digit-1), 2) res += calc(zeroTrim(N[1:]), 2) return res print(calc(N, K))
1
75,798,588,305,120
null
224
224
k = int(input()) x = 7 % k i = 1 set = set() is_there = False while not is_there: set.add(x) if x == 0: print(i) break x = (x*10+7) % k if x in set: is_there = True set.add(x) i += 1 if x == 0: print(i) break else: print('-1')
k = int(input()) rem = 7%k for i in range(10**6+1): if rem == 0: print(i+1) break rem = (rem*10+7)%k else: print(-1)
1
6,151,677,889,148
null
97
97
# coding: utf-8 def main(): N, A, B = map(int, input().split()) ans = (B - A) // 2 if (B - A) % 2 == 0 else min(A - 1, N - B) + 1 + (B - A - 1) // 2 print(ans) if __name__ == "__main__": main()
n, a, b = [int(x) for x in input().split()] if (b - a) % 2 == 0: print((b - a) // 2) else: print(min(a, n - b + 1) + (b - a - 1) // 2)
1
109,264,075,933,562
null
253
253
s,t=map(str,input().split()) a,b=map(int,input().split()) u=input() if u==s: print("{0} {1}".format(a-1,b)) else: print("{0} {1}".format(a,b-1))
l1=input().split() l2=list(map(int,input().split())) l2[l1.index(input())]-=1 print(" ".join(str(x) for x in l2))
1
71,955,824,901,950
null
220
220
def resolve(): import math n = int(input()) for i in range(1, n+1): if math.floor(i * 1.08) == n: print(i) quit() print(":(") resolve()
import math N = int(input()) for i in range(1,N+1): if math.floor(i*1.08) == N: print(i) break else: print(":(")
1
125,890,101,286,532
null
265
265
from collections import Counter import math n = int(input()) pp = [] a0 = 0 b0 = 0 z = 0 for i in range(n): a,b = map(int,input().split()) if a==0 and b==0: z += 1 elif a==0: a0 += 1 elif b==0: b0 += 1 else: gomi = math.gcd(a,b) if a < 0: a *= -1 b *= -1 pp.append((a//gomi,b//gomi)) mod = 10**9+7 a0 %= mod b0 %= mod z %= mod p = Counter(pp) r = 1 s = set() for i,j in p.keys(): if (i,j) in s: continue ans = p[(i,j)] if j < 0: f = (-j,i) else: f = (j,-i) chk = p[f] r *= (pow(2,ans,mod)+pow(2,chk,mod)-1)%mod r %= mod s.add(f) r *= (pow(2,a0,mod)+pow(2,b0,mod)-1)%mod r %= mod print((r+z-1)%mod)
from collections import defaultdict from math import atan2, gcd N = int(input()) MOD = 1000000007 d = defaultdict(lambda: [0, 0]) zero = 0 pos = [] neg = [] for _ in range(N): a, b = map(int, input().split()) if a == 0 and b == 0: zero += 1 continue if a < 0: a = -a b = -b elif a == 0 and b > 0: b = -b if b >= 0: pos.append((a, b)) else: neg.append((a, b)) for a, b in pos: if b != 0: g = gcd(a, b) else: g = a d[(a // g, b // g)][0] += 1 for a, b in neg: a, b = -b, a if b != 0: g = gcd(a, b) else: g = a d[(a // g, b // g)][1] += 1 ans = 1 for p, n in d.values(): ans *= pow(2, p, MOD) + pow(2, n, MOD) - 1 ans %= MOD ans += zero -1 ans %= MOD print(ans)
1
20,867,661,210,616
null
146
146
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_A import sys nums_len = int(sys.stdin.readline()) nums_str = sys.stdin.readline().split(' ') nums = map(int, nums_str) print ' '.join(map(str, nums)) for i in range(1, nums_len): for j in reversed(range(1, i + 1)): if nums[j] < nums[j - 1]: v = nums[j] nums[j] = nums[j - 1] nums[j - 1] = v print ' '.join(map(str, nums))
num = raw_input() num_list = raw_input() num_list = num_list.split() print " ".join(num_list) for i in range(1, len(num_list)): num_list = map(int, num_list) temp = num_list[i] j = i - 1 while (j >= 0 and num_list[j] > temp): num_list[j+1] = num_list[j] j -= 1 num_list[j+1] = temp num_list = map(str, num_list) print " ".join(num_list)
1
5,906,278,030
null
10
10
import bisect as bs import math as m a,b,x = map(int,input().split()) l = 0 r = m.pi/2 for _ in range(10000): s = (l+r)/2 ts = m.tan(s) if a*ts < b: boundary = a*a*b-a*a*a*ts/2 if x > boundary: r = s else: l = s else: boundary = a*b*b/(ts*2) if x > boundary: r = s else: l = s print(l*180/m.pi)
j = [] for i in range(10) : j.append(int(input())) j.sort(reverse = True) for k in range(3) : print(j[k])
0
null
81,989,727,684,480
289
2
import numpy as np from numba import njit N = np.array([int(_) for _ in input()]) def solve(N): p, q = 0, 1 for x in N: p, q = min(p + x, q + 10 - x), min(p + x + 1, q + 9 - x) print(p) solve(N)
#coding: utf-8 #ALDS1_1A import sys n=int(raw_input()) a=map(int,raw_input().split()) for i in xrange(n): print a[i], print for i in xrange(1,n): v=a[i] j=i-1 while j>=0 and a[j]>v: a[j+1]=a[j] j-=1 a[j+1]=v for k in xrange(n): print a[k], print
0
null
35,390,473,251,552
219
10
n,k=map(int,input().split()) A=list(map(int,input().split())) mod=10**9+7 flag=0 A.sort() tmp1=1 tmp2=1 l=0 r=n-1 if k%2==1: tmp1=A[-1]%mod r-=1 if A[-1]<0: flag=1 for i in range(k//2): vl=A[l]*A[l+1] vr=A[r]*A[r-1] if max(vl,vr)<0: flag=1 if vl>vr: l+=2 tmp1*=vl else: r-=2 tmp1*=vr tmp1%=mod from bisect import bisect_right idx=bisect_right(A,0) if idx==n: idx-=1 l=max(0,idx-1) r=idx for i in range(k): vl=A[l] if l>=0 else mod vr=A[r] if r<n else mod if abs(vl)<abs(vr) or r==n: l-=1 tmp2*=vl else: r+=1 tmp2*=vr tmp2%=mod if flag==0: print(tmp1) else: print(tmp2)
# Vicfred # https://atcoder.jp/contests/abc154/tasks/abc154_a # simulation s, t = input().split() a, b = list(map(int, input().split())) u = input() balls = {} balls[s] = a balls[t] = b balls[u] = balls[u] - 1 print(balls[s], balls[t])
0
null
40,636,971,332,188
112
220
a, b, c, d = map(int, input().split()) ans = 10**20 * -1 ans = max(ans, a * c) ans = max(ans, a * d) ans = max(ans, b * c) ans = max(ans, b * d) print(ans)
N = int(input()) results = [input() for _ in range(N)] C = [0] * 4 dic = ['AC', 'WA', 'TLE', 'RE'] for result in results: for i in range(4): if result == dic[i]: C[i] += 1 break for j in range(4): print(dic[j] + ' x ' + str(C[j]))
0
null
5,842,069,768,722
77
109
def main(): N = int(input()) A = [int(i) for i in input().split()] from itertools import accumulate acc = [a for a in accumulate([0] + A)] ans = acc[-1] for i in range(1, N+1): a = acc[i] b = acc[N] - acc[i] v = abs(a-b) ans = min(ans, v) print(ans) if __name__ == '__main__': main()
N = int(input()) A = list(map(int, input().split())) s = sum(A) r = 10**10 n = 0 for i in range(N-1): n += A[i] r = min(abs(s-n*2), r) print(r)
1
142,389,926,653,888
null
276
276
from sys import stdin A, B = [int(x) for x in stdin.readline().rstrip().split()] if A > 2*B: print(A - 2*B) else: print(0)
x=list(map(int, input().split())) if x[1]*2>x[0]: print(0) else: print(x[0]-x[1]*2)
1
167,296,798,887,072
null
291
291
N, M = map(int, input().split()) a = [2*10**5+1] + sorted(map(int, input().split()), reverse=True) def solve(x): count = 0 j = N for i in range(1, N+1): while a[i] + a[j] < x: j -= 1 count += j return count left, right = 0, 2*10**5+1 while left < right - 1: mid = (left + right) // 2 if solve(mid) >= M: left = mid else: right = mid acc = a[:] acc[0] = 0 for i in range(N): acc[i+1] += acc[i] ans = 0 j = N for i in range(1, N+1): while a[i] + a[j] < left+1: j -= 1 ans += a[i]*j + acc[j] M -= j if M: ans += left * M print(ans)
def run(N, M, A): ''' Aiが10^5なので、searchを以下に変えられる サイズ10**5のリストで、その満足度を持つ数の件数を事前計算しておけばO(1)で求められる ''' A = sorted(A, reverse=True) cnt_A = [len(A)] pre_a = 0 cnt_dict_A = {} for a in sorted(A): cnt_dict_A[a] = cnt_dict_A.get(a, 0) + 1 next_cnt = cnt_A[-1] for a in sorted(cnt_dict_A.keys()): cnt_A.extend([next_cnt]*(a-pre_a)) pre_a = a next_cnt = cnt_A[-1]-cnt_dict_A[a] right = A[0] * 2 left = 0 while left <= right: X = (left + right) // 2 # 左手の相手がaで、満足度がX以上となる組合せの数 cnt = 0 for a in A: # cnt += search(A, X - a) if X - a <= A[0]: if X - a >= 0: cnt += cnt_A[X - a] else: cnt += cnt_A[0] if cnt >= M: res = X left = X + 1 else: right = X - 1 X = res # Xが決まったので、累積和で組合せの数分の値を求める sum_A = [0] for a in sorted(A): sum_A.append(sum_A[-1] + a) sum_cnt = 0 ans = 0 for a in A: cnt = search(A, X - a) sum_cnt += cnt if cnt > 0: ans += cnt * a + sum_A[-1] - sum_A[-cnt-1] if sum_cnt > M: ans -= X * (sum_cnt - M) return ans def search(A, X): ''' AのリストからX以上となる数値がいくつか探す Aはソート済み(降順) 二分探索で実装O(logN) leftとrightはチェック未 middleはループ終了後チェック済み ''' left = 0 right = len(A) - 1 res = 0 while left <= right: middle = (left + right) // 2 if A[middle] >= X: res = middle + 1 left = middle + 1 else: right = middle - 1 return res def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) print(run(N, M, A)) if __name__ == '__main__': main()
1
107,690,580,871,160
null
252
252
import math A,B,C,D = list(map(int,input().split())) if (math.ceil(C/B)-math.ceil(A/D))<1: print("Yes") else: print("No")
input() print(*input().split()[::-1],sep=' ')
0
null
15,411,667,477,370
164
53
import numpy as np h, w, k = map(int, input().split()) grid = [] for _ in range(h): grid.append(list(map(str, input().rstrip()))) grid = (np.array(grid)).reshape(h, -1) x, y = np.where(grid == "#") arr = np.zeros((h, w), dtype=int) arr[x, y] = np.arange(1, k+1) np.maximum.accumulate(arr, axis=1, out=arr) for w in range(w - 2, -1, -1): arr[:, w] += arr[:, w+1] * (arr[:, w] == 0) for i in range(1, h): if arr[i, -1] == 0: arr[i] += arr[i - 1] for j in range(h - 1, -1, -1): if arr[j, -1] == 0: arr[j] += arr[j+1] for i in range(h): print(*arr[i])
n, m = list(map(int, input().split())) A = [list(map(int, input().split())) for i in range(n)] bt = [int(input()) for i in range(m)] for i in range(n): print(sum([x * y for (x, y) in zip(A[i], bt)]))
0
null
72,510,548,471,628
277
56
s = input()[::-1] size = len(s) s += "4" ans = 0 bef = 0 for i in range(size): v1 = int(s[i]) v2 = int(s[i+1]) if v1+bef>=6 or (v1+bef>=5 and v2>=5): ans += 10-(v1+bef) bef = 1 else: ans += (v1+bef) bef = 0 ans += bef print(ans)
import sys import math from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN from collections import deque from bisect import bisect_left from itertools import product def I(): 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 LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)] #文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる def LSI(): return list(map(int, list(sys.stdin.readline().rstrip()))) def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)] #文字列として取得 def ST(): return sys.stdin.readline().rstrip() def LST(): return sys.stdin.readline().rstrip().split() def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)] def FILL(i,h): return [i for j in range(h)] def FILL2(i,h,w): return [FILL(i,w) for j in range(h)] def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)] def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)] def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP) #'0.01'や'1E1'などで指定、整数に戻すならintをかます MOD = 1000000007 INF = float("inf") sys.setrecursionlimit(10**6+10) N = LSI() dp = [[INF]*2 for _ in range(len(N)+1)] #dp[i][0]が下からi桁めをぴったり払う最低枚数 dp[0][0] = 0 for i in range(len(N)): d = N[-(i+1)] dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1) dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1) print(min(dp[-1][0], dp[-1][1]+1))
1
70,921,289,504,174
null
219
219
def merge(targ,first,mid,last): left = targ[first:mid] + [10 ** 9 + 1] right = targ[mid:last] + [10 ** 9 + 1] leftcnt = rightcnt = 0 global ans for i in range(first,last): ans += 1 #print(left,right,left[leftcnt],right[rightcnt],targ,ans) if left[leftcnt] <= right[rightcnt]: targ[i] = left[leftcnt] leftcnt += 1 else: targ[i] = right[rightcnt] rightcnt += 1 def mergesort(targ,first,last): if first +1 >= last: pass else: mid = (first + last) // 2 mergesort(targ,first,mid) mergesort(targ,mid,last) merge(targ,first,mid,last) ans = 0 num = int(input()) targ = [int(n) for n in input().split(' ')] mergesort(targ,0,num) print(" ".join([str(n) for n in targ])) print(ans)
def merge(A, left, mid, right): n1 = mid - left n2 = right - mid L = [] R = [] for i in range(n1): L.append(A[left + i]) for i in range(n2): R.append(A[mid + i]) L.append(1000000001) R.append(1000000001) i = 0 j = 0 for k in xrange(left, right): global cnt cnt += 1 if L[i] <= R[j]: A[k] = L[i] i += 1 else: A[k] = R[j] j += 1 def mergeSort(A, left, right): if left + 1 < right: mid = (left + right) / 2 mergeSort(A, left, mid) mergeSort(A, mid, right) merge(A, left, mid, right) return A n = int(raw_input()) S = map(int, raw_input().split()) cnt = 0 A = mergeSort(S, 0, n) for i in xrange(len(A)-1): print(A[i]), print A[len(A) - 1] print cnt
1
112,149,409,090
null
26
26
def n0():return int(input()) def n1():return [int(x) for x in input().split()] def n2(n):return [int(input()) for _ in range(n)] def n3(n):return [[int(x) for x in input().split()] for _ in range(n)] n,m=n1() if n%2==0: if m>=2: e=n//2 s=1 l=e-s c=0 while e>s and c<m: print(s,e) s+=1 e-=1 c+=1 e=n s=n-l+1 while e>s and c<m: print(s,e) s+=1 e-=1 c+=1 else: print(1,2) else: if m>=2: e=n//2+1 s=1 l=e-s c=0 while e>s and c<m : print(s,e) s+=1 e-=1 c+=1 e=n s=n-l+1 while e>s and c<m: print(s,e) s+=1 e-=1 c+=1 else: print(1,2)
def resolve(): N,M = map(int,input().split()) if N % 2 == 1: for i in range(M): print(str(i+1) + " " + str(N-i)) else: for i in range((M-1) // 2 + 1): print(str(i+1) + " " + str(N-i)) for i in range((M-1) // 2 + 1 , M): print(str(i+1) + " " + str(N-i-1)) resolve()
1
28,634,256,776,052
null
162
162
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines sys.setrecursionlimit(10 ** 9) INF = 1 << 60 MOD = 1000000007 def main(): C = readline().strip() print(chr(ord(C) + 1)) return if __name__ == '__main__': main()
print('abcdefghijklmnopqrstuvwxyz'['abcdefghijklmnopqrstuvwxyz'.index(input())+1])
1
91,888,611,277,760
null
239
239
import sys for e in sys.stdin.readlines(): print(len(str(eval('+'.join(e.split())))))
import math import sys for num in sys.stdin: num = num.split() a = int(num[0]) b = int(num[1]) size = int (math.log10(a+b) + 1) print(size)
1
114,840,258
null
3
3
s = list(input()) if len(s) % 2 != 0: print("No") exit() while s: if s[0]=="h" and s[1] == "i": s.pop(0) s.pop(0) else: print("No") exit() print("Yes")
w, h, x, y, r = [int(x) for x in input().split()] if x + r <= w and x - r >= 0 and y + r <= h and y - r >= 0: print('Yes') else: print('No')
0
null
26,740,116,899,382
199
41
from math import floor def main(): a, b = map(int, input().split()) for yen in range(1, 1100): if floor(yen * 0.08) == a and floor(yen * 0.1) == b: print(yen) return print(-1) if __name__ == '__main__': main()
def resolve(): N,K = map(int,input().split()) A = list(map(int,input().split())) F= list(map(int,input().split())) A.sort() F.sort(reverse = True) AF = list(zip(A,F)) if sum(A)<=K: print(0) return l = 0 r = max([i*j for i,j in AF]) center =0 while l < r-1: center = (l+r)//2 score =sum([max(0,a-center//f) for a,f in AF]) if score > K: l = center else: r = center print(r) if __name__ == "__main__": resolve()
0
null
110,320,987,958,770
203
290
A, B, C = map(int, input().split()) temp = B B = A A = temp temp = C C = A A = temp print(str(A) + " " + str(B) + " " + str(C))
A, B, C = input().split() A = int(A) B = int(B) C = int(C) A, B, C = B, A, C B, A, C = C, A, B C, A, B = A, B, C print(A, B, C)
1
38,105,262,347,698
null
178
178
#import sys #import numpy as np import math #from fractions import Fraction import itertools from collections import deque from collections import Counter import heapq #from fractions import gcd #input=sys.stdin.readline import bisect s=input() t=input() n=len(s) ans=0 for i in range(n): if s[i]!=t[i]: ans+=1 print(ans)
import sys import numpy as np def S(): return sys.stdin.readline().rstrip().split(' ') def Ss(): return list(S()) def I(): return _I(Ss()) def Is(): return list(I()) def _I(ss): return map(int, ss) if len(ss) > 1 else int(ss[0]) s = S()[0] t = S()[0] ans = 0 for i, j in zip([c for c in s], [c for c in t]): if i != j: ans += 1 print(ans)
1
10,369,809,195,040
null
116
116
# 視点を変えてやる全探索 000~999までが達成できたらおk n = int(input()) s = list(input()) ans = 0 for i in range(1000): t = list(f"{i:0>3}") j = 0 tmp = "" for c in s: if t[j] == c: j += 1 if j == 3: ans += 1 break print(ans)
s,t=map(str,input().split()) print(t,s,sep="")
0
null
115,822,934,681,400
267
248
a = input() al=[chr(ord('a') + i) for i in range(26)] if a in al: print("a") else: print("A")
print('aA'[input()<'a'])
1
11,279,522,084,800
null
119
119
n,m= map(int,input().split()) A_line =[] row=[] for i in range(n): line = [int(i) for i in input().split()] A_line.append(line) for i in range(m): row += [int(i) for i in input().split()] for i in range(n): c=0 for j in range(m): c += A_line[i][j]*row[j] print(c)
n, m = [int(i) for i in input().split()] vec = [] for i in range(n): vec.append([int(j) for j in input().split()]) c = [] for i in range(m): c.append(int(input())) for i in range(n): sum = 0 for j in range(m): sum += vec[i][j]*c[j] print(sum)
1
1,179,708,491,940
null
56
56
n=int(input());print((n//2)/n if n%2==0 else -(-n//2)/n)
a = input() b = input() ans = '123'.replace(a,'').replace(b,'') print(ans)
0
null
143,709,020,406,172
297
254
n = int(input()) total = 0 out = 1 for _ in range(n): a, b = map(int, input().split()) if a == b: total += 1 else: if total >= 3: out = 0 else: total = 0 if total >= 3: out = 0 if out: print("No") else: print("Yes")
H, W, K = map(int, input().split()) c = [input() for _ in range(H)] r = [[0]*W for _ in range(H)] S = 0 for i in range(2**H): for j in range(2**W): cnt = 0 for k in range(H): for l in range(W): if (i >> k) & 1: if (j >> l) & 1: if c[k][l] == "#": cnt += 1 if cnt == K: S += 1 print(S)
0
null
5,624,290,950,058
72
110
K=int(input());S="" for i in range(K): S=S+"ACL" print(S)
# -*- coding: utf-8 -*- """ Created on Sat Sep 26 21:02:43 2020 @author: liang """ K = int(input()) ans = "" for i in range(K): ans += "ACL" print(ans)
1
2,176,248,425,118
null
69
69
import math n=int(input()) m=100000 k=100000 for i in range(n): m=1.05*k m=m/1000 k=math.ceil(m)*1000 print(k)
import math n = input() money = 100000 for i in range(0, n): money = money*1.05 money /= 1000 money = int(math.ceil(money)*1000) print money
1
1,210,891,988
null
6
6
k = int(input()) print('ACL' * k)
k = int(input()) string = "ACL" s = string * k print(s)
1
2,178,646,301,066
null
69
69
n, a, b = map(int, input().split()) c = n//(a+b) ans = c*a n = n - c*(a+b) if n < a: print(ans + n) else: print(ans + a)
N, A, B = map(int, input().split(' ')) if N - N // (A + B) * (A + B) > A: tail = A else: tail = N - N // (A + B) * (A + B) print(N // (A + B) * A + tail)
1
55,774,177,468,450
null
202
202
import numpy as np MOD = 10**9+7 N = int(input()) A = np.array(list(map(int, input().split()))) A = A % MOD AS = [] s = 0 for a in range(len(A)): s = (s + A[a]) % MOD AS.append(s) AS = np.array(AS) s = 0 for i in range(len(A)-1): s = (A[i] * ((AS[N-1]-AS[i])) % MOD + s) % MOD print(s)
#A x = input() if x.isupper() ==True: print('A') else: print('a')
0
null
7,527,981,734,272
83
119
A, B, C, K = map(int, input().split()) if K<=A: print(1*K) elif A<K<=A+B: print(1*A) else: print(1*A-1*(K-A-B))
n = int(input()) if n-2 == 0: print(0) elif n%2 == 0: if n != 3: print(int(n/2)-1) else: print(1) else: print(int(n/2))
0
null
87,803,806,430,728
148
283
a, b = input().split() print(b, end = "") print(a)
R,G,B=map(int,input().split()) K=int(input()) M=0 while R>=G or G>=B: if R>=G: G*=2 M+=1 if G>=B: B*=2 M+=1 if M<=K: print('Yes') else: print('No')
0
null
55,185,473,896,732
248
101
W = raw_input().lower() T= [] while True: t = raw_input() if t == "END_OF_TEXT": break else: T += t.lower().split() print(T.count(W))
W = raw_input().lower() doc = [] while True: tmp = raw_input() if tmp == 'END_OF_TEXT': break doc.extend(tmp.lower().split()) print doc.count(W)
1
1,836,660,640,320
null
65
65
def nanbanme(n, x): lst = [1 for i in range(n)] ans = 0 for i in range(n): a = x[i] count = 0 for j in range(a - 1): if (lst[j] == 1): count = count + 1 tmp = 1 for k in range(1, n - i): tmp = tmp*k ans = ans + count*tmp lst[a - 1] = 0 return (ans + 1) n = int(input()) lst1 = list(map(int,input().split())) lst2 = list(map(int,input().split())) a = nanbanme(n, lst1) b = nanbanme(n, lst2) print(abs(a - b))
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) n = list(map(int, list(readline().rstrip().decode())[::-1])) a = 0 b = 10000 for x in n: memo = min(a + x, b + x) b = min(a + 11 - x, b + 9 - x) a = memo print(min(a, b))
0
null
85,495,586,213,962
246
219
def fun(d): cnt = 0 for i in d: cnt+= i*d[i] return cnt from collections import defaultdict d = defaultdict(int) n = int(input()) A = list(map(int,input().split())) for i in range(n): d[A[i]]+=1 q = int(input()) cnt = sum(A) for i in range(q): b,c = map(int,input().split()) if b in d: cnt+=(-b*d[b] + c*d[b]) d[c]+=d[b] d[b]=0 #print(cnt,d) print(cnt)
n=int(input()) s=input() r=0 for i in s: if(i=="R"): r+=1 ans=0 for j in range(r): if(s[j]=="W"): ans+=1 print(ans)
0
null
9,215,960,211,040
122
98
temp = input() n = int(input()) for _ in range(n): order,a,b,*value = input().split() a = int(a) b = int(b) if order == "print": print(temp[a:b+1]) elif order == "reverse": rev = temp[a:b+1] temp = temp[:a] + rev[::-1] + temp[b+1:] else:#replace temp = temp[:a] + value[0] + temp[b+1:]
N=int(input()) A=list(map(int,input().split())) L=[[10**10,0]] for i in range(N): L.append([A[i],i]) L.sort(reverse=True) #print(L) DP=[[0 for i in range(N+1)]for j in range(N+1)] #j=右に押し込んだ数 for i in range(1,N+1): for j in range(i+1): if j==0: DP[i][j]=DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1)) #print(DP,DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1))) elif j==i: DP[i][j]=DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j)) #print(DP,DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j))) else: DP[i][j]=max(DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1-j)),DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j))) #print(DP,DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1-j)),DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j))) print(max(DP[-1]))
0
null
17,921,202,399,452
68
171
a = str(input()) if a.isupper() == True: print('A') else: print('a')
#155_B n = int(input()) a = list(map(int, input().split())) judge = True for i in range(n): if a[i]%2 == 0: if a[i]%3 != 0 and a[i]%5 != 0: judge = False break if judge: print('APPROVED') else: print('DENIED')
0
null
40,331,155,414,300
119
217
mod = 10 ** 9 + 7 from math import gcd N = int(input()) from collections import defaultdict X = defaultdict(lambda: [0, 0]) x = 0 y = 0 z = 0 for i in range(N): a, b = map(int, input().split()) g = abs(gcd(a, b)) if a * b > 0: X[(abs(a) // g, abs(b) // g)][0] += 1 elif a * b < 0: X[(abs(b) // g, abs(a) // g)][1] += 1 else: if a: x += 1 elif b: y += 1 else: z += 1 ans = 1 pow2 = [1] for i in range(N): pow2 += [pow2[-1] * 2 % mod] for i in X.values(): ans *= pow2[i[0]] + pow2[i[1]]- 1 ans %= mod ans *= pow2[x] + pow2[y] - 1 ans += z - 1 ans %= mod print(ans)
x=input() X=int(x) y=X+X**2+X**3 print(y)
0
null
15,653,004,286,650
146
115
n, k, c = map(int, input().split()) s = list(input()) count = 0 l, r = [], [] for i in range(n): m = i + c * count if m >= n: break if s[m] == 'o': count += 1 if count > k: break l.append(m) count = 0 for i in range(n - 1, -1, -1): m = i - c * count if m < 0: break if s[m] == 'o': count += 1 if count > k: break r.append(m) l.sort() r.sort() for i in range(k): if l[i] == r[i]: print(l[i] + 1)
n,k,c = map(int, input().split()) s = input() L = [] L.append(-c) workday = 1 for i in range(1, n+1): if i <= L[workday-1] + c: continue if s[i-1] == 'x': continue L.append(i) workday += 1 if workday == k + 1: break R = [0] * (k+2) R[k+1] = n + c + 1 workday = k for i in range(n, 0, -1): if i >= R[workday+1] - c: continue if s[i-1] == 'x': continue R[workday] = i workday -= 1 if workday == 0: break for i in range(1, k+1): if L[i] == R[i]: print(L[i])
1
40,701,794,571,020
null
182
182
input_line = input() input_line_cubic = input_line ** 3 print input_line_cubic
while True: [h,w] = map(int,input().split()) if h==0 and w ==0: break s = ['#'*w+'\n', (('#' + '.'*(w-2) + '#')[0:w]+'\n')*(h-2), '#'*w+'\n' ] t = list(filter(lambda x: x!='', s)) print(''.join(t[0:h]))
0
null
545,249,268,950
35
50
N = int(input()) flag = 'No' cnt = 0 for i in range(N): d1, d2 = map(int, input().split()) if d1 == d2: cnt += 1 if cnt == 3: flag = 'Yes' else: cnt = 0 print(flag)
n=int(input()) cnt=0 ans=0 for i in range(n): d1,d2=map(int,input().split()) if d1==d2: cnt=cnt+1 ans=max(ans,cnt) else: cnt=0 if ans>=3: print("Yes") else: print("No")
1
2,477,088,109,892
null
72
72
N = int(input()) import math o = math.ceil(N / 2) print(o / N)
#!/usr/bin/env python3 def main(): N= int(input()) ans = (N+1)//2/N print(ans) if __name__ == "__main__": main()
1
176,908,464,851,808
null
297
297
def main(): s = (input()) l = ['x' for i in range(len(s))] print(''.join(l)) main()
while True: m, f, r = [int(s) for s in input().split()] if m == f == r == -1: break if m == -1 or f == -1: print('F') continue st = m + f if 80 <= st: print('A') continue if 65 <= st: print('B') continue if 50 <= st: print('C') continue if st < 30: print('F') continue if 50 <= r: print('C') continue print('D')
0
null
37,011,749,603,862
221
57