code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
# -*- coding: utf-8 -*-
from collections import deque
################ DANGER ################
test = ""
#test = \
"""
9 6 1
1 2
2 3
3 4
4 5
5 6
4 7
7 8
8 9
ans 5
"""
"""
5 4 1
1 2
2 3
3 4
3 5
ans 2
"""
"""
5 4 5
1 2
1 3
1 4
1 5
ans 1
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
def input2():
return test.pop()
else:
def input2():
return input()
########################################
n, taka, aoki = map(int, input2().split())
edges = [0] * (n-1)
for i in range(n-1):
edges[i] = tuple(map(int, input2().split()))
adjacencyL = [[] for i in range(n+1)]
for edge in edges:
adjacencyL[edge[0]].append(edge[1])
adjacencyL[edge[1]].append(edge[0])
################ DANGER ################
#print("taka", taka, "aoki", aoki)
#import matplotlib.pyplot as plt
#import networkx as nx
#G = nx.DiGraph()
#G.add_edges_from(edges)
#nx.draw_networkx(G)
#plt.show()
########################################
takaL = [None] * (n+1)
aokiL = [None] * (n+1)
takaL[taka] = 0
aokiL[aoki] = 0
takaQ = deque([taka])
aokiQ = deque([aoki])
for L, Q in ((takaL, takaQ), (aokiL, aokiQ)):
while Q:
popN = Q.popleft()
for a in adjacencyL[popN]:
if L[a] == None:
Q.append(a)
L[a] = L[popN] + 1
#print(L)
print(max(aokiL[i] for i in range(1, n+1) if takaL[i] < aokiL[i]) - 1)
| import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
N,u,v = LI()
Graph = [[] for i in range(N+1)]
for i in range(N-1):
a,b = LI()
Graph[a].append(b)
Graph[b].append(a)
from collections import deque
q1 = deque([u])
distance1 = [-1]*(N+1) # uから各頂点への最短距離
distance1[u] = 0
# bfs
while q1:
n = q1.pop()
for d in Graph[n]:
if distance1[d] != -1:
continue
else:
distance1[d] = distance1[n]+1
q1.appendleft(d)
q2 = deque([v])
distance2 = [-1]*(N+1) # vから各頂点への最短距離
distance2[v] = 0
# bfs
while q2:
n = q2.pop()
for d in Graph[n]:
if distance2[d] != -1:
continue
else:
distance2[d] = distance2[n]+1
q2.appendleft(d)
q3 = deque([u])
distance3 = [-1]*(N+1) # uから各頂点への最短距離、ただし青木君に追いつかれない
distance3[u] = 0
# bfs
while q3:
n = q3.pop()
for d in Graph[n]:
if distance3[d] != -1 or distance1[d] >= distance2[d]:
continue
else:
distance3[d] = distance3[n]+1
q3.appendleft(d)
# 青木君に追いつかれずに行ける頂点まで行き、うろうろする
ans = max(distance2[i]-1 for i in range(1,N+1) if distance3[i] >= 0)
print(ans) | 1 | 117,406,082,230,618 | null | 259 | 259 |
#!/usr/bin/env python3
import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
n = I()
a = LI()
memo = [0] * (10**6 + 1)
f = defaultdict(int)
for i in a:
if f[i] > 1:
continue
f[i] += 1
for j in range(i, 10**6+1, i):
memo[j] += 1
ans = 0
for i in a:
if memo[i] == 1:
ans += 1
print(ans) | from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
maxA = max(A)
dp = {}
for each in A:
dp[each] = True
for i in range(N):
if dp[A[i]]:
tmp = 2 * A[i]
while maxA >= tmp:
if tmp in dp and dp[tmp]:
dp[tmp] = False
tmp += A[i]
counter = 0
t = Counter(A)
for i in range(N):
if dp[A[i]] and t[A[i]] == 1:
counter += 1
print(counter) | 1 | 14,358,055,255,324 | null | 129 | 129 |
a,v=map(int, input().split())
b,w=map(int, input().split())
t=int(input())
if v<=w:
print("NO")
else:
if abs(b-a)/(v-w)<=t:
print("YES")
else:
print("NO")
| A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())
w = abs(A-B)
if w <= (V-W)*T:
print('YES')
else:
print('NO')
| 1 | 15,062,829,676,162 | null | 131 | 131 |
d=int(input())
c=list(map(int,input().split()))
num=[]
for i in range(d):
num.append(list(map(int,input().split())))
ans=0
num2=[0]*26
for i in range(1,d+1):
t=int(input())
num2[t-1]=i
ans+=num[i-1][t-1]
for j in range(26):
ans-=((i-num2[j])*c[j])
print(ans)
| N=int(input())
s=[0]*N
t=[0]*N
for i in range(N):
s[i],t[i] = input().split()
t[i]=int(t[i])
X=input()
j= s.index(X)
if X==s[-1]:
ans=0
else:
ans= sum(t[j+1:])
print(ans) | 0 | null | 53,692,796,137,792 | 114 | 243 |
# -*- coding: utf-8 -*-
N, X, M = map(int, input().split())
mod_check_list = [False for _ in range(M)]
mod_list = [(X ** 2) % M]
counter = 1
mod_sum = (X ** 2) % M
last_mod = 0
for i in range(M):
now_mod = (mod_list[-1] ** 2) % M
if mod_check_list[now_mod]:
last_mod = now_mod
break
mod_check_list[now_mod] = True
mod_list.append(now_mod)
counter += 1
mod_sum += now_mod
loop_start_idx = 0
for i in range(counter):
if last_mod == mod_list[i]:
loop_start_idx = i
break
loop_list = mod_list[loop_start_idx:]
loop_num = counter - loop_start_idx
ans = 0
if (N - 1) <= counter:
ans = X + sum(mod_list[:N - 1])
else:
ans += X + mod_sum
N -= (counter + 1)
ans += sum(loop_list) * (N // loop_num) + sum(loop_list[:N % loop_num])
print(ans) | n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
u, k, *v = list(map(int, input().split()))
for j in v:
A[int(u)-1][int(j)-1] = 1
# A[int(j)-1][int(u)-1] = 1
# for row in A:
# msg = ' '.join(map(str, row))
# # print(msg)
d = [-1] * n
f = [-1] * n
t = 0
# recursive
def dfs(u):
global t
# print('visit:', str(u), str(t))
t += 1
d[u] = t
for i in range(len(A[u])):
if A[u][i] == 1 and d[i] == -1:
dfs(i)
t += 1
f[u] = t
for i in range(n):
if d[i] == -1:
# print('start from:', str(i))
dfs(i)
for i in range(n):
print(str(i+1), str(d[i]), str(f[i]))
| 0 | null | 1,408,842,737,692 | 75 | 8 |
from collections import defaultdict
N,p = map(int,input().split())
S = input()
S = S[::-1]
d = defaultdict(lambda:0)
r = 0
for i in range(N):
r += int(S[i])*pow(10,i,p)
r %= p
d[r] += 1
ans = 0
for r in d:
ans += d[r]*(d[r]-1)//2
ans += d[0]
if p==2 or p==5:
S = S[::-1]
ans = 0
for i in range(N):
if int(S[i])%p==0:
ans += i+1
print(ans) | import sys
input = sys.stdin.readline
from collections import *
N, P = map(int, input().split())
S = input()[:-1]
if P in [2, 5]:
ans = 0
for i in range(N):
if int(S[i])%P==0:
ans += i+1
print(ans)
exit()
cnt = defaultdict(int)
cnt[0] = 1
now = 0
ans = 0
for i in range(N-1, -1, -1):
now = (int(S[i])*pow(10, N-1-i, P)+now)%P
ans += cnt[now]
cnt[now] += 1
print(ans) | 1 | 57,936,776,476,480 | null | 205 | 205 |
n=int(input())
if n%2==0:
print(int((n/2)-1))
else:
print(int((n-1)/2))
| import sys
from time import time
from random import randrange, random
input = lambda: sys.stdin.buffer.readline()
DEBUG_MODE = False
st = time()
def get_time():
return time() - st
L = 26
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
ans = [randrange(L) for _ in range(D)]
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.data = [0] * (self.n+1)
def sum(self, i):
res = 0
while i > 0:
res += self.data[i]
i -= i & -i
return res
def add(self, i, x):
if i <= 0: raise IndexError
while i <= self.n:
self.data[i] += x
i += i & -i
def lower_bound(self, x):
if x <= 0: return 0
cur, s, k = 0, 0, 1 << (self.n.bit_length()-1)
while k:
nxt = cur + k
if nxt <= self.n and s + self.data[nxt] < x:
s += self.data[nxt]
cur = nxt
k >>= 1
return cur + 1
def rsum(x):
return x*(x+1)//2
b = [BinaryIndexedTree(D) for _ in range(L)]
score = 0
for d, cont in enumerate(ans):
b[cont].add(d+1, 1)
score += S[d][cont]
for i in range(L):
m = b[i].sum(D)
tmp = 0
s = [0]
for j in range(1, m+1):
s.append(b[i].lower_bound(j))
s.append(D+1)
for j in range(len(s)-1):
x = s[j+1] - s[j] - 1
tmp += rsum(x)
score -= tmp*C[i]
def chg(d, p, q):
diff = 0
d += 1
o = b[p].sum(d)
d1 = b[p].lower_bound(o-1)
d2 = b[p].lower_bound(o+1)
diff += rsum(d-d1) * C[p]
diff += rsum(d2-d) * C[p]
diff -= rsum(d2-d1) * C[p]
o = b[q].sum(d)
d1 = b[q].lower_bound(o)
d2 = b[q].lower_bound(o+1)
diff += rsum(d2-d1) * C[q]
diff -= rsum(d-d1) * C[q]
diff -= rsum(d2-d) * C[q]
d -= 1
diff -= S[d][p]
diff += S[d][q]
ans[d] = q
b[p].add(d+1, -1)
b[q].add(d+1, 1)
return diff
while True:
if random() >= 0.8:
d, q = randrange(D), randrange(L)
p = ans[d]
diff = chg(d, p, q)
if diff > 0:
score += diff
else:
chg(d, q, p)
else:
d1 = randrange(D-1)
d2 = randrange(d1+1, min(d1+3, D))
p, q = ans[d1], ans[d2]
diff = chg(d1, p, q)
diff += chg(d2, q, p)
if diff > 0:
score += diff
else:
chg(d1, q, p)
chg(d2, p, q)
if DEBUG_MODE: print(score)
if get_time() >= 1.7: break
if DEBUG_MODE: print(score)
else:
for x in ans: print(x+1)
| 0 | null | 81,906,760,042,048 | 283 | 113 |
N, K = map(int, input().split())
heights = list(map(int, input().split()))
result = 0
for h in heights:
if h >= K:
result += 1
print(result) | N, K = map(int, input().split())
H = tuple(map(int, input().split()))
assert len(H) == N
print(len([h for h in H if h >= K])) | 1 | 178,809,778,705,470 | null | 298 | 298 |
n,m = map(int,raw_input().split())
A = [[0 for i in range(m)] for i in range(n)]
B = [ 0 for i in range(m)]
for i in range(n):
A[i] = map(int,raw_input().split())
for i in range(m):
B[i] = input()
for i in range(n):
sum = 0
for j in range(m):
sum += A[i][j] * B[j]
print sum | n, m = map(int, input().split())
A = [[int(e) for e in input().split()] for i in range(n)]
b = []
for i in range(m):
e = int(input())
b.append(e)
for i in range(n):
p = 0
for j in range(m):
p += A[i][j] * b[j]
print(p) | 1 | 1,164,352,303,130 | null | 56 | 56 |
a, b, c = raw_input().split()
def ITP1(a, b, c):
if (a <= b):
if (b <= c):
print a, b, c
return
if (c <= b):
if (c <= a):
print c, a, b
return
if (a <= c):
print a, c, b
return
if (b <= a):
if (a <= c):
print b, a, c
return
if (c <= a):
if (c <= b):
print c, b, a
return
if (b <= c):
print b, c, a
return
ITP1(a, b, c) | input()
numbers = input().split()
numbers.reverse()
print(" ".join(numbers)) | 0 | null | 696,576,649,792 | 40 | 53 |
from collections import deque
# 両端を扱うならdequeが速い
# 内側を扱うならリストが速い
S = input()
q = deque()
for s in list(S):
q.append(s)
Q = int(input())
direction = True
for _ in range(Q):
line = input()
# TrueならFalseに、FalseならTrueに変える
if line == "1":
direction = not direction
else:
if (direction and line[2] == '1') or (not direction and line[2] == '2'):
# 左端に追加する場合
q.appendleft(line[4])
else:
# 右端に追加する場合
q.append(line[4])
if direction:
print("".join(q))
elif not direction:
q.reverse()
print("".join(q))
| from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = -1
for i in range(q):
line = list(input().split())
t = int(line[0])
if t == 1:
isR *= -1
else:
f = int(line[1])
if isR + f == 0 or isR + f == 3:
s.appendleft(line[2])
else:
s.append(line[2])
if isR == 1:
s.reverse()
print("".join(s)) | 1 | 57,078,179,534,142 | null | 204 | 204 |
#coding: UTF-8
N=int(input())
p=[int(input()) for i in range(0,N)]
maxv=p[1]-p[0]
buy=p[0]
for i in range(1,N):
if p[i]-buy>maxv:
maxv=p[i]-buy
if p[i]<buy:
buy=p[i]
print(maxv) | N=int(input())
for x in range(1,N+1):
if int(1.08*x)==N:
print(x)
break
if x==N:
print(":(")
| 0 | null | 62,810,136,431,838 | 13 | 265 |
A=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
n=int(input())
s=str(input())
t=""
for i in range(len(s)):
for j in range(26):
if s[i]==A[j]:
if j+n<=25:
t+=A[j+n]
break
else:
t+=A[j+n-26]
break
print(t) | N = int(input())
S = list(input())
for i in range(len(S)):
S[i] = ord(S[i])
for i in range(len(S)):
if S[i] + N <= 90:
S[i] = chr(S[i] + N)
else:
S[i] = chr(64 + S[i] + N - 90)
S = ''.join(S)
print(S) | 1 | 134,521,651,718,688 | null | 271 | 271 |
def binary_search(border, b):
ok = b
ng = n
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if L[mid] < border:
ok = mid
else:
ng = mid
return ok
n = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for a in range(0,n-1):
for b in range(a+1, n):
a_b = L[a] + L[b]
ans += binary_search(a_b, b) - b
print(ans) | from bisect import bisect_left
N=int(input())
y=list(map(int, input().split()))
y.sort()
ans=0
for i in range(N-2):
for j in range(i+1, N-1):
tmp=y[i]+y[j]
ans+=bisect_left(y,tmp)-j-1
print(ans)
| 1 | 171,345,667,570,976 | null | 294 | 294 |
mark = ["S","H","C","D"]
numbers = ["1","2","3","4","5","6","7","8","9","10","11","12","13"]
cardlist = {}
for m in mark:
for n in numbers:
key = m + " " + n
cardlist[key] = 0
n = int(input())
for _ in range(n):
card = input()
cardlist[card] = 1
for card in cardlist:
if cardlist[card] == 0:
print(card)
| z = input()
d = {}
c = 0
L = ['S','H','C','D']
for i in L:
for j in range(1,14):
d[i] = d.get(i,[]) + [j]
while c < z:
x = raw_input().split()
d[x[0]].remove(int(x[1]))
c += 1
for i in L:
for j in sorted(d[i]):
print i + " " + str(j) | 1 | 1,030,545,794,122 | null | 54 | 54 |
h,w=map(int,input().split())
s=[input() for _ in range(h)]
dp=[[10**9]*w for _ in range(h)]
dp[0][0]=(s[0][0]=='#')+0
for i in range(h):
for j in range(w):
a=dp[i-1][j]
b=dp[i][j-1]
if s[i-1][j] == "." and s[i][j] == "#":
a+=1
if s[i][j-1] == "." and s[i][j] == "#":
b+=1
dp[i][j]=min(a,b,dp[i][j])
print(dp[-1][-1])
| import itertools
N = int(input())
H = []
for n in range(N):
A = int(input())
tmp = []
for a in range(A):
tmp.append(list(map(int, input().split())))
H.append(tmp)
ans = 0
for pro in itertools.product([0, 1], repeat=N):
flag = 0
for p in range(len(pro)):
if pro[p] != 0:
for h in H[p]:
if pro[h[0]-1] != h[1]:
flag = 1
break
if flag == 1:
break
if p == len(pro) - 1:
ans = max(ans, sum(pro))
print(ans)
| 0 | null | 85,015,075,052,130 | 194 | 262 |
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
N, K = map(int, input().split())
A = list(map(int, input().split()))
ng = 0
ok = 10 ** 9
# able(X) を、すべての丸太を X 以下の長さに切れる場合 1 , そうでない場合 0 を返す関数とする
# 二分探索によって、able(X) が 1 に切り替わる X 点を見つける
def able(x):
count = 0
for i in range(N):
if A[i] % x:
count += A[i] // x
else:
count += A[i] // x - 1
if count <= K:
return True
else:
return False
while ok - ng > 1:
mid = ng + (ok - ng) // 2
if able(mid):
ok = mid
else:
ng = mid
print(ok)
| def check(x, A, K):
import math
sumA = 0
for a in A:
if a > x:
sumA += math.ceil(a / x) - 1
if sumA <= K:
return True
else:
return False
def resolve():
_, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ok = max(A) # maxVal when minimize
ng = -1 # maxVal when maximize
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if mid > 0 and check(mid, A, K):
ok = mid
else:
ng = mid
print(ok)
resolve()
| 1 | 6,506,828,274,830 | null | 99 | 99 |
class Queue:
queue_list = []
def enqueue(self, a):
self.queue_list.append(a)
def dequeue(self):
return self.queue_list.pop(0)
def isEmpty(self):
return len(self.queue_list) == 0
n, q = list(map(lambda x: int(x), input().strip().split()))
queue = Queue()
for i in range(n):
queue.enqueue(input().strip().split())
sum_time = 0
while not queue.isEmpty():
name, time = queue.dequeue()
if int(time) <= q:
sum_time += int(time)
print(name + ' ' + str(sum_time))
else:
queue.enqueue([name, str(int(time) - q)])
sum_time += q
| import sys
import collections
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=collections.deque(e.split()for e in s[1:])
t=0
while d:
k,v=d.popleft()
v=int(v)
if v>q:
v-=q
t+=q
d.append([k,v])
else:
t+=v
print(k,t)
| 1 | 44,002,174,372 | null | 19 | 19 |
def main():
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
res = 0
for i in T:
if i in S:
res += 1
print(res)
if __name__ == '__main__':
main()
| h,n=map(int,input().split())
A=list(map(int,input().split()))
attack=0
for i in range(n):
attack+=A[i]
if attack>=h:
print("Yes")
else:
print("No") | 0 | null | 39,278,584,333,718 | 22 | 226 |
N = int(input())
A = [int(num) for num in input().split()]
S = {-A[0] : 1}
ans = 0
for i in range(1,N):
ans += S.get(A[i]-i, 0)
S[-A[i]-i] = S.get(-A[i]-i, 0) + 1
print(ans) |
N = int(input())
A = list(map(int,input().split()))
d = {}
ans = 0
for i in range(N):
sa = i-A[i]
if sa in d:
ans += d[sa]
wa = i+A[i]
if wa in d:
d[wa] += 1
else:
d[wa] = 1
print(ans)
| 1 | 26,132,060,942,748 | null | 157 | 157 |
w = raw_input()
words = []
while True:
line = raw_input()
if line == "END_OF_TEXT":
break
else:
line_list = line.split(" ")
words += line_list
#print words
cnt = 0
for word in words:
if w.lower() == word.lower():
cnt += 1
print cnt | N=int(input())
music=[]
T=[]
ans=0
for i in range(N):
s, t=input().split()
t=int(t)
music.append(s)
T.append(t)
X=input()
num=music.index(X)
for j in range(num+1, N):
ans+=T[j]
print(ans) | 0 | null | 49,643,392,598,800 | 65 | 243 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, *A = map(int, read().split())
U = 10 ** 18
p = 1
for x in A:
p *= x
if p > U:
p = U + 1
if p > U:
print(-1)
else:
print(p)
| n,m,q = map(int,input().split())
l = []
def dfs(a):
if len(a)==n: l.append(a)
else:
for i in range(a[-1],m+1):
dfs(a+[i])
for i in range(m):
dfs([i+1])
Query = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for li in l:
tmp = 0
for a,b,c,d in Query:
if li[b-1]-li[a-1]==c:
tmp += d
ans = max(ans,tmp)
print(ans) | 0 | null | 21,917,302,641,792 | 134 | 160 |
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) | n = int(input())
s = input()
t = ''
for c in s:
if ord(c) + n > ord('Z'):
m = ord('Z') - ord(c)
t += chr(ord('A') + (n - m - 1))
else:
t += chr(ord(c) + n)
print(t)
| 0 | null | 75,015,246,282,702 | 133 | 271 |
a,b=map(int,input().split())
if a<0 or a>9:
print(-1)
elif b<0 or b>9:
print(-1)
else:
print(a*b) | h, w, k = map(int, input().split())
s = [list(map(int, input())) for _ in range(h)]
ans = float("inf")
for sep in range(1 << (h-1)):
g_id = [0] * h
cur = 0
for i in range(h-1):
g_id[i] = cur
if sep >> i & 1: cur += 1
g_id[h-1] = cur
g_num = cur+1
ng = False
for i in range(w):
tmp = [0] * g_num
for j in range(h): tmp[g_id[j]] += s[j][i]
if any([x > k for x in tmp]): ng = True; break
if ng: continue
res = g_num-1
gs = [0] * g_num
for i in range(h):
gs[g_id[i]] += s[i][0]
for i in range(1, w):
tmp = gs[::]
for j in range(h):
tmp[g_id[j]] += s[j][i]
if any([x > k for x in tmp]):
res += 1
gs = [0] * g_num
for j in range(h): gs[g_id[j]] += s[j][i]
ans = min(ans, res)
print(ans)
| 0 | null | 103,371,510,648,662 | 286 | 193 |
# coding: utf-8
# Here your code !
def func():
try:
line = input()
line = input().rstrip()
numbers = line.split(" ")
except:
print("input error")
return -1
numbers.reverse()
result=""
for item in numbers:
result += item+" "
print(result.rstrip(" "))
func() | import copy
class Dice:
def __init__(self, eyes: [int]):
self.__eyes = eyes
def top(self):
return self.__eyes[0]
def roll_s(self):
tmp = copy.copy(self.__eyes)
tmp[0] = self.__eyes[4]
tmp[1] = self.__eyes[0]
tmp[2] = self.__eyes[2]
tmp[3] = self.__eyes[3]
tmp[4] = self.__eyes[5]
tmp[5] = self.__eyes[1]
self.__eyes = tmp
def roll_e(self):
tmp = copy.copy(self.__eyes)
tmp[0] = self.__eyes[3]
tmp[1] = self.__eyes[1]
tmp[2] = self.__eyes[0]
tmp[3] = self.__eyes[5]
tmp[4] = self.__eyes[4]
tmp[5] = self.__eyes[2]
self.__eyes = tmp
def roll_n(self):
tmp = copy.copy(self.__eyes)
tmp[0] = self.__eyes[1]
tmp[1] = self.__eyes[5]
tmp[2] = self.__eyes[2]
tmp[3] = self.__eyes[3]
tmp[4] = self.__eyes[0]
tmp[5] = self.__eyes[4]
self.__eyes = tmp
def roll_w(self):
tmp = copy.copy(self.__eyes)
tmp[0] = self.__eyes[2]
tmp[1] = self.__eyes[1]
tmp[2] = self.__eyes[5]
tmp[3] = self.__eyes[0]
tmp[4] = self.__eyes[4]
tmp[5] = self.__eyes[3]
self.__eyes = tmp
def roll(self, direction: str):
if direction == 'N':
self.roll_n()
if direction == 'E':
self.roll_e()
if direction == 'S':
self.roll_s()
if direction == 'W':
self.roll_w()
def resolve():
eyes = list(map(int, input().split()))
directions = list(input())
dice = Dice(eyes)
for direction in directions:
dice.roll(direction)
print(dice.top())
resolve()
| 0 | null | 607,694,133,244 | 53 | 33 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def print_array(a):
print(" ".join(map(str, a)))
def selectionsort(arr, n):
a = arr[:]
s = 0
for i in range(n):
minj = i
for j in range(i, n):
if a[j] < a[minj]:
minj = j
if i != minj:
(a[i], a[minj]) = (a[minj], a[i])
s += 1
return (a, s)
def main():
n = int(input())
arr = list(map(int, input().split()))
(a, s) = selectionsort(arr, n)
print_array(a)
print(s)
if __name__ == "__main__":
main() | nums = input().split()
a = int(nums[0])
b = int(nums[1])
if a < b:
print('a < b')
elif a > b:
print('a > b')
else:
print('a == b') | 0 | null | 193,355,117,952 | 15 | 38 |
while 1:
try:
line = raw_input()
except EOFError:
break
arr = map((lambda x: int(x)), line.split())
print len(str(arr[0]+arr[1])) | def main(S):
rains = S.split('S')
return max([len(r) for r in rains])
if __name__ == '__main__':
S = input()
ans = main(S)
print(ans)
| 0 | null | 2,428,871,552,042 | 3 | 90 |
s,w = list(map(int,input().split()))
if w >= s:
print('unsafe')
else:
print('safe') | def main():
import sys
input = sys.stdin.readline
N = int(input())
plus = []
minus = []
total = 0
for _ in range(N):
s = input().strip()
h = 0
bottom = 0
for ss in s:
if ss == "(":
h += 1
else:
h -= 1
bottom = min(bottom, h)
total += h
if h > 0:
plus.append((bottom, h))
else:
minus.append((bottom-h, -h))
if total != 0:
print("No")
sys.exit()
plus.sort(reverse=True)
minus.sort(reverse=True)
height = 0
for b, h in plus:
if height + b < 0:
print("No")
sys.exit()
height += h
height = 0
for b, h in minus:
if height + b < 0:
print("No")
sys.exit()
height += h
print("Yes")
if __name__ == '__main__':
main() | 0 | null | 26,556,194,772,380 | 163 | 152 |
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
x,k,d = map(int, input().split())
x = abs(x)
if x == 0:
if k % 2 == 0:
print(0)
exit()
else:
print(abs(d))
exit()
bai = x//d
if bai >= k:
print(x-k*d)
exit()
else:
x -= bai*d
if (k-bai)%2==0:
print(x)
exit()
else:
print(abs(x-d))
| a, b, c = map(int, input().split())
d = abs(a)
count = min(d//c, b)
d -= c*count
b -= count
if b % 2 == 1:
d -= c
print(abs(d)) | 1 | 5,218,535,265,060 | null | 92 | 92 |
from itertools import permutations
N = int(input())
P = ''.join(input().split())
Q = ''.join(input().split())
l = sorted(permutations(range(1, N + 1), N))
d = {''.join(map(str, k)): v+1 for v, k in enumerate(l)}
print(abs(d[P]-d[Q]))
| N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fact = [1]
for i in range(0,N):
fact.append(fact[i] * (i+2))
p = 0
q = 0
def number(a):
res = 0
global fact
l = len(a)
for i in range(l-2):
res += (a[i] - 1) * fact[l-2-i]
for j in range(i+1, l-2):
if a[j] > a[i]:
a[j] -= 1
if a[l-1] < a[l-2]:
res += 2
else:
res += 1
return res
p, q = number(P), number(Q)
print(abs(q-p)) | 1 | 100,752,929,417,470 | null | 246 | 246 |
n = int(input())
S = list(input())
ans = 0
for i in range(len(S)-2):
if S[i] == "A":
if S[i+1] == "B":
if S[i+2] == "C":
ans += 1
print(ans) | #-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import heapq
def main():
A=[]
B=[]
M=[]
values=[]
a,b,m = map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
M=[list(map(int,input().split())) for _ in range(m)]
# #割引した組み合わせ
for m in M:
values.append(A[m[0]-1]+B[m[1]-1]-m[2])
heapq.heapify(A)
heapq.heapify(B)
#最も安い組み合わせ
cheap=heapq.heappop(A)+heapq.heappop(B)
print(min(cheap,min(values)))
if __name__=="__main__":
main() | 0 | null | 76,382,345,063,068 | 245 | 200 |
from decimal import Decimal
a, b, c = [Decimal(x) for x in input().split()]
result = a.sqrt() + b.sqrt() < c.sqrt()
print('Yes' if result else 'No') | # -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
def solve():
a, b, c = [int(x) for x in input().split()]
d = c - a - b
if d > 0 and d * d > 4 * a * b:
print("Yes")
else:
print("No")
t = 1
# t = int(input())
for case in range(1,t+1):
ans = solve()
"""
7
3 1
4 1
5 9
2 6
5 3
5 8
9 7 5
"""
| 1 | 51,926,526,507,460 | null | 197 | 197 |
nList = list(map(int, input().split()))
print(nList.index(0) + 1)
| s=input()
s_list=list(s)
s_list.append('0')
for i in range(len(s)):
if s_list[i]=='?':
if s_list[i+1]=='D':
if s_list[i-1]=='P':
s_list[i]='D'
else:
s_list[i]='P'
elif s_list[i+1]=='?':
if s_list[i-1]=='P':
s_list[i]='D'
else :
s_list[i]='P'
else :
s_list[i]='D'
s_list.pop()
a="".join(s_list)
print(a) | 0 | null | 15,960,294,815,908 | 126 | 140 |
h = int(input())
w = int(input())
n = int(input())
ans = n // max(h,w) if n % max(h,w) == 0 else n // max(h,w) + 1
print(ans) | H = int(input())
W = int(input())
N = int(input())
print(int(N/max(H,W)) + (1 if N % max(H,W) != 0 else 0)) | 1 | 88,607,307,874,492 | null | 236 | 236 |
a, b = map(int, input().split())
print("unsafe" if (a <= b) else "safe") | #coding:utf-8
data = input()
xmax = len(data)
data_list = list(data)
data_list.reverse()
reverse_data = []
for sig in data_list:
if sig == "\\":
reverse_data.append("/")
elif sig == "/":
reverse_data.append("\\")
else:
reverse_data.append(sig)
reverse_data = "".join(reverse_data)
def partialSqu(h, sig):
if sig == "\\":
squ = h + 1/2
h += 1
elif sig == "/":
squ = h - 1/2
h -= 1
elif sig == "_":
squ = h
return squ, h
x_squ_dict = {}
cnt = 0
sw, x, h, totalSqu = 0, 0, 0, 0
for sig in data:
x += 1
if sw == 0 and sig == "\\":
sw = 1
if sw == 1 :
squ, h = partialSqu(h, sig)
totalSqu += squ
if h == 0:
x_squ_dict[x] = totalSqu
totalSqu = 0
sw = 0
keys = x_squ_dict.keys()
sw, x, h, totalSqu = 0, 0, 0, 0
for sig in reverse_data:
x += 1
if sw == 0 and sig == "\\" :
sw = 1
x_p = xmax - x +1
if sw == 1 :
squ, h = partialSqu(h, sig)
totalSqu += squ
if h == 0:
x_squ_dict[x_p] = totalSqu
totalSqu = 0
sw = 0
keys = x_squ_dict.keys()
keys = list(keys)
keys.sort()
squ_list = []
for key in keys:
squ_list.append(x_squ_dict[key])
a = int(sum(squ_list))
print(a)
squ_list.insert(0,len(keys))
squ_list = " ".join([str(int(num)) for num in squ_list])
print(squ_list)
| 0 | null | 14,645,436,962,438 | 163 | 21 |
# AOJ ITP1_9_A
# ※大文字と小文字を区別しないので注意!!!!
# 全部大文字にしちゃえ。
def main():
W = input().upper() # 大文字にする
line = ""
count = 0
while True:
line = input().split(" ")
if line[0] == "END_OF_TEXT": break
for i in range(len(line)):
word = line[i].upper()
if W == word: count += 1
print(count)
if __name__ == "__main__":
main()
# Accepted.
| x,n=map(int,input().split())
p=set(list(map(int,input().split())))
if x not in p :
print(x)
exit()
i=1
while True:
if x-i not in p:
print(x-i)
exit()
if x+i not in p:
print(x+i)
exit()
i+=1
| 0 | null | 7,983,622,246,140 | 65 | 128 |
S = input()
T = input()
max_count = 0
for i in range(max(1, len(S) - len(T))):
count = 0
for j in range(len(T)):
if S[i+j] == T[j]:
count += 1
max_count = max(max_count, count)
print(len(T) - max_count)
| #a,b,c = map(int,input().split())
a = int(input())
b = input()
c = b[:a//2]
d = b[a//2:a]
print("Yes" if c == d else "No") | 0 | null | 74,992,047,567,752 | 82 | 279 |
xyz = input().split()
print(xyz[2]+ " " + xyz[0] + " " + xyz[1]) | import numpy as np
H = int(input())
W = int(input())
N = int(input())
print(int(np.ceil(N/max(H,W)))) | 0 | null | 63,460,567,802,490 | 178 | 236 |
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N,K = LI()
p = LI()
p.sort()
p1 = np.array(p)
print(p1[:K].sum())
| n = int(input())
mod = 10**9+7
def pow(n, num):
s = num
for i in range(1,n):
s = s*num%mod
return s
print((pow(n,10) - 2*pow(n,9) + pow(n,8))%mod)
| 0 | null | 7,289,721,520,832 | 120 | 78 |
s = input()
ans = chk = 0
ans = [-1] * (len(s)+1)
if s[0] == "<":
ans[0] = 0
if s[-1] == ">":
ans[-1] = 0
for i in range(len(s) - 1):
if s[i] == ">" and s[i+1] == "<":
ans[i+1] = 0
for i in range(len(s)):
if s[i] == "<":
ans[i+1] = ans[i] + 1
for i in range(-1, -len(s)-1,-1):
if s[i] == ">":
ans[i-1] = max(ans[i-1], ans[i]+1)
print(sum(ans))
| S = input()
N=len(S)+1
SS = '>'+S+'<'
a = [-1]*(N+2)
for i in range(N+1):
if SS[i] == '>' and SS[i+1]=='<':
a[i+1]=0
b = a[1:-1]
c = a[1:-1]
for i in range(N-1):
if S[i] == '<' and b[i]>=0 :
b[i+1] = b[i] +1
for i in range(N-2,-1,-1):
if S[i] == '>' and c[i+1]>=0:
c[i] = c[i+1] +1
ans = 0
for bb,cc in zip(b,c):
ans += max(bb,cc)
print(ans)
| 1 | 156,789,241,575,758 | null | 285 | 285 |
k = int(input())
a, b = map(int, input().split())
range_max = int(b / k) * k
if range_max >= a:
print('OK')
else:
print('NG')
| k=int(input())
a,b=map(int,input().split())
print("OK" if (a-1)//k!=b//k else"NG") | 1 | 26,508,274,075,008 | null | 158 | 158 |
from itertools import chain
from bisect import bisect_left, bisect_right
H, W, M = map(int, input().split())
YX = [list(map(int, input().split())) for _ in range(M)]
# H, W, M = 3*10**5, 3*10**5, 3*10**5
# YX = [[(i*100)//W, (i*100)%W] for i in range(M)]
X_sets = [[] for _ in range(W+1)]
Y_sets = [[] for _ in range(H+1)]
for y, x in YX:
X_sets[x].append(y)
Y_sets[y].append(x)
[s.sort() for s in X_sets]
[s.sort() for s in Y_sets]
# print(X_sets, Y_sets)
X_set_max_len = max(map(len, X_sets))
Y_set_max_len = max(map(len, Y_sets))
# print("ppp", X_set_max_len, Y_set_max_len)
# for s in X_sets:
# print(s)
X_list = set(i for i, s in enumerate(X_sets) if len(s) == X_set_max_len)
Y_list = set(i for i, s in enumerate(Y_sets) if len(s) == Y_set_max_len)
score = X_set_max_len + Y_set_max_len - 1
# print("b", score, X_list, Y_list)
for x in X_list:
for y in Y_list:
c = bisect_right(X_sets[x], y) - bisect_left(X_sets[x], y)
# print(x, y, c)
if c == 0:
score += 1
print(score)
exit()
print(score) | N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort(reverse=True)
if A[M-1] >= sum(A)/(4*M) :
print("Yes")
else:
print("No") | 0 | null | 21,687,135,578,888 | 89 | 179 |
a, b = input().split()
ab = a*int(b)
ba = b*int(a)
if ab <= ba:
print(ab)
else:
print(ba) | N = input()
K = int(input())
dp = [[[0 for _ in range(5)] for _ in range(2)] for _ in range(len(N)+1)]
dp[0][0][0] = 1
for i in range(len(N)):
dgt = int(N[i])
for k in range(K+1):
dp[i+1][1][k+1] += dp[i][1][k] * 9
dp[i+1][1][k] += dp[i][1][k]
if dgt > 0:
dp[i+1][1][k+1] += dp[i][0][k] * (dgt-1)
dp[i+1][1][k] += dp[i][0][k]
dp[i+1][0][k+1] = dp[i][0][k]
else:
dp[i+1][0][k] = dp[i][0][k]
print(dp[len(N)][0][K] + dp[len(N)][1][K])
| 0 | null | 79,901,402,730,940 | 232 | 224 |
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
divisible = [False for _ in range(int(1e6+1))]
cnt = defaultdict(int)
for a in A:
cnt[a] += 1
if divisible[a] == False and cnt[a] <= 1:
for i in range(a+a, len(divisible), a):
divisible[i] = True
ans = 0
for a in A:
if divisible[a] is False and cnt[a] <= 1:
ans += 1
print(ans)
| def main():
r, c = map(int, input().split())
table = []
for _ in range(r):
table.append(list(map(int, input().split())))
table.append([0] * (c+1))
for index, line in enumerate(table[:-1]):
table[index].append(sum(line))
for index1, value in enumerate(table[index]):
table[-1][index1] += value
for line in [map(str, line) for line in table]:
print(' '.join(line))
return
if __name__ == '__main__':
main()
| 0 | null | 7,967,708,736,654 | 129 | 59 |
def getval():
return int(input())
def main(x):
for i in range(-200,201):
flag = False
for j in range(-200,201):
if i**5-j**5==x:
print("{a} {b}".format(a=str(i),b=str(j)))
flag = True
break
if flag:
break
if __name__=="__main__":
main(getval()) | x=int(input())
for i in range(-200,200):
for j in range(-200,200):
if i**5-j**5==x:
print("{} {}".format(i,j))
exit() | 1 | 25,440,754,925,948 | null | 156 | 156 |
k,x=map(int,input().split())
print('YNeos'[x>500*k::2]) | H = int(input())
count = 0
ans = 0
while H>1:
H = H//2
ans += 2**count
count += 1
ans += 2**count
print(ans) | 0 | null | 88,788,177,128,872 | 244 | 228 |
t = input('')
tlist = list(t)
for i in range(len(tlist)):
if tlist[i] == '?':
if i==0:
tlist[i] = 'D'
elif i==len(tlist)-1:
tlist[i] = 'D'
else:
if tlist[i-1] == 'P':
if tlist[i+1] == 'P':
tlist[i] = 'D'
elif tlist[i+1] == 'D':
tlist[i] = 'D'
elif tlist[i+1] == '?':
tlist[i] = 'D'
else:
# 1つ前が'D'
if tlist[i+1] == 'P':
tlist[i] = 'D'
elif tlist[i+1] == 'D':
tlist[i] = 'P'
elif tlist[i+1] == '?':
tlist[i] = 'P'
print("".join(tlist)) | s=input()
s=s.replace('?','D')
print(s)
| 1 | 18,551,200,081,934 | null | 140 | 140 |
n = int(input())
if 30 <= n:
print("Yes")
else:
print("No") | n, m ,l = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
b = [list(map(int, input().split())) for j in range(m)]
c = [[sum(ak*bk for ak, bk in zip(ai, bj)) for bj in zip(*b)] for ai in a]
for ci in c:
print(*ci) | 0 | null | 3,573,386,601,280 | 95 | 60 |
n = int(input())
a = [[0, 0] for i in range(n)]
for i in range(n):
x, l = list(map(int, input().split()))
a[i] = [x + l, max(x - l, 0)]
a.sort()
ans = 0
p = 0
for i in a:
if p <= i[1]:
ans += 1
p = i[0]
print(ans) | num = input()
lst = []
for x in num:
lst.append(int(x))
total = sum(lst)
if total % 9 == 0:
print('Yes')
else:
print('No') | 0 | null | 47,312,863,367,900 | 237 | 87 |
s=input()
n=len(s)
K=int(input())
dp=[[[0] * 5 for _ in range(2)] for _ in range(n+1)]
dp[0][0][0]=1
for i in range(n):
ni= int(s[i])
for k in range(4):
# smaller 1 -> 全部OK
dp[i + 1][1][k + 1] += dp[i][1][k] * 9;
dp[i + 1][1][k] += dp[i][1][k];
# smaller 0,next smaller 1 -> nowより小さいのを全部
# 次の桁からsmallerであるためにはN<ni であることが必要で
# Nは0より大きい必要がある。(0より小さいものがない=確定しない
if (ni > 0):
dp[i + 1][1][k + 1] += dp[i][0][k] * (ni - 1)
dp[i + 1][1][k] += dp[i][0][k]
# smaller 0,next smaller 0 -> nowだけ
if (ni > 0) :
dp[i + 1][0][k + 1] = dp[i][0][k]
else:
dp[i + 1][0][k] = dp[i][0][k]
ans=dp[n][0][K] + dp[n][1][K]
print(ans) | import sys
# import numba as nb
import numpy as np
input = sys.stdin.readline
# @nb.njit("i8(i8[:],i8,i8)", cache=True)
def solve(N, K, L):
dp = np.zeros(shape=(L + 1, 2, K + 1), dtype=np.int64)
dp[0][0][0] = 1
for i in range(L):
D = N[i]
for j in range(2):
d_max = 9 if j == 1 else D
for k in range(K + 1):
if k < K:
for d in range(d_max + 1):
dp[i + 1][int(j or (d < D))][k + int(d > 0)] += dp[i][j][k]
else:
dp[i + 1][j][k] += dp[i][j][k]
return dp[L][0][K] + dp[L][1][K]
def main():
N = np.array(list(input().rstrip()), dtype=np.int64)
K = int(input())
L = N.shape[0]
ans = solve(N, K, L)
print(ans)
if __name__ == "__main__":
main()
| 1 | 76,324,262,496,990 | null | 224 | 224 |
class Flood:
"""
begin : int
水たまりの始まる場所
area : int
水たまりの面積
"""
def __init__(self, begin, area):
self.begin = begin
self.area = area
down_index_stack = [] # \の場所
flood_stack = [] # (水たまりの始まる場所, 面積)
for i, c in enumerate(input()):
if c == '\\':
down_index_stack.append(i)
if c == '/':
if len(down_index_stack) >= 1: # 現在の/に対応する\が存在したら
l = down_index_stack.pop() # 現在の水たまりの始まる場所
area = i - l # 現在の水たまりの現在の高さの部分の面積はi-l
# 現在の水たまりが最後の水たまりを内包している間は
# (現在の水たまりの始まる場所が最後の水たまりの始まる場所より前にある間は)
while len(flood_stack) >= 1 and l < flood_stack[-1].begin:
# 最後の水たまりを現在の水たまりに取り込む
last_flood = flood_stack.pop()
area += last_flood.area
flood_stack.append(Flood(l, area)) # 現在の水たまりを最後の水たまりにして終了
area_list = [flood.area for flood in flood_stack]
print(sum(area_list))
print(' '.join(list(map(str, [len(area_list)] + area_list))))
| # -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')
N=INT()
nodes=[[] for i in range(N)]
for i in range(N):
l=LIST()
u=l[0]
l=l[2:]
for v in l:
nodes[u-1].append(v-1)
nodes[u-1].sort()
que=deque()
que.append((0, 0))
costs=[-1]*N
while len(que):
cost,node=que.popleft()
if costs[node]==-1:
costs[node]=cost
for v in nodes[node]:
que.append((cost+1, v))
for i in range(N):
print(i+1, costs[i])
| 0 | null | 32,615,474,270 | 21 | 9 |
Q =input()
if Q.count('R')==3:
print(3)
elif Q.count('R')==0:
print(0)
elif Q.count('R')==1:
print(1)
elif Q.count('R')==2:
if Q[1] =='S':
print(1)
else :
print(2) | stack = input().split()
temp = []
num=0
for loop in stack:
a = loop
if a is "+":
num = temp.pop() + temp.pop()
temp.append(num)
elif a is "-":
b=temp.pop()
c=temp.pop()
num = c-b
temp.append(num)
elif a is "*":
num = temp.pop() * temp.pop()
temp.append(num)
else:
temp .append(int(a))
print(temp[0]) | 0 | null | 2,454,059,085,380 | 90 | 18 |
s = input()
t = input()
def f(k):
c = 0
for i in range(len(t)):
if s[k + i] == t[i]:
c += 1
return c
maxi = f(0)
for i in range(len(s) - len(t) + 1):
if f(i) > maxi:
maxi = f(i)
print(len(t) - maxi) | N=input()
A=map(int,raw_input().split())
for i in range(N-1):
for k in range(N-1):
print A[k],
print A[N-1]
v=A[i+1]
j=i
while j>=0 and A[j] >v:
A[j+1]=A[j]
j=j-1
A[j+1]=v
for m in range(N-1):
print str(A[m]),
print A[N-1] | 0 | null | 1,843,825,444,120 | 82 | 10 |
import itertools
def main():
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = [i for i in range(1, N+1)]
p = list(itertools.permutations(l, N))
print(abs(p.index(P) - p.index(Q)))
if __name__ == '__main__':
main() | n, k = map(int, input().split())
m = 0
while True:
m += 1
if k**m > n:
print(m)
break | 0 | null | 82,548,287,350,220 | 246 | 212 |
def az11():
n, xs = int(input()), map(int,raw_input().split())
print min(xs), max(xs), sum(xs)
az11() | input()
x=map(int,raw_input().split())
print min(x),max(x),sum(x) | 1 | 715,552,990,492 | null | 48 | 48 |
S = input()
Q = int(input())
T = ""
for q in range(Q):
que = input().split()
if que[0]=="1":
S,T = T,S
else:
if que[1]=="1":
T+=que[2]
else:
S+=que[2]
print(T[::-1]+S) | S = input()
Q = int(input())
reverse = False
before = []
after = []
for i in range(Q):
q = input().split()
if len(q) == 1:
reverse = not reverse
else:
T, F, C = q
if (F == '1' and not reverse) or (F == '2' and reverse):
before.append(C)
else:
after.append(C)
if not reverse:
answer = ''.join(before[::-1]) + S + ''.join(after)
else:
answer = ''.join(after[::-1]) + S[::-1] + ''.join(before)
print(answer) | 1 | 57,078,060,352,092 | null | 204 | 204 |
import sys
def solve():
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().split()]
if N == 0 and A[0] > 1:
print(-1)
return 0
low, high = A[N], A[N]
maxN = [0] * (N + 1)
maxN[N] = A[N]
Limit = [2 ** 50] * (N + 1)
Limit[N] = A[N]
for i in reversed(range(N)):
leaf = A[i]
Limit[i] = min(Limit[i], Limit[i+1] + leaf)
if i <= 50: Limit[i] = min(pow(2, i), Limit[i])
low = (low + 1) // 2 + leaf
high += leaf
if low > Limit[i]:
print(-1)
break
else:
maxN[i] = min(high, Limit[i])
else:
for i in range(N):
if (maxN[i] - A[i]) * 2 <= maxN[i+1]: maxN[i + 1] = (maxN[i] - A[i]) * 2
print(sum(maxN))
return 0
if __name__ == "__main__":
solve()
| N=int(input())
A_list=sorted(list(map(int, input().split())), reverse=True)
ans=1
for a in A_list:
ans*=a
if ans>1e+18:
ans=-1
break
if 0 in A_list:
ans=0
print(ans)
| 0 | null | 17,670,108,476,360 | 141 | 134 |
a, b, c = map(int, input().split())
if a / c <= b:
print("Yes")
else:
print("No")
| def main():
N, M = map(int, input().split()) # N must be an odd number
left = 1
count = 0
buff = 0
for d in range(N-1, 0, -2):
if (N-1) & 1 and d <= -d % N:
buff = 1
right = left + d - buff
print(left, right)
left += 1
count += 1
if count == M:
return
if __name__ == "__main__":
main()
| 0 | null | 16,144,921,697,620 | 81 | 162 |
from collections import defaultdict
class UnionFind:
def __init__(self, n):
class KeyDict(dict):
# 辞書にないときの対応
def __missing__(self,key):
self[key] = key
return key
self.parent = KeyDict()
self.rank = defaultdict(int)
self.weight = defaultdict(int)
# 根を探す
def find(self, x):
if self.parent[x] == x:
return x
else:
# 経路圧縮
# 自分自身じゃない場合は、上にさかのぼって検索(再帰的に)
y = self.find(self.parent[x])
self.weight[x] += self.weight[self.parent[x]] #圧縮時にweightを更新(和)
self.parent[x] = y #親の置き換え(圧縮)
return self.parent[x]
# 結合
def union(self, x, y):
x = self.find(x)
y = self.find(y)
# 低い方を高い方につなげる(親のランクによる)
if self.rank[x] < self.rank[y]:
self.parent[x] = y
else:
self.parent[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
### 重み付き
def weighted_union(self, x, y, w):
# print("unite",x,y,w,self.weight)
px = self.find(x)
py = self.find(y)
# 低い方を高い方につなげる(親のランクによる)
# if px == py: return 0
if self.rank[px] < self.rank[py]:
self.parent[px] = py
self.weight[px] = - w - self.weight[x] + self.weight[y]
else:
self.parent[py] = px
self.weight[py] = w + self.weight[x] - self.weight[y]
if self.rank[px] == self.rank[py]:
self.rank[px] += 1
return 0
# 判定
def judge(self, x, y):
return self.find(x) == self.find(y)
n, m = map(int, input().split())
uf = UnionFind(n)
for i in range(m):
a,b = map(int, input().split())
a -= 1
b -= 1
if not uf.judge(a,b):
uf.union(a,b)
d = defaultdict(int)
for i in range(n):
d[uf.find(i)] += 1
print(len(d)-1) | from collections import deque
n = int(input())
li = deque()
for i in range(n):
command = input().split()
if command[0] == 'insert':
li.appendleft(command[1])
elif command[0] == 'delete':
if command[1] in li:
li.remove(command[1])
elif command[0] == 'deleteFirst':
li.popleft()
else:
li.pop()
print(*li) | 0 | null | 1,170,532,618,990 | 70 | 20 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
N = input()
s = 0
for i in range(len(N)):
s += int(N[i])
if s%9 == 0:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
| 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,395,575,983,330 | null | 87 | 87 |
N, X, M = map(int, input().split())
visited = [False] * M
tmp = X
total_sum = X
visited[tmp] = True
total_len = 1
while True:
tmp = (tmp ** 2) % M
if visited[tmp]:
loop_start_num = tmp
break
total_sum += tmp
visited[tmp] = True
total_len += 1
path_len = 0
tmp = X
path_sum = 0
while True:
if tmp == loop_start_num:
break
path_len += 1
path_sum += tmp
tmp = (tmp ** 2) % M
loop_len = total_len - path_len
loop_sum = total_sum - path_sum
result = 0
if N <= path_len:
tmp = X
for i in range(N):
result += X
tmp = (tmp ** 2) % M
print(result)
else:
result = path_sum
N -= path_len
loops_count = N // loop_len
loop_rem = N % loop_len
result += loop_sum * loops_count
tmp = loop_start_num
for i in range(loop_rem):
result += tmp
tmp = (tmp ** 2) % M
print(result)
# print("loop_start_num", loop_start_num)
# print("path_sum", path_sum)
# print("loop_sum", loop_sum)
| d=input()
if d.count("A")==3 or d.count("B")==3: print("No")
else: print("Yes")
| 0 | null | 28,843,756,817,500 | 75 | 201 |
L = int(input())
print(L*L*L/27)
| n = int(input())
for i in range(1,n+1):
if i % 3 == 0:
print(" {0}".format(i), end="")
else:
x = i
while x != 0:
if x % 10 == 3:
print(" {0}".format(i), end="")
break;
x //= 10;
print("")
| 0 | null | 24,053,302,858,790 | 191 | 52 |
X, K, D = map(int, input().split())
X = abs(X)
if K - (X // D) < 0:
X -= K * D
print(X)
else:
K = K - (X // D)
X -= D * (X // D)
if K % 2 == 0:
print(abs(X))
else:
print(abs(X-D)) | n,m = [int(i) for i in input().split()]
a = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
a[i] = [int(j) for j in input().split()]
b = [0 for i in range(m)]
for i in range(m):
b[i] = int(input())
for i in range(n):
sum = 0
for j in range(m):
sum += a[i][j] * b[j]
print(sum) | 0 | null | 3,225,796,379,920 | 92 | 56 |
a,b,c = map(int, input().split())
k = int(input())
for i in range(k):
if a >= b:
b *= 2
continue
if b >= c:
c *= 2
continue
print("Yes" if a < b and b < c else "No")
| n = int(input())
al = []
bl = []
for i in range(n):
a,b = [int(i) for i in input().split()]
al.append(a)
bl.append(b)
al = sorted(al)
bl = sorted(bl)
median_a = al[n//2]if n % 2 else (al[n // 2] + al[n//2 - 1]) / 2
median_b = bl[n//2]if n % 2 else (bl[n // 2] + bl[n//2 - 1]) / 2
if n % 2:
print(int((median_b - median_a + 1)))
else:
print(int((median_b - median_a) * 2 + 1)) | 0 | null | 12,060,673,870,880 | 101 | 137 |
m,k=input().split(" ")
result=int(m)*int(k)
print("%d"%result) | data = [[[0 for r in range(10)] for f in range(3)] for b in range(4)]
count = int(input())
for c in range(count):
(b, f, r, v) = [int(x) for x in input().split()]
data[b - 1][f - 1][r - 1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('',data[b][f][r], end='')
print()
print('#' * 20) if b < 4 - 1 else print(end='') | 0 | null | 8,477,196,970,400 | 133 | 55 |
n = int(input()) #
ns = list(map(int, input().split()))
ns.reverse()
print(*ns)
| import itertools
N = int(input())
d = list(map(int, input().split()))
lst = list(itertools.combinations(d, 2))
total = 0
for n in lst:
c = n[0] * n[1]
total += c
print(total) | 0 | null | 84,969,171,760,902 | 53 | 292 |
h,n=map(int,input().split())
l=list(map(int,input().split()))
a=0
for i in range(n):
a+=l[i]
if a>=h:
print("Yes")
else:
print("No") | n = int(input())
A = list(map(int, input().split()))
m = A[0]
ans = 0
for a in A[1:]:
m = max(m,a)
if m > a:
ans += m-a
print(ans) | 0 | null | 41,220,934,750,424 | 226 | 88 |
N=int(input())
mod=10**9+7
a = (10**N)%mod
b = (9**N)%mod
c = (8**N)%mod
ans = (a+c-2*b)%mod
print(ans) | import sys
input = sys.stdin.readline
N = int(input())
S = list(input().rstrip())
ord_a = ord("a")
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
bits = [Bit(N) for _ in range(26)]
for i in range(N):
s = S[i]
bits[ord(s) - ord_a].add(i + 1, 1)
Q = int(input())
for _ in range(Q):
query = input().rstrip().split()
if query[0] == "1":
i, c = int(query[1]), query[2]
bits[ord(S[i - 1]) - ord_a].add(i, -1)
S[i - 1] = c
bits[ord(S[i - 1]) - ord_a].add(i, 1)
else:
l, r = int(query[1]), int(query[2])
res = 0
for bit in bits:
if l != 1:
res += int(bit.sum(r) - bit.sum(l - 1) > 0)
else:
res += int(bit.sum(r) > 0)
print(res) | 0 | null | 32,854,993,583,678 | 78 | 210 |
n=map(int,input().split())
*p,=map(int,input().split())
lmin=10**10
ans=0
for pp in p:
ans+=1 if pp<=lmin else 0
lmin=min(pp,lmin)
print(ans)
| def main():
input()
x, y = [list(map(int, input().split())) for _ in range(2)]
for p in (1, 2, 3):
distance = (sum(map(lambda i: i ** p, [abs(xi - yi) for xi, yi, in zip(x, y)]))) ** (1/p)
print('{:.5f}'.format(distance))
distance = max([abs(xi - yi) for xi, yi in zip(x, y)])
print('{:.5f}'.format(distance))
if __name__ == '__main__':
main()
| 0 | null | 42,635,216,680,130 | 233 | 32 |
r = int(input())
print(r * 6.28) | import math
a, b = map(str, input().split())
a2 = int(a)
b2 = int(b.replace('.', ''))
print(int((a2 * b2)//100)) | 0 | null | 24,151,279,355,868 | 167 | 135 |
N, K = (int(x) for x in input().split())
listN = [1] * N
# N is population / K is candy
for i in range(K):
d = int(input())
tmp = map(int, input().split())
for j in tmp:
listN[j-1] = 0
print(sum(listN))
| N, K = map(int, input().split())
okashi_holders = set()
for _ in range(K):
_ = input()
for a in list(map(int, input().split())):
okashi_holders.add(a)
print(len(set(range(1, N + 1)) - okashi_holders))
| 1 | 24,697,652,427,812 | null | 154 | 154 |
N = input()[-1]
if N == "3":
print("bon")
elif N in {"2", "4", "5", "7", "9"}:
print("hon")
else:
print("pon")
| N=str(input())
pon=["0","1","6","8"]
hon=["2","4","5","7","9"]
bon=["3"]
if(N[-1] in pon):
print("pon")
elif(N[-1] in hon):
print("hon")
else:
print("bon") | 1 | 19,283,677,108,520 | null | 142 | 142 |
A, B = input().split(" ")
A = int(A.strip())
B = int(B.strip().replace(".", ""))
print(A * B // 100) | # 解説
n,k,c = map(int, input().split())
s = input()
l = [0 for i in range(n)]
r = [-1 for i in range(n)]
a = 1
b = -1
for i in range(n):
if s[i]=='o' and b<i:
l[i]=a
a+=1
b=i+c
if a>k:
break
a = k
b = n
for i in range(n):
if s[-i-1]=='o' and b>n-i-1:
r[-i-1]=a
a-=1
b=n-i-1-c
if a<1:
break
for i in range(n):
if l[i]==r[i]:
print(i+1)
| 0 | null | 28,691,907,245,860 | 135 | 182 |
#####
# B #
#####
N = input()
sum = sum(map(int,N))
if sum % 9 == 0:
print('Yes')
else:
print('No')
| x = int(input())
ans = x*x*x
print(ans) | 0 | null | 2,356,949,932,220 | 87 | 35 |
A1, A2, A3 = map(int, input().split())
print('win') if A1 + A2 + A3 <= 21 else print('bust') | from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF = float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353
A = map(int, input().split())
if sum(A) >= 22:
print("bust")
else:
print("win")
| 1 | 118,287,005,687,030 | null | 260 | 260 |
N = int(input())
li = list(map(int, input().split()))
an = 1
mi = li[0]
for i in range(1,N):
if mi >= li[i]:
an += 1
mi = li[i]
print(an) | n=int(input())
P=list(map(int,input().split()))
m = n + 1
cnt = 0
for i in range(n):
if m >= P[i]:
m = P[i]
cnt += 1
print(cnt) | 1 | 85,438,539,131,070 | null | 233 | 233 |
from sys import stdin
for line in stdin:
a,b = line.split(" ")
c = int(a)+int(b)
print(len(str(c)))
| # -*-coding:utf-8
import fileinput
if __name__ == '__main__':
for line in fileinput.input():
digit = 0
tokens = list(map(int, line.strip().split()))
a, b = tokens[0], tokens[1]
num = a + b
print(len(str(num))) | 1 | 110,594,608 | null | 3 | 3 |
from copy import deepcopy
n, m, q = map(int, input().split())
tuples = [tuple(map(int, input().split())) for i in range(q)]
As = [[1]*n]
def next(A, i):
if i == 0 and A[i] == m:
return
if A[i] == m:
next(A, i-1)
elif A[i] < m:
if (i < n - 1 and A[i] < A[i+1]) or (i == n-1):
A1 = deepcopy(A)
A1[i] += 1
As.append(A1)
next(A1, i)
if i > 0 and A[i] > A[i-1]:
A2 = deepcopy(A)
next(A2, i-1)
elif i == 0:
A1 = deepcopy(A)
next(A1, n-1)
next([1]*(n), n-1)
def check(A):
score = 0
for a, b, c, d in tuples:
if A[b-1] - A[a-1] == c:
score += d
return score
ans = 0
for A in As:
score = check(A)
ans = max(ans, score)
print (ans) | from itertools import combinations_with_replacement as R
N,M,Q = map(int, input().split())
L = [tuple(map(int, input().split())) for _ in range(Q)]
print(max([sum((d if S[b-1] - S[a-1] == c else 0) for a,b,c,d in L) for S in R(range(1, M+1), N)])) | 1 | 27,789,955,703,690 | null | 160 | 160 |
s = "hi"
S = input()
flag = False
for i in range(5):
if S==s:
flag=True
break
s = s+"hi"
if flag:
print("Yes")
else:
print("No") | def main():
N, M = map(int, input().split())
if N == M:
print("Yes")
exit(0)
print("No")
if __name__ == "__main__":
main()
| 0 | null | 68,587,673,137,060 | 199 | 231 |
# -*- coding: utf-8 -*-
# モジュールのインポート
import math
def get_input() -> int:
"""
標準入力の取得
Returns:\n
int: 標準入力
"""
N = int(input())
return N
def main(N: int) -> None:
"""
求解処理
Args:
N (int): ページ数
"""
result = math.ceil(N/2)
# 結果出力
print(result)
if __name__ == "__main__":
N = get_input()
main(N)
| X, K, D =map(int,input().split())
X = abs(X)
if X > K*D:
print(X-K*D)
else:
greed = X//D
if (K - greed)%2 == 0:
print(X-greed*D)
else:
print((1+greed)*D -X) | 0 | null | 32,053,462,975,138 | 206 | 92 |
D = {"SUN":7,"MON":6,"TUE":5,"WED":4,"THU":3,"FRI":2,"SAT":1}
print(D[input().strip()]) | S=input()
lis=['SUN','MON','TUE','WED','THU','FRI','SAT']
i=lis.index(S)
if S=='SUN':
print(7)
else:
for j in range(1,8):
if lis[i-j]=='SUN':
print(7-j)
break | 1 | 132,767,258,847,892 | null | 270 | 270 |
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT() : return int(input())
def MAP() : return map(int,input().split())
def LIST() : return list(MAP())
def is_ok(arg):
count = 0
for i in range(n):
if a[i] % arg != 0:
count += a[i] // arg
else:
count += a[i] // arg - 1
return count <= k
def meguru_bisect(ng, ok):
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
n, k = MAP()
a = LIST()
if k == 0:
print(max(a))
else:
print(meguru_bisect(0, max(a)+1)) | n=int(input())
orgn=n
m=n-1
orgm=m
ncounter=1
mcounter=1
for i in range(2,int(m**0.5)+1):
if m % i == 0:
tmpc=1
m //= i
while m % i ==0:
tmpc += 1
m //= i
mcounter *= tmpc + 1
if m == orgm and m != 1:
mcounter += 1
elif m > int(orgm**0.5):
mcounter *= 2
mcounter -= 1
for i in range(2,int(n**0.5)+1):
if orgn % i == 0:
n = orgn // i
while n % i ==0:
n //= i
if n % i == 1:
ncounter += 1
print(ncounter+mcounter)
| 0 | null | 24,103,144,456,732 | 99 | 183 |
x, y = [ int(i) for i in input().split()]
if x > y:
x, y = y, x
while True:
if x % y:
x, y = y, x % y
continue
break
print(y)
| from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
n,m=nii()
l=[lnii() for i in range(m)]
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -self.parents[self.find(x)]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
uf=UnionFind(n)
for a,b, in l:
a-=1
b-=1
if uf.same(a,b)==False:
uf.union(a,b)
ans=0
roots=uf.roots()
for i in uf.roots():
ans=max(ans,uf.size(i))
print(ans) | 0 | null | 2,001,895,106,268 | 11 | 84 |
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
def main():
n = int(input())
a = list(enumerate(map(int,input().split())))
a.sort(key = lambda x:x[1],reverse = True)
dp = [[0] * (n + 1) for _ in range(1 + n)]
for i in range(n + 1):
for j in range(i + 1,n):
dp[i][j] = -INF
for j in range(n):
i,num = a[j]
i += 1
for k in range(j + 2):
if k == 0:
dp[j + 1][k] = dp[j][k] + num * abs(i - n + j - k)
else:
dp[j + 1][k] = max(dp[j][k - 1] + num * abs(i - k),dp[j][k] + num * abs(i - n + j - k))
print(max(dp[-1]))
if __name__ =='__main__':
main() | s = input()
inc = 0
dec = 0
increasing = True
ans = 0
for c in s:
if increasing:
if c == '<':
inc += 1
else:
increasing = False
dec += 1
else:
if c == '>':
dec += 1
else:
if inc > dec:
inc, dec = dec, inc
ans += inc * (inc - 1) // 2 + dec * (dec + 1) // 2
increasing = True
inc = 1
dec = 0
else:
if increasing:
ans += inc * (inc + 1) // 2
else:
if inc > dec:
inc, dec = dec, inc
ans += inc * (inc - 1) // 2 + dec * (dec + 1) // 2
print(ans)
| 0 | null | 95,565,444,818,962 | 171 | 285 |
def make_divisors(n):
lower_divisors , upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
def min2(x,y):
return x if x < y else y
N = int(input())
P = make_divisors(N)
L = len(P) + 1
res =float("inf")
for p in P:
res = min2((p - 1) + (N // p - 1), res)
print(res) | import math
N = int(input())
ans = N - 1 #初期値、√Nの中に答えがなかったときはこれ
t = int(math.ceil(N**0.5))
for i in range(1,t+1):
j = N / i
if N % i == 0:
step = i + j -2
if step < ans:
ans = int(step)
print(ans) | 1 | 160,952,184,306,496 | null | 288 | 288 |
N=int(input())
A,B=[],[]
for i in range(N):
x,y=map(int, input().split())
A.append(x+y)
B.append(x-y)
A=sorted(A)
B=sorted(B)
print(max(abs(A[0]-A[-1]),abs(B[0]-B[-1]))) | n = int(input())
plus,minus = [],[]
for i in range(n):
a,b = map(int,input().split())
plus.append(a+b)
minus.append(a-b)
plus.sort()
minus.sort()
print(max(plus[-1]-plus[0],minus[-1]-minus[0])) | 1 | 3,440,630,149,728 | null | 80 | 80 |
cnt = 0
s = input().lower()
while 1:
line = input()
if line == "END_OF_TEXT":
break
for w in line.lower().split():
if w == s:
cnt+=1
print(cnt) | k = int(input())
ans = 0
s = 7
for i in range(1, 10**6 + 1):
s %= k
if s == 0:
print(i)
exit()
s = s*10 +7
print(-1) | 0 | null | 3,969,796,554,042 | 65 | 97 |
#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 math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
return list(map(int,readline().split()))
def readTuples():
return tuple(map(int,readline().split()))
def I():
return int(readline())
n = I()
lis = list(range(1,n+1))
a,b = None,None
P = readInts()
Q = readInts()
i = 0
for A in permutations(lis,n):
if list(A) == P == Q:
a,b = i,i
elif list(A) == Q:
b = i
elif list(A) == P:
a = i
i += 1
print(abs(a-b))
| import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline.rstrip()
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return tuple(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
n = inp()
P = inpl()
Q = inpl()
i = 0
a = 0
b = 0
for p in permutations(range(1,n+1)):
i += 1
if p == P:
a = i
if p == Q:
b = i
print(abs(a-b)) | 1 | 100,397,205,246,532 | null | 246 | 246 |
n = int(input())
al = list(map(int,input().split()))
lst = [[] for _ in range(n)]
for i in range(1,n+1):
lst[al[i-1]-1] = i
print(*lst)
| import bisect
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
cnt = 0
for i in range(n):
for j in range(i+1, n):
x = bisect.bisect_left(l, l[i]+l[j])
cnt += x-j-1
print(cnt) | 0 | null | 176,414,991,990,446 | 299 | 294 |
from itertools import product
n = int(input())
al = []
for _ in range(n):
curr_al = []
a = int(input())
for i in range(a):
x,y = map(int, input().split())
curr_al.append((x,y))
al.append(curr_al)
pattern = 2
ite = product(range(pattern),repeat=n)
ans = 0
for it in ite:
a_01_l = list(it)
curr_ans = a_01_l.count(1)
# print('-----')
# print(a_01_l)
for i, curr_it in enumerate(it):
if curr_it == 1:
check = True
for a in al[i]:
if a_01_l[a[0]-1] != a[1]:
check = False
if not check:
break
else:
# print('ok')
ans = max(curr_ans,ans)
print(ans)
| import sys
Stack = []
MAX = 1000
def push(x):
#if isFull():
# print "ERROR!"
# sys.exit()
Stack.append(x)
def pop():
#if isEmpty():
# print "ERROR!"
# sys.exit()
Stack.pop(-1)
def cul(exp):
for v in exp:
if v == "+":
x = reduce(lambda a,b:int(a)+int(b), Stack[-2:])
for i in range(2):
pop()
push(x)
elif v == "-":
x = reduce(lambda a,b:int(a)-int(b), Stack[-2:])
for i in range(2):
pop()
push(x)
elif v == "*":
x = reduce(lambda a,b:int(a)*int(b), Stack[-2:])
for i in range(2):
pop()
push(x)
else:
push(v)
return Stack[0]
if __name__ == "__main__":
exp = sys.stdin.read().strip().split(" ")
ans = cul(exp)
print ans | 0 | null | 60,958,431,585,590 | 262 | 18 |
[X, Y] = [int(i) for i in input().split()]
if 2*X <= Y and Y <= 4*X and Y % 2 == 0:
print("Yes")
else:
print("No") | x, y = list(map(int, input().split()))
if y % 2 == 0 and 2 * x <= y and y <= 4 * x:
print("Yes")
else:
print("No") | 1 | 13,803,766,130,862 | null | 127 | 127 |
n,m=map(int,input().split())
a =list(map(int,input().split()))
day = 0
for i in range(m):
day += a[i]
print(-1 if day>n else n - day) | N,M = map(int,input().split())
home = list(map(int,input().split()))
if N - sum(home) < 0:
print("-1")
else:
print(N - sum(home)) | 1 | 31,920,373,087,020 | null | 168 | 168 |
#coding:UTF-8
def IS(N,A):
for i in range(int(N)):
v=A[i]
j=i-1
while j>=0 and A[j]>v:
A[j+1]=A[j]
j=j-1
A[j+1]=v
ans=""
for k in range(len(A)):
ans+=str(A[k])+" "
print(ans[:-1])
if __name__=="__main__":
N=input()
a=input()
A=a.split(" ")
for i in range(len(A)):
A[i]=int(A[i])
IS(N,A) | length = int(input())
targ = [int(n) for n in input().split(' ')]
for l in range(length):
for m in range(l):
if targ[l - m] < targ[l - m - 1]:
disp = targ[l-m -1]
targ[l-m-1] = targ[l-m]
targ[l-m] = disp
print(" ".join([str(n) for n in targ])) | 1 | 5,817,046,650 | null | 10 | 10 |
N, K = list(map(int, input().split()))
ans = 0
all_sum = N*(N+1)/2
mod = 10**9+7
for i in range(K,N+2):
min_val = i*(i-1)/2
max_val = N*(N+1)/2 - (N-i+1)*(N-i)/2
ans += (int(max_val) - int(min_val) + 1) % mod
# print(i, min_val, max_val)
print(ans%mod)
| def main():
N,K=map(int,input().split())
res=0
MOD=10**9+7
for i in range(K,N+2):
MAX=(N+(N-i+1))*i//2
MIN=(i-1)*i//2
res+=MAX-MIN+1
res%=MOD
print(res%MOD)
if __name__=="__main__":
main() | 1 | 33,188,116,610,150 | null | 170 | 170 |
import math
N = int(input())
total = N/2*(N+1)
f = math.floor(N/3)
f = (3*f+3)*f/2
b = math.floor(N/5)
b = (5*b+5)*b/2
fb = math.floor(N/15)
fb = (15*fb+15)*fb/2
print(int(total-f-b+fb))
| N = input()
l = len(N)
for i in range(l):
if N[i] =="7":
print("Yes")
exit()
print("No") | 0 | null | 34,343,901,082,646 | 173 | 172 |
k=int(input())
a=0
cnt=-1
for i in range(1,k+1):
a=(10*a+7)%k
if a==0:
cnt=i
break
print(cnt) | def main():
x,y = list(map(int,input().split()))
ans=0
for i in range(0,x+1):
if 2*i+4*(x-i)==y:
ans=1
if ans==1:
print("Yes")
else:
print("No")
main() | 0 | null | 9,942,411,125,330 | 97 | 127 |
import math
a,b,c=(int(x) for x in input().split())
s=1/2*a*b*math.sin(c/180*math.pi)
l=math.sqrt(a**2+b**2-2*a*b*math.cos(c/180*math.pi))+a+b
h=s/a*2
print('{:.05f}'.format(s))
print('{:.05f}'.format(l))
print('{:.05f}'.format(h))
| N,K,S = map(int,input().split())
ANS = [S] * K
if S != 10 ** 9:
ANS += [S+1] * (N-K)
else:
ANS += [S-1] * (N-K)
print(" ".join(map(str,ANS)))
| 0 | null | 45,533,728,471,612 | 30 | 238 |
def main():
S = int(input())
dp = [0]*(S+1)
dp[0] = 1
dp[1] = 0
if S >= 2:
dp[2] = 0
for i in range(3,S+1,1):
for j in range(0,i-2,1):
dp[i] += dp[j]
return dp[S]%1000000007
print(main())
| # -*- coding: utf-8 -*-
## Library
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
## input
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split())
# P = [list(map(int,input().split())) for i in range(M)]
# S = []
# for _ in range(N):
# S.append(list(input()))
N=int(input())
for i in range(1,N+1):
if floor(i*1.08)==N:
print(i)
sys.exit()
print(":(") | 0 | null | 64,232,673,918,688 | 79 | 265 |
order=[]
for number in range(10):
hight=int(input())
order.append(hight)
for j in range(9):
for i in range(9):
if order[i] < order[i+1]:
a = order[i]
b = order[i+1]
order[i] = b
order[i+1] = a
for i in range(3):
print(order[i]) | moutain = [0 for i in range(10)]
for i in range(10):
moutain[i] = int(raw_input())
moutain.sort(reverse=True)
for i in range(3):
print moutain[i]
| 1 | 23,635,880 | null | 2 | 2 |
n = int(input())
graph = [[] for _ in range(n)]
for i in range(n):
in_list = list(map(int, input().split()))
u = in_list[0]
graph[u - 1] = in_list[2:]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]
time = 0
def dfs(now, prev, graph, d, f, time):
if d[now] != 0:
return d, f, time
time += 1
d[now] = time
if not graph[now]:
f[now] = time + 1
time += 1
return d, f, time
for k in graph[now]:
if k - 1 == prev or d[k-1] !=0:
continue
_, _, time = dfs(k - 1, now, graph, d, f, time)
else:
time += 1
f[now] = time
return d, f, time
for i in range(n):
d, f, time = dfs(i, 0, graph, d, f, time)
print(i+1, d[i], f[i])
| n=int(input())
adj=[]
for i in range(n):
a=list(map(int,input().split()))
for j in range(2,len(a)):
a[j]-=1
adj.append(a[2:])
visit_time1=[0]*n
visit_time2=[0]*n
visited=[0]*n
def dfs(now):
global time
visit_time1[now]=time
time+=1
for next in adj[now]:
if not visited[next]:
visited[next]=True
dfs(next)
visit_time2[now]=time
time+=1
time=1
for i in range(n):
if not visited[i]:
visited[i]=True
dfs(i)
for i in range(n):
print(i+1,visit_time1[i],visit_time2[i])
| 1 | 2,875,214,870 | null | 8 | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.