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
|
---|---|---|---|---|---|---|
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
r = list(itertools.permutations([i for i in range(1,n+1)]))
P , Q = (r.index(p)), (r.index(q))
print(abs(P-Q)) | f,l,n=map(int,input().split())
ctr=0
for i in range(f,l+1):
if i%n==0:
ctr+=1
print(ctr) | 0 | null | 54,177,114,578,620 | 246 | 104 |
import math
a, b, x = map(int, input().split())
theta = math.atan((-2) * x / (a ** 3) + 2 * b / a)
if a * math.tan(theta) > b:
theta = math.atan2(a * b * b, 2 * x)
print(math.degrees(theta)) | a, b, x = map(int, input().split())
if x >= a**2*b/2:
t = 2*(b*a**2-x)/(a**3)
else:
t = (a*b**2)/(2*x)
import math
p = math.atan(t)
p = math.degrees(p)
print(p)
| 1 | 163,007,693,516,050 | null | 289 | 289 |
s = str(input())
num = 0
for i in range(len(s)//2):
if s[i] != s[-i-1]:
num += 1
print(num) | import sys
input = sys.stdin.readline
s = list(input())
a = 0
#print(len(s))
for i in range((len(s) - 1) // 2):
#print(s[i], s[-2-i])
if s[i] != s[-2-i]:
a += 1
print(a) | 1 | 119,943,225,738,660 | null | 261 | 261 |
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
a,b,x = list(map(int, input().split()))
import math
if x*2>=a*a*b:
the = (a*b - x/a) / (0.5*a*a)
else:
the = (b*b*a) / (2*x)
ans = math.atan(the)
print(ans*180/math.pi) | S = input()
N = len(S)
ans = [0] * (N + 1)
for i in range(N):
if S[i] == "<":
ans[i + 1] = ans[i] + 1
for i in range(N - 1, -1, -1):
if S[i] == ">":
if ans[i] <= ans[i + 1]:
ans[i] = ans[i + 1] + 1
print(sum(ans)) | 0 | null | 160,161,537,477,322 | 289 | 285 |
from sys import exit
n = int(input())
num_list = [int(i) for i in input().split()]
if 0 in num_list:
print(0)
exit()
ans = 1
for i in range(n):
ans *= num_list[i]
if ans > 10**18:
print(-1)
exit()
print(ans) | N = int(input())
A = list(map(int, input().split()))
if 0 in A:
print(0)
else:
ans = 1
for a in A:
ans *= a
if ans > 10**18:
print(-1)
exit(0)
print(ans)
| 1 | 16,261,893,718,108 | null | 134 | 134 |
n = int(input())
print(len({input() for i in range(n)})) | import numpy as np
N = int(input())
data_list = []
for i in range(N):
si = input()
data_list.append(si)
data_list = np.array(data_list)
print(len(np.unique(data_list)))
| 1 | 30,393,659,301,348 | null | 165 | 165 |
w = str(input()).lower()
ans = 0
while True:
t = str(input())
if t == 'END_OF_TEXT': break
words = t.lower().split()
for word in words:
if word == w: ans += 1
print(ans)
| import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline
def popcount(n):
if n == 0:
return 0
return 1 + popcount(n % bin(n).count("1"))
def main():
N = int(input())
X = input().strip()
x1 = 0
xc = bin(int(X, 2)).count("1")
if xc == 1:
x2 = -1
else:
x2 = 0
for i in range(N):
if X[-1 - i] == '1':
x1 += pow(2, i, xc + 1)
if x2 != -1:
x2 += pow(2, i, xc - 1)
for i in range(N):
if X[i] == '0':
print(1 + popcount((x1 + pow(2, N - i - 1, xc + 1)) % (xc + 1)))
else:
if x2 == -1:
print(0)
else:
print(1 + popcount((x2 - pow(2, N - i - 1, xc - 1) + xc - 1) % (xc - 1)))
if __name__ == '__main__':
main()
| 0 | null | 4,976,923,554,720 | 65 | 107 |
import sys
from collections import deque
n = int(input())
q = deque()
for i in range(n):
c = sys.stdin.readline()[:-1]
if c[0] == 'i':
q.appendleft(c[7:])
elif c[6] == ' ':
try:
q.remove(c[7:])
except:
pass
elif c[6] == 'F':
q.popleft()
else:
q.pop()
print(*q) | n = int(input())
p = list(map(int, input().split()))
cnt = 0
p_min = 2*10**5+1
for i in range(n):
if p_min >= p[i]:
cnt += 1
p_min = min(p_min, p[i])
print(cnt) | 0 | null | 42,749,092,871,258 | 20 | 233 |
N = int(input())
if N == 1 or N == 2:
print(0)
else:
if N % 2 == 0:
print(N//2-1)
else:
print(N//2) | from sys import stdin
import sys
## input functions for me
def ria(sep = ''):
if sep == '' :
return list(map(int, input().split()))
else: return list(map(int, input().split(sep)))
def rsa(sep = ''):
if sep == '' :
return input().split()
else: return input().split(sep)
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##
def main():
N = ri()
if N % 2 == 0:
print(N // 2 - 1)
else:
print(N // 2)
if __name__ == "__main__":
main()
| 1 | 153,759,073,539,548 | null | 283 | 283 |
a = int(input())
alist = list(map(int, input().split()))
mod = 10**9+7
total = sum(alist)
sumlist = []
ans = 0
for i in range(len(alist)-1):
total -= alist[i]
ans += alist[i]*(total)
print(ans%mod) | mod = 10**9+7
N = int(input())
A = list(map(int, input().split()))
#累積和のリストを作成
S = [0]*(N+1)
ans = 0
for i in range(N):
S[i+1] = A[i] + S[i]%mod
#累積和って感じですね
for i in range(N):
sum = S[N] - S[i + 1]
if sum < 0:
sum += mod
ans += A[i] * sum
print(ans % mod) | 1 | 3,779,244,728,220 | null | 83 | 83 |
# coding:utf-8
# ????????????
s = [False] * 13
# ?????????
h = [False] * 13
# ?????????
c = [False] * 13
# ?????????
d = [False] * 13
T = {'S':s, 'H':h, 'C':c, 'D':d}
# ??\???
n = int(input())
for i in range(n):
suit, num = input().split()
num = int(num)
T[suit][num-1] = True
for v in range(13):
if not(T['S'][v]):
print('S', v + 1)
for v in range(13):
if not(T['H'][v]):
print('H', v + 1)
for v in range(13):
if not(T['C'][v]):
print('C', v + 1)
for v in range(13):
if not(T['D'][v]):
print('D', v + 1) | # D - Caracal vs Monster
H = int(input())
def rec(x):
if x==1:
return 1
else:
return 2*rec(x//2)+1
print(rec(H)) | 0 | null | 40,379,881,560,832 | 54 | 228 |
H,W,K = map(int, input().split())
S = [input() for _ in range(H)]
L = [[0]*(W) for _ in range(H)]
for i in range(H):
for j in range(W):
if S[i][j] == "1":
L[i][j] = 1
ans = H*W
for i in range(2**(H-1)):
cnt = 0
used = [0]*W
memo = []
for j in range(H-1):
if i & (1<<j):
cnt += 1
memo.append(j+1)
memo.append(H)
num = [0]*(cnt+1)
for j in range(W):
idx = 0
for k in range(H):
if memo[idx] == k:
idx += 1
num[idx] += L[k][j]
f = False
for k in range(len(num)):
if num[k] > K:
f = True
break
if f:
if j == 0 or used[j-1] == 1:
cnt = H*W
break
used[j-1] = 1
cnt += 1
num = [0]*len(num)
idx = 0
for k in range(H):
if memo[idx] == k:
idx += 1
num[idx] += L[k][j]
ans = min(ans, cnt)
print(ans) | K = int(input())
A, B = map(int, input().split())
x = 1000//K
for n in range(x+1):
if (n * K >= A) and (n * K <= B):
print('OK')
break
if n == x:
print('NG') | 0 | null | 37,701,305,863,144 | 193 | 158 |
from bisect import bisect_right
def bisectsearch_right(L,a):
i=bisect_right(L,a)
return(i)
N,M,K= list(map(int, input().split()))
A= list(map(int, input().split()))
B= list(map(int, input().split()))
Asum=[0]*N
Bsum=[0]*M
Asum[0]=A[0]
Bsum[0]=B[0]
for i in range(1,N):
Asum[i]=Asum[i-1]+A[i]
for j in range(1,M):
Bsum[j]=Bsum[j-1]+B[j]
# print(Asum)
# print(Bsum)
ans=[0]*(N+1)
ans[0]=bisectsearch_right(Bsum,K)
# print(ans[0])
for i in range(1,N+1):
if Asum[i-1]>K:
continue
j=bisectsearch_right(Bsum,K-Asum[i-1])
# print(j)
ans[i]=i+j
# print(ans)
print(max(ans))
| # 2butan renshuu 0812
def f(m):
cut=0
for aa in a:
cut+=(aa-1)//m
if cut > k:
return False
return True
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=0
r=10**9+1
while r-l>1:
m=(l+r)//2
# right move
if f(m):
r=m
else:
l=m
print(r)
| 0 | null | 8,556,073,430,320 | 117 | 99 |
import sys
N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))
alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)
def check(m):
cnt=0
for i in range(N):
if alist[i]*flist[i]>m:
cnt+=-(-(alist[i]*flist[i]-m)//flist[i])
#print(m,cnt,K)
if cnt<=K:
return True
else:
return False
if sum(alist)<=K:
print(0)
sys.exit(0)
baseline=0
for i in range(N):
baseline=max(baseline,alist[i]*flist[i])
#print(baseline)
l,r=0,baseline
while l<=r:
mid=(l+r)//2
#print(l,mid,r)
if not check(mid-1) and check(mid):
print(mid)
break
elif not check(mid):
l=mid+1
else:
r=mid-1 | n = int(input())
s = input()
num = n
for i in range(n-1,0,-1):
if s[i]==s[i-1]:
s = s[:i] + s[i+1:]
print(len(s))
| 0 | null | 167,522,530,369,040 | 290 | 293 |
S = list(str(input()))
count = 0
max = 0
for i in range(3):
if S[i] == "R":
count += 1
else:
if max < count:
max = count
count = 0
if max < count:
max = count
print(max) | def merge(A, left, mid, right):
L = A[left:mid] + [int(1e9)]
R = A[mid:right] + [int(1e9)]
i,j = 0,0
for k in range(left, right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
global count
count += (right - left)
def merge_sort(A, left, right):
if left+1 < right:
mid = (left + right) // 2
merge_sort(A, left, mid)
merge_sort(A, mid, right)
merge(A, left, mid, right)
if __name__=='__main__':
n = int(input())
A = list(map(int, input().strip().split()))
count = 0
merge_sort(A, 0, n)
print(" ".join(map(str, A)))
print(count) | 0 | null | 2,493,954,029,908 | 90 | 26 |
import math
n = int(input())
cnt = 0
mod = 10
if n%2 == 1:
print(0)
exit()
while mod<=n:
cnt += n//mod
mod*=5
print(cnt)
| N = input()
sum_n = 0
for n in N:
num = int(n)
sum_n += num
if(sum_n % 9 == 0):
print("Yes")
else:
print("No")
| 0 | null | 60,335,809,246,170 | 258 | 87 |
from collections import deque
def bfs(start, g, visited):
q = deque([start])
visited[start] = 0
while q:
curr_node = q.popleft()
for next_node in g[curr_node]:
if visited[next_node] >= 0: continue
visited[next_node] = visited[curr_node] + 1
q.append(next_node)
def main():
n,u,v = map(int, input().split())
gl = [ [] for _ in range(n+1)]
visited_u = [-1] * (n+1)
visited_v = [-1] * (n+1)
for i in range(n-1):
a, b = map(int, input().split())
gl[a].append(b)
gl[b].append(a)
bfs(u, gl, visited_u)
bfs(v, gl, visited_v)
# print(visited_u)
# print(visited_v)
# target_i = 0
# max_diff_v = 0
ans = 0
for i in range(1,n+1):
if visited_u[i] < visited_v[i] and visited_v[i]:
# if (visited_v[i]-visited_u[i])%2 == 0:
val = visited_v[i]-1
ans = max(ans,val)
# else:
# val = visited_v[i]
# ans = max(ans,val)
print(ans)
if __name__ == "__main__":
main() | while True:
l = input().split()
if l[1] == '?':
break
print(int(eval(l[0]+l[1]+l[2]))) | 0 | null | 58,977,832,542,738 | 259 | 47 |
D = int(input())
cs = list(map(int, input().split()))
ss = [input().split() for l in range(D)]
ts = [int(input()) for i in range(D)]
ans=0
all_cs=sum(cs)
cs_last=[0]*26
def c0(d):
mainas=0
for i in range(26):
#print(cs[i]*(d-cs_last[i]))
mainas-=cs[i]*(d-cs_last[i])
return mainas
for i in range(D):
cs_last[ts[i]-1]=i+1
ans+=int(ss[i][ts[i]-1])+c0(i+1)
print(ans) | # -*- coding: utf-8 -*-
# 入力
D = int(input())
c_i = list(map(int, input().split()))
s_d_i = []
for d in range(D):
s_d_i.append(list(map(int, input().split())))
last_i = []
for i in range(26):
last_i.append(-1)
# tが入力の場合(Mainでは不要)
t_d = []
for d in range(D):
t_d.append(int(input()))
#scoring
r_sum = 0
for d in range(D):
t = t_d[d] - 1
#last更新
last_i[t] = d
#c
c_sum = 0
for i in range(26):
c_sum += c_i[i] * (d - last_i[i])
#s - c
r_d = s_d_i[d][t] - c_sum
r_sum += r_d
print(r_sum) | 1 | 10,019,832,173,510 | null | 114 | 114 |
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
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 size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def main():
V, M = MI()
uni = UnionFind(V)
for i in range(M):
a,b = MI()
a-=1
b-=1
uni.union(a,b)
print(uni.group_count()-1)
if __name__ == "__main__":
main() | f,h=range,input
R,C,K=map(int,h().split())
l=lambda:[-~C*[0]for i in f(R+1)]
G=l()
for i in'_'*K:r,c,v=map(int,h().split());G[r][c]=v
F=[l()for i in f(4)]
for r in f(1,R+1):
for x in f(3):
for c in f(1,C+1):F[x+1][r][c]=max(F[x][r][c],max(F[x][r][c-1],(x<1)*F[3][r-1][c])+G[r][c],F[x+1][r][c-1])
print(F[3][R][C]) | 0 | null | 3,891,639,232,792 | 70 | 94 |
# -*- coding:utf-8 -*-
a = int(input())
b_int = map(int, input().split())
max = -1000000
min = 1000000
total =0
for i in b_int:
if i > max:
max = i
if i < min:
min = i
total += i
print(min, max, total) | h, w = map(int, input().split())
S = []
for _ in range(h):
s = str(input())
S.append(s)
DP = [[0 for _ in range(w)] for _ in range(h)]
if S[0][0] == '.':
DP[0][0] = 0
else:
DP[0][0] = 1
ren = False
if DP[0][0] == 1:
ren = True
for i in range(1,h):
if S[i][0] == '.':
DP[i][0] = DP[i-1][0]
ren = False
elif ren == False and S[i][0] == '#':
DP[i][0] = DP[i-1][0] + 1
ren = True
elif ren == True and S[i][0] == '#':
DP[i][0] = DP[i-1][0]
ren = True
ren = False
if DP[0][0] == 1:
ren = True
for j in range(1,w):
if S[0][j] == '.':
DP[0][j] = DP[0][j-1]
ren = False
elif ren == False and S[0][j] == '#':
DP[0][j] = DP[0][j-1] + 1
ren = True
elif ren == True and S[0][j] == '#':
DP[0][j] = DP[0][j-1]
ren = True
for i in range(1,h):
for j in range(1,w):
if S[i][j] == '.':
DP[i][j] = min(DP[i-1][j], DP[i][j-1])
elif S[i][j] == '#':
res_i = 0
res_j = 0
if S[i-1][j] == '.':
res_i = 1
if S[i][j-1] == '.':
res_j = 1
DP[i][j] = min(DP[i-1][j] + res_i, DP[i][j-1] + res_j)
print(DP[h-1][w-1]) | 0 | null | 25,120,110,878,082 | 48 | 194 |
class Combination:
def __init__(self, size, mod=10**9 + 7):
self.size = size + 2
self.mod = mod
self.fact = [1, 1] + [0] * size
self.factInv = [1, 1] + [0] * size
self.inv = [0, 1] + [0] * size
for i in range(2, self.size):
self.fact[i] = self.fact[i - 1] * i % self.mod
self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod
def npr(self, n, r):
if n < r or n < 0 or r < 0:
return 0
return self.fact[n] * self.factInv[n - r] % self.mod
def ncr(self, n, r):
if n < r or n < 0 or r < 0:
return 0
return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod
def nhr(self, n, r): # 重複組合せ
return self.ncr(n + r - 1, n - 1)
def factN(self, n):
if n < 0:
return 0
return self.fact[n]
N, K = map(int, input().split())
K = min(K, N)
MOD = 10**9 + 7
comb = Combination(N + 100)
ans = 0
for k in range(K + 1):
ans += comb.ncr(N, k) * comb.nhr(N - k, k)
ans %= MOD
print(ans)
| N,K = map(int,input().split())
ps = list(map(int,input().split()))
su = sum(ps[0:K])
ksum = su
ind = 0
for i in range(1,N-K+1):
ksum -= ps[i-1]
ksum += ps[i+K-1]
if ksum > su:
ind = i
su = ksum
ki = 0
for i in range(ind, ind+K):
ki += (ps[i]*(ps[i]+1))/2/ps[i]
print(ki)
| 0 | null | 70,741,210,123,990 | 215 | 223 |
import sys
sys.setrecursionlimit(10**5)
def DFS(i,g):
if i not in done:
done.add(i)
circle.add(i)
g_list[i]=g
for f in F[i]:
DFS(f,g)
else:
return
N,M,K=map(int,input().split())
F=[[] for _ in range(N)]
B=[[] for _ in range(N)]
for f in range(M):
f1,f2=map(int,input().split())
F[f1-1].append(f2-1)
F[f2-1].append(f1-1)
for b in range(K):
b1,b2=map(int,input().split())
B[b1-1].append(b2-1)
B[b2-1].append(b1-1)
Group=[]
circle=set()
done=set()
g_list=[0 for _ in range(N)]
g=0
for i in range(N):
if i not in done:
DFS(i,g)
Group.append(circle)
circle=set()
g+=1
ans=[-1 for _ in range(N)]
for i in range(N):
G=g_list[i]
ans[i]+=len(Group[G])
for fr in F[i]:
if fr in Group[G]:
ans[i]-=1
for bl in B[i]:
if bl in Group[G]:
ans[i]-=1
print(*ans) | import sys
sys.setrecursionlimit(1000000) #再帰回数を増やさないとREになる
N, M, K = map(int, input().split())
friend = [[] for _ in range(N)]
block = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
friend[a-1].append(b-1)
friend[b-1].append(a-1)
for _ in range(K):
c, d = map(int, input().split())
block[c-1].append(d-1)
block[d-1].append(c-1)
friendnetwork = []
sgn = [0 for _ in range(N)] #groupnumを書いていく
groupnum = 0
def makenetwork(n, groupnum):
for item in friend[n]:
if sgn[item] > 0:
continue
else:
sgn[item] = groupnum
makenetwork(item, groupnum)
for k in range(N):
if sgn[k] == 0:
groupnum += 1
sgn[k] = groupnum
makenetwork(k, groupnum)
friends = [[] for _ in range(groupnum)]
for k in range(N):
friends[sgn[k]-1].append(k)
ans = []
for k in range(N):
num = len(friends[sgn[k]-1])-1 - len(friend[k])
for item in block[k]:
if sgn[item] == sgn[k]:
num -= 1
ans.append(num)
for j in range(len(ans)):
print(ans[j]) | 1 | 61,673,147,910,418 | null | 209 | 209 |
n = int(input())
x = 100000
for i in range(n):
x = x*1.05
if x % 1000 == 0:
pass
else:
x = x//1000
x = x+1
x = x*1000
print('{:.0f}'.format(x))
| n = int(input())
x = 100000
for i in range(n):
x += (x * 0.05 + 999) // 1000 * 1000
print(int(x)) | 1 | 1,001,244,648 | null | 6 | 6 |
n = int(input())
m = [[] for _ in range(n+1)]
for _ in range(n):
tmp = [int(x) for x in input().split()]
u,k,l = tmp[0],tmp[1],tmp[2:]
m[u] += tmp[2:]
dp = [float('inf') for _ in range(n+1)]
dp[1] = 0
queue = [1]
while queue:
t = queue.pop(0)
for x in m[t]:
if dp[x] > dp[t] + 1:
dp[x] = dp[t] + 1
queue.append(x)
for i,x in enumerate(dp):
if i == 0:
pass
else:
if x == float('inf'):
print(i, -1)
else:
print(i, x)
| a = list(map(int,input().split()))
ans = "unsafe" if a[1]>=a[0] else "safe"
print(ans) | 0 | null | 14,470,000,617,082 | 9 | 163 |
class UnionFind():
def __init__(self,n):
self.n=n
self.root=[-1]*n
self.rank=[-1]*n
def Find_Root(self,x):
if self.root[x]<0:return x
else:
self.root[x]=self.Find_Root(self.root[x])
return self.root[x]
def Unite(self,x,y):
x=self.Find_Root(x)
y=self.Find_Root(y)
if x==y:return
elif self.rank[x]>self.rank[y]:
self.root[x] +=self.root[y]
self.root[y]=x
else:
self.root[y] +=self.root[x]
self.root[x]=y
if self.rank[x]==self.rank[y]:
self.rank[y] +=1
def isSameGroup(self,x,y):
return self.Find_Root(x)==self.Find_Root(y)
def size(self,x):
return -self.root[self.Find_Root(x)]
def members(self,x):
root=self.Find_Root(x)
return [i for i in range(self.n) if self.Find_Root(i)==root]
def roots(self):
return[i for i,j in enumerate(self.root) if j<0]
def group_members(self):
return {i:self.members(i) for i in self.roots()}
def group_count(self):
return len(self.roots())
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
UnionFind=UnionFind(N+1)
for i in range(N):
UnionFind.Unite(i+1,P[i])
D=UnionFind.group_members()
E=[]
for i in D:
A,length=D[i],len(D[i])
if A[0]==0:continue
index,F=A[0]-1,[]
while len(F)!=length:
index=P[index]-1
F.append(C[index])
E.append(F)
ans=-float("inf")
for A in E:
length=len(A)
if sum(A)<0:
A=A*2
for i in range(length):
point=0
for j in range(min(K,length)):
point +=A[i+j]
ans=max(ans,point)
else:
div,mod=divmod(K,length)
base=(div-1)*sum(A)
A=A*3
for i in range(length):
point=0
for j in range(mod+length):
point +=A[i+j]
ans=max(ans,base+point)
print(ans) | n = int(input())
cn= 1
an= [1]
i = 1
while cn < n / 4:
an.append(cn*3+1)
cn=cn*3+1
an.sort(reverse=True)
def insertionSort(a,n,g):
cnt=0
for i in range(g,n):
v = a[i]
j = i - g
while j>= 0 and a[j]>v:
a[j + g]= a[j]
j = j-g
cnt+=1
a[j + g]=v
return cnt
def shellSort(a,n,an):
cnt=0
m = len(an)
g = an
for i in an:
cnt += insertionSort(a,n,i)
return cnt
x=[]
for i in range(n):
x.append(int(input()))
print(len(an))
print(" ".join([str(i) for i in an]))
y= shellSort(x,n,an)
print(y)
for i in range(n):
print(x[i]) | 0 | null | 2,747,068,432,512 | 93 | 17 |
d,t,s=[int(i) for i in input().split()]
if d<=t*s:
print("Yes")
else:
print("No") | # A - Don't be late
D, T, S = map(int, input().split())
if (D + S - 1) // S <= T:
print('Yes')
else:
print('No') | 1 | 3,572,239,548,988 | null | 81 | 81 |
n = int(input())
a = [(_a, i) for i, _a in enumerate(map(int, input().split()))]
a.sort(reverse=True)
dp = [[0]*(n+1) for _ in range(n+1)]
for i, (v, p) in enumerate(a):
# 前にj人いる
dp[i+1][0] = dp[i][0] + v*abs((n-1 - i) - p)
for j in range(1, i+1):
dp[i+1][j] = max(dp[i][j] + v*abs((n-1 - (i-j)) - p),
dp[i][j-1] + v*abs((j-1) - p))
dp[i+1][i+1] = dp[i][i] + v*abs(i - p)
print(max(dp[n]))
| # from pprint import pprint
n = int(input())
a = map(int, input().split())
a = sorted(((ax, x) for x, ax in enumerate(a)), reverse=True)
dp = [[-1] * (n+1) for _ in range(n+1)]
dp[0][0] = 0
for i, (ax, x) in enumerate(a):
for j in range(i+1):
dp[j+1][i-j] = max(dp[j+1][i-j], dp[j][i-j] + ax*abs(x-j))
dp[j][i-j+1] = max(dp[j][i-j+1], dp[j][i-j] + ax*abs(n-1-x-i+j))
# pprint(dp)
print(max(dp[i][n-i] for i in range(n+1)))
| 1 | 33,466,056,484,120 | null | 171 | 171 |
a = int(input())
b = a/3
print(b*b*b) |
def resolve():
INF = float("-inf")
N = int(input())
A = list(map(int, input().split()))
skip = 1 + N % 2
# i番目までの要素を見たときに、j個選ばない時の最大値
dp = [[INF] * (skip + 2) for _ in range(N + 1)]
dp[0][0] = 0
for i in range(N):
for j in range(skip + 1): # j: スキップする個数
# 今見ている要素をスキップ
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j])
now = dp[i][j]
if (i + j) % 2 == 0:
now += A[i]
dp[i + 1][j] = max(dp[i + 1][j], now)
print(dp[N][skip])
if __name__ == "__main__":
resolve() | 0 | null | 42,240,524,157,678 | 191 | 177 |
def main():
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
l = ["" for i in range(K)]
s = 0
for i in range(N):
# print(l)
if l[i%K] == T[i]:
l[i%K]=""
continue
if l[i%K] == "":
if T[i] == "r":
s = s+P
l[i%K] = "r"
elif T[i] == "s":
s = s+R
l[i%K] = "s"
else:
s = s+S
l[i%K] = "p"
elif T[i]=="r":
s = s+P
l[i%K] = "r"
elif T[i]=="s":
s = s+R
l[i%K] = "s"
else:
s = s+S
l[i%K] = "p"
print(s)
if __name__ == "__main__":
main() | l = raw_input()
print l.swapcase()
| 0 | null | 54,044,795,639,940 | 251 | 61 |
from collections import deque
import numpy as np
# (sx, sy) から (gx, gy) への最短距離を求める
# 辿り着けないと INF
def bfs(sx, sy):
# すべての点を INF で初期化
d = [[float("-inf")] * m for i in range(n)]
# 移動4方向のベクトル
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
# スタート地点をキューに入れ、その点の距離を0にする
que = deque([])
que.append((sx, sy))
d[sx][sy] = 0
# キューが空になるまでループ
while que:
# キューの先頭を取り出す
p = que.popleft()
# 取り出してきた状態がゴールなら探索をやめる
#if p[0] == gx and p[1] == gy:
# break
# 移動4方向をループ
for i in range(4):
# 移動した後の点を (nx, ny) とする
nx = p[0] + dx[i]
ny = p[1] + dy[i]
# 移動が可能かの判定とすでに訪れたことがあるかの判定
# d[nx][ny] != INF なら訪れたことがある
if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] != "#" and d[nx][ny] == float("-inf"):
# 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する
que.append((nx, ny))
d[nx][ny] = d[p[0]][p[1]] + 1
a = np.max(d)
return a
n, m = map(int, input().split())
maze = [list(input()) for i in range(n)]
ans = 1
for x in range(n):
for y in range(m):
sx = x
sy = y
if maze[sx][sy] == "#":
continue
A = bfs(sx, sy)
ans = max(A, ans)
print(int(ans)) | from collections import deque
def bfs(i, j):
dist = [[0] * w for _ in range(h)]
q = deque()
q.append((i, j))
dist[i][j] = 1
while q:
nx, ny = q.pop()
for dx, dy in D:
X, Y = nx + dx, ny + dy
if X < 0 or Y < 0 or X >= h or Y >= w:
continue
if m[X][Y] == "#":
continue
if dist[X][Y] != 0:
continue
q.appendleft((X, Y))
dist[X][Y] = 1 + dist[nx][ny]
mx = 0
for i in dist:
mx = max(mx, max(i))
return mx
h, w = map(int, input().split())
m = [input() for _ in range(h)]
D = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = 0
for i in range(h):
for j in range(w):
if m[i][j] == ".":
ans = max(ans, bfs(i, j))
print(ans - 1) | 1 | 94,581,318,967,850 | null | 241 | 241 |
N = int(input())
res = 0
if N % 2 == 0:
div = N // 2
for i in range(1,30):
res += div // 5**i
print(res)
| N = int(input())
if N % 2 == 1:
print(0)
else:
n = 0
i = 1
while True:
if N < 2*5**i:
break
n += (N // 5**i // 2)
i += 1
print(n)
| 1 | 116,281,146,286,240 | null | 258 | 258 |
def dfs(seq):
ans = 0
if len(seq) == n:
score = 0
for a, b, c, d in req:
if seq[b-1] - seq[a-1] == c:
score += d
return score
else:
for i in range(seq[-1], m+1):
next_seq = seq + (i,)
score = dfs(next_seq)
ans = max(ans, score)
return ans
n, m, q = list(map(int, input().split()))
req = [list(map(int, input().split())) for _ in range(q)]
print(dfs((1,)))
| k = int(input())
s = input()
if(len(s) <= k):
print(s)
else:
print(s[0:k]+'...')
| 0 | null | 23,765,813,500,092 | 160 | 143 |
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
n, m, l = map(int, input().split())
F = np.zeros((n, n))
for _ in range(m):
a, b, c = map(int, input().split())
a -= 1
b -= 1
F[a, b] = c
F[b, a] = c
csr = csr_matrix(F)
A = floyd_warshall(csr)
G = np.zeros((n, n))
for i in range(n-1):
for j in range(i+1, n):
if A[i][j] <= l:
G[i, j] = 1
G[j, i] = 1
ncsr = csr_matrix(G)
B = floyd_warshall(ncsr)
q = int(input())
for _ in range(q):
s, t = map(int, input().split())
k = B[s-1][t-1]
if k == float("inf"):
print(-1)
else:
print(int(k)-1) | N = input()
if N.count('7') > 0:
print("Yes")
else:
print('No')
| 0 | null | 103,825,085,886,914 | 295 | 172 |
d = int(input())
C = list(map(int, input().split()))
S = []
for _ in range(d):
S.append(list(map(int, input().split())))
v = 0
LDs = [0 for _ in range(26)]
for i in range(d):
t = int(input()) - 1
R = [(i + 1 - LDs[j]) * C[j] for j in range(26)]
del R[t]
v += S[i][t] - sum(R)
LDs[t] = i + 1
print(v) | s = input()
if s == "RRR":
print(3)
elif s == "RRS" or s == "SRR":
print(2)
elif s == "RSS" or s == "RSR" or s == "SRS" or s == "SSR":
print(1)
else:
print(0)
| 0 | null | 7,351,350,721,050 | 114 | 90 |
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 MAP1() : return map(lambda x:int(x)-1,input().split())
def LIST() : return list(MAP())
def LIST1() : return list(MAP1())
def solve():
N = INT()
a = []
b = []
for i in range(N):
A, B = MAP()
a.append(A)
b.append(B)
a.sort()
b.sort()
if N % 2 == 1:
am = a[(N+1)//2-1]
bm = b[(N+1)//2-1]
ans = bm - am + 1
else:
am = ( a[N//2-1]+a[N//2] ) / 2
bm = ( b[N//2-1]+b[N//2] ) / 2
ans = int( ( bm - am ) * 2 + 1 )
print(ans)
if __name__ == '__main__':
solve() | import sys
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
from math import gcd
a,b=MI()
print((a*b)//gcd(a,b))
main()
| 0 | null | 65,493,929,538,368 | 137 | 256 |
import numpy as np
n,m,k = list(map(int, input().split()))
a_list = np.array(input().split()).astype(int)
b_list = np.array(input().split()).astype(int)
a_sum =[0]
b_sum=[0]
for i in range(n):
a_sum.append(a_sum[-1]+ a_list[i])
for i in range(m):
b_sum.append(b_sum[-1] + b_list[i])
#print(a_sum, b_sum)
total = 0
num = m
for i in range(n+1):
if a_sum[i] > k:
break
while (k - a_sum[i]) < b_sum[num]:
num -=1
#print(i, num)
if num == -1:
break
total = max(i+num, total)
print(total) | import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
class SWAG(object):
def __init__(self,dot):
self.__front=[]
self.__back=[]
self.__dot=dot
def __bool__(self):
return True if(self.__front or self.__back) else False
def __len__(self):
return len(self.__front)+len(self.__back)
def append(self,x):
back=self.__back
if(not back): back.append((x,x))
else: back.append((x,self.__dot(back[-1][1],x)))
def popleft(self):
assert(self)
front=self.__front; back=self.__back
if(not front):
front.append((back[-1][0],back[-1][0]))
back.pop()
while(back):
front.append((back[-1][0],self.__dot(back[-1][0],front[-1][1])))
back.pop()
front.pop()
def sum(self):
assert(self)
front=self.__front; back=self.__back
if(not front): return back[-1][1]
elif(not back): return front[-1][1]
else: return self.__dot(front[-1][1],back[-1][1])
def resolve():
n,m=map(int,input().split())
S=list(map(int,input()))
swag=SWAG(min)
dp=[INF]*(n+1)
dp[n]=0
for i in range(n-1,-1,-1):
if(i+1+m<=n): swag.popleft()
swag.append(dp[i+1])
if(S[i]==1): continue
dp[i]=swag.sum()+1
if(dp[0]==INF):
print(-1)
return
ans=[]
now=0
next=0
while(now!=n):
next+=1
if(dp[next]!=INF and dp[now]!=dp[next]):
ans.append(next-now)
now=next
print(*ans)
resolve() | 0 | null | 74,470,034,688,710 | 117 | 274 |
# https://atcoder.jp/contests/agc043/tasks/agc043_a
import sys
# sys.setrecursionlimit(100000)
def input():
return sys.stdin.readline().strip()
def input_int():
return int(input())
def input_int_list():
return [int(i) for i in input().split()]
ans = (100 * 100) + 1
def main():
global ans
h, w = input_int_list()
grid = []
grid.append(["#"] * (w + 2))
for _ in range(h):
grid.append(["#"] + list(input()) + ["#"])
grid.append(["#"] * (w + 2))
dp = [[101 for _ in range(w + 2)] for _ in range(h + 2)]
if grid[1][1] == "#":
dp[1][1] = 1
else:
dp[1][1] = 0
for i in range(1, h + 1):
for j in range(1, w + 1):
# 上のマスからくる場合を評価
if i > 1:
if grid[i - 1][j] == "." and grid[i][j] == "#":
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j])
else:
dp[i][j] = min(dp[i][j], dp[i - 1][j])
# 左のマスからくる場合を評価
if j > 1:
if grid[i][j - 1] == "." and grid[i][j] == "#":
dp[i][j] = min(dp[i][j - 1] + 1, dp[i][j])
else:
dp[i][j] = min(dp[i][j - 1], dp[i][j])
print(dp[h][w])
return
if __name__ == "__main__":
main()
| n = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
A = [a+1 for a in A]
C = [0]*(n+1)
C[0] = 3
ans = 1
for a in A:
if C[a-1] < C[a]:
print(0)
exit()
else:
ans *= C[a-1]-C[a]
ans %= mod
C[a] += 1
print(ans) | 0 | null | 89,406,845,081,180 | 194 | 268 |
n,m=input().split()
n=int(n)
m=int(m)
f=[0]*n
wa=[0]*n
ac=0
for i in range(m):
p,s=input().split()
p=int(p)-1
if f[p]==1:
continue
if s=="WA":
wa[p]+=1
else:
ac+=1
f[p]=1
x=0
for i in range(n):
if f[i]==1:
x+=wa[i]
print(ac,x) | # ABC170
n = int(input())
a = list(map(int, input().split()))
a.sort()
a_max = max(a)
dp = [True for _ in range(a_max+1)]
ans = 0
for i in range(n):
if dp[a[i]]:
for j in range(0, a_max+1, a[i]):
dp[j] = False
if i > 0:
if a[i] == a[i-1]:
continue
if i < n-1:
if a[i] == a[i+1]:
continue
ans += 1
print(ans)
| 0 | null | 53,734,103,025,900 | 240 | 129 |
def solve(n):
ans = 0
for i in range(1, n+1):
m = n // i
ans += m * (m+1) * i // 2
return ans
n = int(input())
print(solve(n)) | # Problem D - Sum of Divisors
# input
N = int(input())
# initialization
count = 0
# count
for j in range(1, N+1):
M = N // j
count += j * ((M * (M+1)) // 2)
# output
print(count)
| 1 | 11,069,997,984,682 | null | 118 | 118 |
m1,_ = input().split()
m2,_ = input().split()
if m1==m2:
print(0)
else:
print(1) | # input
N, K, C = map(int, input().split())
S = input()
# process
l = [i+1 for i in range(N) if S[i] == 'o']
left = [l[0]]
right = [l[-1]]
for i in range(1, len(l)):
if l[i] > left[-1]+C:
left.append(l[i])
if l[-i-1] < right[-1]-C:
right.append(l[-i-1])
# output
# print(l)
# print(left)
# print(right)
if len(left) == K:
for i in range(len(left)):
if left[i] == right[-i-1]:
print(left[i])
| 0 | null | 82,488,800,332,160 | 264 | 182 |
import math
def gcd(a,b):
if a == 0:
return b
if b == 0:
return a
if a >= b:
return gcd(b, a%b)
else:
return gcd(a, b%a)
a,b = map(int, input().split())
print(gcd(a,b)) | a, b = map(int, input().split())
c =1
if b > a:
a, b = b, a
while c != 0:
c = a % b
a = b
b = c
print(a) | 1 | 7,411,794,580 | null | 11 | 11 |
def main():
num=map(int,input().split())
print(" ".join([str(x) for x in sorted(num)]))
if __name__=='__main__':
main()
| N = int(input())
S = input()
count_R = 0
count_G = 0
count_B = 0
count = 0
for i in range(N):
if S[i] == 'R':
count_R = count_R+1
elif S[i] == 'G':
count_G = count_G+1
else:
count_B = count_B+1
count = count_R*count_G*count_B
for i in range(N):
for j in range(i+1,N):
if S[i] != S[j]:
k = 2*j-i
if k>=N:
break
else:
if S[i] != S[k] and S[j] != S[k]:
count = count-1
print(count) | 0 | null | 18,231,989,656,380 | 40 | 175 |
import math
n, d = map(int,input().split( ))
#print(n,d)
number = 0
for a in range(n):
x, y = map(int, input().split( ))
if math.sqrt(x**2 + y**2) <= d:
number += 1
print(number)
| n = int(input())
c = 0
for _ in range(n):
r = [int(e) for e in input().split(" ")]
if(r[0] == r[1]):
c += 1
else:
c = 0
if(c == 3):
print("Yes")
break
else:
print("No")
| 0 | null | 4,200,936,848,810 | 96 | 72 |
S = raw_input()
S1, S2 = [], []
ans = pool = 0
for i in xrange(len(S)):
if S[i] == "/" and len(S1) > 0:
j = S1.pop()
ans += i - j
a = i - j
while (len(S2) > 0 and S2[-1][0] > j):
a += S2.pop()[1]
S2.append([j, a])
if S[i] == "\\":
S1.append(i)
print ans
if len(S2) > 0:
print len(S2), " ".join(map(str, [a for j, a in S2]))
else:
print 0 | N=int((input()))
if N%1000==0:
print(0)
else:
N=str(N)
a = len(N)
if a>=3:
x = int(N[a-3]+N[a-2]+N[a-1])
print(1000-x)
elif a==2:
x = int(N[a-2]+N[a-1])
print(1000-x)
else :
x = int(N[a-1])
print(1000-x) | 0 | null | 4,229,975,059,100 | 21 | 108 |
r = int(input())
r2 = r**2
ans = r**2 / 1
print(int(ans)) | from sys import stdin
r = int(stdin.readline())
if r < 1:
print(0)
else:
circle = r * r
print(circle) | 1 | 145,242,685,644,060 | null | 278 | 278 |
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
S = input()
l = len(S)
print("x" * l)
if __name__ == '__main__':
main()
| s=input()
a=['x' for _ in range(len(s))]
ans=''
for aa in a:
ans+=aa
print(ans) | 1 | 72,760,037,736,630 | null | 221 | 221 |
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit
INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7
def mapline(t = int):
return map(t, sysread().split())
def mapread(t = int):
return map(t, read().split())
def run():
N, *A = mapread()
dp = [defaultdict(lambda:0) for _ in range(N+1)]
dp[0][(0,0,0)] = 1
for i in range(N):
a = A[i]
k = i + 1
for key in dp[k-1].keys():
for ii, v in enumerate(key):
if v == a:
tmp = list(key)
tmp[ii] += 1
dp[k][tuple(tmp)] += dp[k-1][key] % mod
ans = 0
for key in dp[N].keys():
ans = (ans + dp[N][key]) % mod
#print(dp)
print(ans)
if __name__ == "__main__":
run()
| h,w = map(int,input().split())
if h == 1 or w == 1:
print(1)
elif (w*h) % 2 == 0:
print((w*h)//2)
elif (w*h) % 2 == 1:
print(((w*h)//2)+1)
| 0 | null | 90,263,015,078,600 | 268 | 196 |
N = int(input())
A = map(int, input().split())
A = sorted(enumerate(A), key=lambda x: x[1], reverse=True)
dp = [[0]*(N+1) for _ in range(N+1)]
for n, (from_i, a) in enumerate(A):
for j in range(n + 1):
dp[n+1][j+1] = max(dp[n+1][j+1], dp[n][j] + a*(from_i - j))
dp[n+1][j] = max(dp[n+1][j], dp[n][j] + a*(N - (n - j) - 1 - from_i))
print(max(dp[N]))
| n = input()
a = map(int, raw_input(). split())
while n != 0:
print(a[n-1]),
n-=1 | 0 | null | 17,489,240,912,420 | 171 | 53 |
import math
a = float(input())
b = math.acos(-1)
print "%f %f" % (a * a * b , 2 * a * b) | # encoding:utf-8
import math as ma
# math.pi
x = float(input())
pi = (x * x) * ma.pi
# Circumference
pi_line = (x + x) * ma.pi
print("{0} {1}".format(pi,pi_line)) | 1 | 626,947,408,708 | null | 46 | 46 |
li1 = []
li2 = []
ans = 0
for i, s in enumerate(input()):
if s == "\\":
li1.append(i)
elif s == "/" and li1:
j = li1.pop()
c = i - j
ans += c
while li2 and li2[-1][0] > j:
c += li2[-1][1]
li2.pop()
li2.append((j, c))
print(ans)
if li2:
print(len(li2), *list(zip(*li2))[1])
else:
print(0)
| n = input()
s1 = []
s2 = []
for i in range(len(n)):
if n[i] == '\\':
s1.append(i)
elif n[i] == '/' and len(s1) > 0:
a = s1.pop(-1)
s2.append([a, i - a])
i = 0
while len(s2) > 1:
if i == len(s2) - 1:
break
elif s2[i][0] > s2[i + 1][0]:
s2[i + 1][1] += s2.pop(i)[1]
i = 0
else:
i += 1
s = []
total = 0
for i in s2:
s.append(str(int(i[1])))
total += int(i[1])
print(total)
if len(s2) == 0:
print('0')
else:
print('{} {}'.format(len(s2), ' '.join(s)))
| 1 | 59,583,866,200 | null | 21 | 21 |
[print(len(str(i)))for i in[sum(int(e)for e in ln.split())for ln in __import__("sys").stdin]] | l = list(map(int, input().split()))
while(l != []):
try:
length = len(str(l[0]+l[1]))
print(length)
l = list(map(int, input().split()))
except:
break
| 1 | 170,463,920 | null | 3 | 3 |
from functools import reduce
N = int(input())
S = input()
ans = [chr(ord("A") + (ord(s) + N - ord("A"))%26) for s in S]
ans = reduce(lambda x, y: x + y, ans)
print(ans) | N=int(input())
S=input()
indices = list(map(ord, S))
indices2 = []
for idx in indices:
if idx + N > 90: indices2.append(idx - 26 + N)
else: indices2.append(idx + N)
S2 = list(map(chr,indices2))
print(''.join(S2)) | 1 | 134,560,437,059,008 | null | 271 | 271 |
def resolve():
n = int(input())
s = [input() for _ in range(n)]
print(len(set(s)))
resolve() | mod = 998244353
n,s = map(int, input().split())
a = list(map(int, input().split()))
table = [0]*(s+1)
table[0] = 1
ans = 0
for ai in a:
nxt = [0]*(s+1)
if ai <= s:
for j in range(ai, s+1):
nxt[j] = table[j-ai]
table = [(2*table[i] + nxt[i])%mod for i in range(s+1)]
ans += table[-1]
ans %= mod
print(ans)
| 0 | null | 24,175,726,093,918 | 165 | 138 |
s=input()
li=list(s)
adana_li=[]
for i in range(3):
adana_li.append(li[i])
adana=li[0]+li[1]+li[2]
print(adana) | K=int(input())
S=input()
if len(S) <= K :
print(S)
else:
print(S[:K]+'...',end='') | 0 | null | 17,208,686,352,064 | 130 | 143 |
n=int(input())
a=list(map(int,input().split()))
cnt=[0]*(max(a)+4)
cnt[0]=3
mod=10**9+7
ans=1
for i in range(n):
ans*=cnt[a[i]]
ans%=mod
cnt[a[i]]-=1
cnt[a[i]+1]+=1
print(ans)
| n=int(input())
a=list(map(int,input().split()))
l=[0,0,0]
mod=10**9+7
ans=1
for i in a:
ans*=l.count(i)
ans%=mod
for j in range(3):
if l[j]==i:
l[j]+=1
break
print(ans) | 1 | 129,740,901,657,228 | null | 268 | 268 |
mountain = []
for _ in range(10):
mountain.append(int(input()))
mountain.sort()
mountain.reverse()
print(mountain[0])
print(mountain[1])
print(mountain[2]) | #!/usr/bin/env python3
hight = []
for i in range(10):
hight.append(int(input()))
hight = sorted(hight, reverse=True)
for i in range(3):
print(hight[i]) | 1 | 29,768,860 | null | 2 | 2 |
n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [[10**8]*(n+1) for i in range(m+1)]
dp[0][0] = 0
for i in range(m):
for j in range(n+1):
dp[i+1][j] = min(dp[i][j], dp[i+1][j])
if j + c[i] <= n:
dp[i+1][j+c[i]] = min(dp[i+1][j]+1, dp[i+1][j+c[i]])
print(dp[m][n])
| n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [float("inf")] * (n+1)
dp[0] = 0
for i in range(1, n + 1):
for j in range(m):
if i >= c[j]:
dp[i] = min(dp[i], dp[i-c[j]]+1)
print(dp[n])
| 1 | 147,388,209,540 | null | 28 | 28 |
s, t = map(str, input().split())
a, b = map(int, input().split())
u = str(input())
sums = 0
if s == u:
a -= 1
if t == u:
b -= 1
print(a, b) | # -*- coding: utf-8 -*-
def selection_sort(A, N):
change = 0
for i in xrange(N):
minj = i
for j in xrange(i, N, 1):
if A[j] < A[minj]:
minj = j
if minj != i:
temp = A[minj]
A[minj] = A[i]
A[i] = temp
change += 1
return (A, change)
if __name__ == "__main__":
N = input()
A = map(int, raw_input().split())
result, change = selection_sort(A, N)
print ' '.join(map(str, result))
print change | 0 | null | 36,130,794,936,752 | 220 | 15 |
def selectionSort(A, N): # N個の要素を含む0-オリジンの配列A
A = list(map(int, A.split(" ")))
change_count = 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] を交換
tmp = A[i]
A[i] = A[minj]
A[minj] = tmp
change_count+=1
# 回答
print(" ".join(map(str,A)))
print(change_count)
#return True
# 入力
N = int(input()) # 数列の長さを表す整数(N)
A = input("") # N個の整数が空白区切り
selectionSort(A,N)
| def linear_search(A, n, key):
A.append(key)
i = 0
while A[i] != key:
i += 1
A.pop()
return i != n
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
ans = 0
for t in T:
if linear_search(S, n, t):
ans += 1
print(ans)
| 0 | null | 45,154,483,808 | 15 | 22 |
#解説参照
#i桁目で10^i円多くはらうか否か
n_ =list(input())
n = [int(i) for i in n_]
ans = 0
if len(n_)==1:
print(min(n[0],11-n[0]))
exit()
dp1 = 0
dp2 = 1
for i in range(len(n)):
dp_1 = min(dp1+n[i],dp2+10-n[i])
dp_2 = min(dp1+n[i]+1,dp2+9-n[i])
dp1 = dp_1
dp2 = dp_2
print(dp1)
| #create date: 2020-06-30 14:42
import sys
stdin = sys.stdin
from itertools import combinations_with_replacement
def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))
def main():
n, m, q = na()
matrix = list()
for i in range(q):
matrix.append(na())
l = list(combinations_with_replacement(range(1, m+1), n))
ans = 0
for li in l:
res = 0
for q in matrix:
a, b, c, d = q
if li[b-1] - li[a-1] == c:
res += d
ans = max(ans, res)
print(ans)
if __name__ == "__main__":
main() | 0 | null | 49,332,361,260,864 | 219 | 160 |
print "\n".join(['{0}x{1}={2}'.format(x,y,x*y) for x in range(1,10) for y in range(1,10)]) | #coding:UTF-8
def QQ():
for i in range(1,10):
for j in range(1,10):
print(str(i)+"x"+str(j)+"="+str(i*j))
if __name__=="__main__":
QQ() | 1 | 2,713,438 | null | 1 | 1 |
n,k,s=map(int,input().split())
if s==1000000000:
x=[s]*k+[1]*(n-k)
else:
x=[s]*k+[s+1]*(n-k)
print(*x) | N=int(input())
ans=[]
while(1):
if N==0:
break
tmp=N%26
if tmp==0:
ans.append(26)
N=N//26-1
else:
N//=26
ans.append(tmp)
new_ans = list(reversed(ans))
ANS=""
for i in new_ans:
ANS+=chr(96+int(i))
print(ANS) | 0 | null | 51,790,540,668,116 | 238 | 121 |
def main():
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
move = T1*(A1-B1) + T2*(A2-B2)
mid = T1*(A1-B1)
if move==0:
return -1
if move*mid > 0:
return 0
if move < 0:
move *= -1
else:
mid *= -1
if mid%move == 0:
return 2*(mid//move)
else:
return 2*(mid//move)+1
if __name__ == "__main__":
ans = main()
if ans==-1:
print("infinity")
else:
print(ans)
| import sys
import time
import math
import itertools as it
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N = int(input())
mn = 1001001001001
a = 1
while a*a <= N:
if N % a == 0:
mn = min(mn, a + N//a - 2)
a += 1
print(mn)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
| 0 | null | 146,717,985,367,188 | 269 | 288 |
text = input()
times = int(input())
for i in range(times):
function = input().split()
word = text[int(function[1]):int(function[2])+1]
if function[0] == "print":
print(word)
elif function[0] == "reverse":
word_reverse = word[::-1]
text = text[:int(function[1])] + word_reverse + text[int(function[2])+1:]
elif function[0] == "replace":
text = text[:int(function[1])] + function[3] + text[int(function[2])+1:] | s=input()
for i in range(int(input())):
sou_com=input().split()
if sou_com[0]=='print':
print(s[int(sou_com[1]):int(sou_com[2])+1])
elif sou_com[0]=='reverse':
s=s[:int(sou_com[1])]\
+s[int(sou_com[1]):int(sou_com[2])+1][::-1]\
+s[int(sou_com[2])+1:]
elif sou_com[0]=='replace':
s=s[:int(sou_com[1])]\
+sou_com[3]\
+s[int(sou_com[2])+1:] | 1 | 2,097,623,216,480 | null | 68 | 68 |
def resolve():
'''
code here
'''
N = input()
res = 0
if int(str(N)[-1]) % 2 == 0:
i = 0
N=int(N)
while int(N) >= 2*5**i:
i += 1
res += N//(2*5**i)
print(res)
if __name__ == "__main__":
resolve()
| N=int(input())
if N%2==1:
print(0)
exit(0)
ans=N//10
cur=N//10
while cur//5>0:
cur//=5
ans+=cur
print(ans)
| 1 | 115,746,081,394,578 | null | 258 | 258 |
#!/usr/bin/env python
n, k = map(int, input().split())
a = list(map(int ,input().split()))
def check(x):
now = 0
for i in range(n):
now += (a[i]-1)//x
return now <= k
l = 0
r = int(1e9)
while r-l > 1:
mid = (l+r)//2
if check(mid):
r = mid
else:
l = mid
print(r) | def solve():
N, K = map(int,input().split())
A = list(map(int,input().split()))
left = 0
right = 10 ** 9
while right - left > 1:
mid = (right+left) // 2
cnt = 0
for a in A:
cnt += (a-1) // mid
if cnt <= K:
right = mid
else:
left = mid
print(right)
if __name__ == '__main__':
solve()
| 1 | 6,514,642,808,684 | null | 99 | 99 |
import sys
input = sys.stdin.readline
n = int(input())
num = list(map(int, input().split()))
mod = 10**9+7
import fractions
lc = num[0]
for i in range(1, n):
lc = lc * num[i] // fractions.gcd(lc, num[i])
lc %= mod
def modinv(a, mod=10**9+7):
return pow(a, mod-2, mod)
ans =0
for i in range(n):
ans += lc*modinv(num[i],mod)
ans %= mod
print(ans) | from collections import defaultdict
facts = defaultdict(int)
mod=10**9+7
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
n=int(input())
a=[int(i) for i in input().split()]
for i in range(n):
for b,p in factorization(a[i]):
if facts[b]<p:
facts[b]=p
ans=0
f=1
for k,v in facts.items():
f*=pow(k,v,mod)
f%=mod
for i in range(n):
ans+=f*pow(a[i],mod-2,mod)
ans%=mod
print(ans) | 1 | 87,630,488,819,500 | null | 235 | 235 |
def selsort(array):
counter = 0
for i in range(len(array)):
minnum = i
for c in range(i,len(array)):
if array[c] < array[minnum]:
minnum = c
if minnum != i:
x = array[i]
array[i] = array[minnum]
array[minnum] = x
counter = counter + 1
array.append(counter)
N = int(input())
arr = [int(i) for i in input().split()]
selsort(arr)
print(' '.join([str(i) for i in arr[:-1]]))
print(arr[-1]) | N,M=map(int,input().split())
class UnionFind():
def __init__(self,n): #n:要素数
self.n=n
self.parents = [-1]*n
#parents:各要素の親要素番号を格納
#要素が根である場合、-(そのグループの要素数)を格納する
def find(self,x): #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のグループとyのグループを併合する
x = self.find(x)
y = self.find(y)
if x==y:
return
if self.parents[x] > self.parents[y]: #|xのグループ|<|yのグループ|
x,y=y,x
self.parents[x] += self.parents[y]
self.parents[y] = x
#よりグループ数が多い方の根にもう一方のグループを接続
def size(self,x): #xの属するグループのサイズ
return -self.parents[self.find(x)]
uf=UnionFind(N)
for i in range(M):
a,b=map(lambda x:int(x)-1,input().split())
uf.union(a,b)
ans=0
for i in range(N):
if uf.size(i) > ans:
ans=uf.size(i)
print(ans) | 0 | null | 1,960,133,679,420 | 15 | 84 |
a,b=(map(int,input().split()))
c=0
if a<=b:
print(0)
elif a>b:
c=a-b*2
if c>0:
print(c)
elif c<=0:
print(0) | from sys import stdin, setrecursionlimit
input = stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))
max_A = max(A)
if max_A == 1:
print("pairwise coprime")
exit()
class Prime:
def __init__(self, n):
self.prime_factor = [i for i in range(n + 1)]
self.prime = []
for i in range(2, n + 1):
if self.prime_factor[i] == i:
for j in range(2, n // i + 1):
self.prime_factor[i * j] = i
self.prime.append(i)
#print(self.prime_factor)
#print(self.prime)
def factorize(self, m):
ret = []
while m != 1:
if not ret:
ret = [[self.prime_factor[m], 1]]
elif self.prime_factor[m] == ret[-1][0]:
ret[-1][1] += 1
else:
ret.append([self.prime_factor[m], 1])
m //= self.prime_factor[m]
return ret
def divisors(self, m):
ret = []
for i in range(1, int(m ** 0.5) + 1):
if m % i == 0:
ret.append(i)
if i != m // i:
ret.append(m // i)
#self.divisors.sort()
return ret
pm = Prime(max_A)
def is_pairwise_coprime(arr):
cnt = [0] * (max_A + 1)
for a in arr:
for b, c in pm.factorize(a):
cnt[b] += 1
if max(cnt[2:]) <= 1:
return 1
else:
return 0
def gcd(a, b):
while b:
a, b = b, a % b
return a
def is_setwise_coprime(A):
gcd_a = 0
for a in A:
gcd_a = gcd(gcd_a, a)
if gcd_a == 1:
return 1
else:
return 0
if is_pairwise_coprime(A):
print("pairwise coprime")
elif is_setwise_coprime(A):
print("setwise coprime")
else:
print("not coprime") | 0 | null | 85,401,447,663,480 | 291 | 85 |
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1
n,m=map(int,input().split())
A=sorted(map(int,input().split()))
B=[0]+A[:]
for i in range(n):
B[i+1]+=B[i]
def solve_binary(mid):
tmp=0
for i,ai in enumerate(A):
tmp+=n-bisect.bisect_left(A,mid-ai)
return tmp>=m
def binary_search(n):
ok=0
ng=n
while abs(ok-ng)>1:
mid=(ok+ng)//2
if solve_binary(mid):
ok=mid
else:
ng=mid
return ok
binresult=binary_search(2*10**5+1)
for i ,ai in enumerate(A):
ans+=ai*(n-bisect.bisect_left(A,binresult-ai))+B[n]-B[bisect.bisect_left(A,binresult-ai)]
count+=n-bisect.bisect_left(A,binresult-ai)
# print(ans,count)
ans-=binresult*(count-m)
print(ans)
# print(binresult) | def main():
h,w = map(int,input().split(" "))
s = [list(input()) for i in range(h)]
dp = [[0]*w for i in range(h)]
dp[0][0] = 1 if s[0][0]=="#" else 0
for i in range(1,h):
sgmdown = 1 if s[i][0]=="#" and s[i-1][0]=="." else 0
dp[i][0] = dp[i-1][0] + sgmdown
for j in range(1,w):
sgmright = 1 if s[0][j]=="#" and s[0][j-1]=="." else 0
dp[0][j] = dp[0][j-1] + sgmright
for i in range(1,h):
for j in range(1,w):
sgmdown = 1 if s[i][j]=="#" and s[i-1][j]=="." else 0
sgmright = 1 if s[i][j]=="#" and s[i][j-1]=="." else 0
dp[i][j] = min(dp[i-1][j]+sgmdown, dp[i][j-1]+sgmright)
print(dp[h-1][w-1])
main() | 0 | null | 78,448,213,488,348 | 252 | 194 |
d=raw_input().split(" ")
l=list(map(int,d))
if(l[2]>0 and l[3]>0):
if(l[0]>=(l[2]+l[4])):
if(l[1]>=(l[3]+l[4])):
print "Yes"
else:
print "No"
else:
print "No"
else:
print "No"
| s = list(input())
n = len(s)-1
count = 0
if s != s[::-1]:
for i in range(0,(n+1)//2):
if s[i] != s[n-i]:
count += 1
print(count) | 0 | null | 60,363,829,524,518 | 41 | 261 |
#!/usr/bin/env python3
import sys
from itertools import chain
def solve(N: int, K: int, H: "List[int]"):
H = sorted(H, reverse=True)
return sum(H[K:])
def main():
tokens = chain(*(line.split() for line in sys.stdin))
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
H = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
answer = solve(N, K, H)
print(answer)
if __name__ == "__main__":
main()
| import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18
def solve():
#n = int(stdin.readline().rstrip())
n,k = map(int, stdin.readline().rstrip().split())
H = list(map(int, stdin.readline().rstrip().split()))
#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
#word = [stdin.readline().rstrip() for _ in range(n)]
#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
#zeros = [[0] * w for i in range(h)]
H.sort()
if n <= k:
print(0)
exit()
for i in range(k):
H.pop(-1)
print(sum(H))
if __name__ == '__main__':
solve()
| 1 | 78,611,468,578,008 | null | 227 | 227 |
first_flag = True
while True:
H, W = map(int, input().split())
if H == 0 and W == 0: break
if not first_flag:
print()
first_flag = False
print(("#" * W + "\n") * H) | while True:
H,W=map(int,raw_input().split())
if H==W==0:
break
elif H!=0 or W!=0:
print (('#'*W +'\n')*H) | 1 | 776,763,153,408 | null | 49 | 49 |
from math import pi
r = float(input())
S = r * r * pi
L = 2 * r * pi
print(f'{(S):.6f} {(L):.6f}')
| import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect
from collections import defaultdict
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))
# sys.setrecursionlimit(10**6)
def solve():
s = inp().strip();
dic = defaultdict(int); dic[0] = 1
# sett.add(0)
ans = 0; cum = 0
for i in range(len(s)-1, -1, -1):
cum += (int(s[i])*pow(10, (len(s)-i-1), 2019)) % 2019
cum %= 2019
dic[cum] += 1
# print(dic)
for i in dic:
ans += ((dic[i])*(dic[i]-1))//2
print(ans)
if __name__ == "__main__":
solve() | 0 | null | 15,716,957,134,120 | 46 | 166 |
import sys
input = sys.stdin.readline
(n, k), (r, s, p), t = map(int, input().split()), map(int, input().split()), input()[:-1]
d, res = {'r': p, 's': r, 'p': s}, 0
for i in range(k):
b = False
for j in range(i, n, k):
if i == j: res += d[t[j]]; b = False; continue
if (t[j] != t[j-k]) | b: res += d[t[j]]; b = False
else: b = True
print(res) | N, K = map(int, input().split())
A = tuple(map(int, input().split()))
T = input()
bestmove = {'r':2, 's':0, 'p':1}
ans = 0
for k in range(K):
t = T[k::K]
prev = -1
for opp in t:
me = bestmove[opp]
if prev != me:
prev = me
ans += A[me]
else:
prev = -1
print(ans)
| 1 | 107,224,974,959,772 | null | 251 | 251 |
a, b = map(int, input().split())
def gcd(a, b):
if a > b:
a, b = b, a
if b % a == 0:
return a
else:
return gcd(a, b % a)
print(gcd(a, b)) | x,y = map(int, raw_input().split())
while y > 0:
x,y = y,x%y
print x | 1 | 7,968,362,020 | null | 11 | 11 |
n = input()
m = len(n)
x = 0
for i in range(m):
x += int(n[i])
print("Yes" if x % 9 == 0 else "No") | print('No' if sum(map(int, input())) % 9 else 'Yes') | 1 | 4,387,971,537,220 | null | 87 | 87 |
#146_F
n, m = map(int, input().split())
s = input()[::-1]
ans = []
flg = True
cur = 0
while cur < n and flg:
for to in range(cur + m, cur, -1):
if to > n:
continue
if s[to] == '0':
ans.append(to - cur)
cur = to
break
if to == cur + 1:
flg = False
if flg:
print(*ans[::-1])
else:
print(-1) | def main():
n, m = map(int, input().split())
s = input()
ss = s[::-1]
if n <= m:
print(n)
else:
cnt = 0
for i in range(n + 1):
if s[i] == "1":
cnt += 1
else:
cnt = 0
if cnt >= m:
print(-1)
exit()
pos = 0
res = []
while n != pos:
for i in reversed(range(m + 1)):
if pos + i > n:
continue
if ss[pos + i] != "1":
pos += i
res.append(i)
break
print(*res[::-1])
if __name__ == '__main__':
main()
| 1 | 139,345,209,953,768 | null | 274 | 274 |
#B問題
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(0,len(A)):
for j in range(i+1,len(A)):
for k in range(j+1,len(A)):
if A[i] + A[j] > A[k] and A[j] + A[k] > A[i] and A[k] + A[i] > A[j] and A[i] != A[j] and A[j] != A[k] and A[k] != A[i] :
ans += 1
print(ans) | n,k=map(int,input().split())
i=0
ans=0
while k**i <= n:
ans+=1
i+=1
print(ans) | 0 | null | 34,786,642,945,080 | 91 | 212 |
N, X, T = map(int, input().split())
t = N % X
s = N // X
if t != 0:
print(T * (s + 1))
else:
print(T * s) | n, x, t = map(int, input().split())
time = 0
cnt = 0
while cnt < n:
time += t
cnt += x
print(time)
| 1 | 4,298,886,009,658 | null | 86 | 86 |
N,K=map(int,input().split())
P=[1 for i in range(N)]
for i in range(K):
d=int(input())
a=list(map(int,input().split()))
for j in a:
x=j-1
P[x]=0
print(sum(P)) | import itertools # accumulate, compress, permutations(nPr), combinations(nCr)
import bisect # bisect_left(insert位置), bisect_right(slice用)
# import math # factorical(階乗) # hypot(距離)
# import heapq
# from fractions import gcd # Python3.5以前 # lcm(最小公倍数) = (a*b)//gcd(a,b)
# from fractions import Fraction
# from math import gcd # Python3.6以降
# --------------------------------------------------------------
n = int(input())
bo = list(map(int,input().split()))
cnt = 0
bo.sort()
for a in range(n-1):
for b in range(a+1,n):
cnt += bisect.bisect_left(bo, bo[a]+bo[b]) - (b+1)
print(cnt) | 0 | null | 98,510,602,698,912 | 154 | 294 |
from collections import deque
h,w = map(int,input().split())
maze = []
for _ in range(h):
maze.append(input())
ans = 0
move = [[1,0],[-1,0],[0,1],[0,-1]]
for i in range(h):
for j in range(w):
if(maze[i][j]=="."):
dist = [ [99999999]*w for _ in range(h) ]
dq = deque()
y=i
x=j
d=0
dist[y][x]=0
dq.append((y,x,d))
while(len(dq)):
y,x,d = dq.popleft()
for m in move:
if((0<=x+m[0]<w) and (0<=y+m[1]<h) and
(dist[y+m[1]][x+m[0]] > d+1) and
(maze[y+m[1]][x+m[0]] == ".")):
dist[y+m[1]][x+m[0]]=d+1
dq.append((y+m[1],x+m[0],d+1))
ans = max(ans,d)
# print(d,i,j)
print(ans) | 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 | 60,931,337,975,132 | 241 | 160 |
x,y = map(int,input().split())
n = int((y-2*x)/2)
m = int((4*x-y)/2)
if x == m+n and m >= 0 and n >= 0:
print('Yes')
else:
print('No')
| N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
###Bをi冊目まで読むのにどれだけ時間がかかるか
P = [0]*M
P[0] = B[0]
for i in range(1,M):
P[i] = P[i-1] + B[i]
T = K###残り時間
ans = 0
num = M-1
for i in range(N):
T -= A[i]
if T < 0:
break
while P[num] > T and num >= 0:
num -= 1
ans = max(ans,i+num+2)
#print(i,num,ans)
###Aを1冊も読まない場合
for i in range(M):
if P[i] <= K:
ans = max(ans,i+1)
else:
break
print(ans) | 0 | null | 12,382,563,150,148 | 127 | 117 |
# -*- coding: utf-8 -*-
import math
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(M):
N -= A[i]
if N >= 0:
print(N)
else:
print(-1) | n = int(input())
debt = 100000
for i in range(n):
debt *= 1.05
if debt%1000 != 0:
debt += 1000 - debt%1000
print(int(debt)) | 0 | null | 16,083,866,230,910 | 168 | 6 |
import math
while True:
try:
a,b =map(int,input().split())
c =math.gcd(a,b)
d =a*b// math.gcd(a,b)
print(c,d)
except:
break
| n=int(input())
s=input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans=r*g*b
for i in range(len(s)):
for j in range(i+1,len(s)):
if s[i]!=s[j]:
k=j+j-i
if k>=len(s):
break
else:
if s[i]!=s[k] and s[j]!=s[k]:
ans-=1
print(ans)
| 0 | null | 18,067,470,791,490 | 5 | 175 |
#coding: UTF-8
import sys
class Algo:
@staticmethod
def insersion_sort(A, N):
for i in range(1, N):
for k in A:
if k==A[len(A)-1]:
print(k)
else:
print(k, " ", sep="", end="")
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 A:
if k==A[len(A)-1]:
print(k)
else:
print(k, " ", sep="", end="")
N = int(input())
A = list(map(int, input().split()))
Algo.insersion_sort(A, N) | import sys, re
def echo(A):
for i, a in enumerate(A):
if i < len(A) - 1:
sys.stdout.write('%d ' % a)
else:
sys.stdout.write('%d\n' % a)
def insertionSort(A, N):
echo(A)
for i in range(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
echo(A)
n = int(raw_input())
a_text = raw_input()
splits = re.split(' ', a_text)
a = []
for i, s in enumerate(splits):
if s:
a.append(int(s))
insertionSort(a, n) | 1 | 5,888,789,760 | null | 10 | 10 |
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np
import sys
def input():
return sys.stdin.readline().strip()
N, M, L = list(map(int, input().split()))
A = [0]*M
B = [0]*M
C = [0]*M
for i in range(M):
Ai, Bi, Ci = list(map(int, input().split()))
A[i] = Ai - 1
B[i] = Bi - 1
C[i] = Ci
graph = csr_matrix((C, (A, B)), shape=(N, N))
#print(graph)
dist = floyd_warshall(graph, directed=False)
graph2 = np.full((N, N), np.inf)
graph2[dist <= L] = 1
graph2 = csr_matrix(graph2)
num = floyd_warshall(graph2, directed=False)
num = num.tolist()
Q = int(input())
for q in range(Q):
s, t = list(map(int, input().split()))
if num[s - 1][t - 1] < 10000000:
print(int(num[s - 1][t - 1] - 1))
else:
print(-1) | N,D=map(int,input().split())
ans=0
for _ in range(N):
X,Y=map(int,input().split())
if X*X+Y*Y<=D*D:
ans+=1
print(ans)
| 0 | null | 89,669,509,650,020 | 295 | 96 |
def main():
n = int(input())
if n % 10 == 3:
print("bon")
return
elif n % 10 == 0 or n % 10 == 1 or n % 10 == 6 or n % 10 == 8:
print("pon")
return
else:
print("hon")
return
main() | N = int(input())
if (N % 10 == 0) or (N % 10 == 1) or (N % 10 == 6) or (N % 10 == 8):
print("pon")
elif N % 10 == 3:
print("bon")
else:
print("hon") | 1 | 19,233,543,282,162 | null | 142 | 142 |
import math
def main():
a,b,c = map(int,input().split())
left = 4*a*b
right = (c-a-b)**2
if (c-a-b)<0:
return 'No'
if left < right:
return 'Yes'
else:
return 'No'
print(main())
| N,M = (int(x) for x in input().split())
AC = [False]*N
WA_count = [0]*N
AC_count = 0
for i in range(M):
p,S = (y for y in input().split())
if AC[int(p)-1] == False:
if S == 'AC':
AC[int(p)-1] = True
AC_count += 1
else:
WA_count[int(p)-1] += 1
for i in range(N):
if AC[i] == False:
WA_count[i] = 0
print(AC_count,sum(WA_count)) | 0 | null | 72,563,570,350,960 | 197 | 240 |
import sys
input = sys.stdin.readline
from collections import deque
import math
n, d, a = map(int, input().split())
XH = []
for _ in range(n):
x, h = map(int, input().split())
XH.append((x, h))
XH.sort()
answer = 0
damage = 0
QUEUE = deque()
for x, h in XH:
if QUEUE:
while x > QUEUE[0][0]:
damage -= QUEUE[0][1]
QUEUE.popleft()
if not QUEUE:
break
h -= damage
if h <= 0:
continue
bomb = math.ceil(h / a)
QUEUE.append((x + 2 * d, a * bomb))
damage += a * bomb
answer += bomb
print(answer)
| import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
### BIT
def init(bit, values):
for i,v in enumerate(values):
update(bit,i,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
res = 0
while i > 0:
res += bit[i]
i -= i&(-i)
return res
#ai += x(logN)
def update(bit,i,x):
while i <= len(bit)-1:
bit[i] += x
i += i&(-i)
return
n,d,a = map(int, input().split())
xh = [None]*n
# h = [None]*n
for i in range(n):
xh[i] = tuple(map(int, input().split()))
xh.sort()
bit = [0] * (n+1)
r = 0
val = 0
done = False
for l,(x,h) in enumerate(xh):
bound = x+2*d
while r<n and xh[r][0]<=bound:
r += 1
nokori = h + query(bit, l+1)
if nokori>0:
# print(nokori, a, l, r, bit)
num = nokori//a + int(nokori%a!=0)
val += num
update(bit, l+1, -num*a)
if r<n:
update(bit, r+1, num*a)
print(val) | 1 | 82,148,711,418,102 | null | 230 | 230 |
S = int(input())
if S<3:
print(0)
exit()
clear = 0
memory = dict()
def hoge(S):
clear = 0
i = 3
while True:
if (S-i)==0:
clear+=1
break
elif S-i in memory:
clear += memory[S-i]
elif S-i>=3:
memory[S-i] = hoge(S-i)
clear += memory[S-i]
i += 1
return clear
print(hoge(S)%(10**9+7))
| s=int(input())
MOD=10**9+7
fact=[0]*s
invfact=[0]*s
fact[0]=1
for i in range(1,s):
fact[i]=fact[i-1]*i % MOD
invfact[s-1]=pow(fact[s-1],MOD-2,MOD)
for i in range(s-1,0,-1):
invfact[i-1]=invfact[i]*i%MOD
def nCk(n, k):
return fact[n]*invfact[k]*invfact[n-k]
ans = 0
for n in range(1,s):
if 3*n>s:
break
ans+=nCk(s-2*n-1,n-1)
ans%=MOD
print(ans)
| 1 | 3,305,793,610,730 | null | 79 | 79 |
N = int(input())
i = 100
cnt = 0
while True:
i += i // 100
cnt += 1
if i >= N:
print(cnt)
exit() | #k = int(input())
#s = input()
#a, b = map(int, input().split())
#l = list(map(int, input().split()))
x = int(input())
yokin = 100
for i in range(1, 100000000000):
yokin *= 101
yokin = yokin //100
if (yokin >= x):
print(i)
break
| 1 | 26,952,514,468,732 | null | 159 | 159 |
N = input()
K = int(input())
l = len(N)
dp = [[[0]*(K+1) for _ in range(2)] for _ in range(l)]
dp[0][0][1] = 1
dp[0][1][1] = int(N[0]) - 1
dp[0][1][0] = 1
#print(dp[0])
for i in range(1,l):
for j in range(K+1):
if int(N[i]) == 0:
if j > 0:
dp[i][0][j] = dp[i-1][0][j]
dp[i][1][j] += 9*dp[i-1][1][j-1]
else:
if j > 0:
dp[i][0][j] = dp[i-1][0][j-1]
dp[i][1][j] += dp[i-1][0][j-1]*(int(N[i])-1) + 9*dp[i-1][1][j-1]
dp[i][1][j] += dp[i-1][0][j]
dp[i][1][j] += dp[i-1][1][j]
#print(dp[i])
print(dp[l-1][0][K]+dp[l-1][1][K]) | import sys
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))
N = ins()
K = ini()
def solve():
M = len(N)
dp_eq = [[0 for _ in range(K + 1)] for _ in range(M)]
dp_less = [[0 for _ in range(K + 1)] for _ in range(M)]
dp_eq[0][K - 1] = 1
dp_less[0][K] = 1
dp_less[0][K - 1] = ord(N[0]) - ord("0") - 1
for i in range(1, M):
is_zero = N[i] == "0"
for k in range(K + 1):
if is_zero:
dp_eq[i][k] = dp_eq[i - 1][k]
elif k < K:
dp_eq[i][k] = dp_eq[i - 1][k + 1]
dp_less[i][k] = dp_less[i - 1][k]
if k < K:
dp_less[i][k] += dp_less[i - 1][k + 1] * 9
if not is_zero:
dp_less[i][k] += dp_eq[i - 1][k]
if k < K:
dp_less[i][k] += dp_eq[i - 1][k + 1] * (ord(N[i]) - ord("0") - 1)
return dp_eq[M - 1][0] + dp_less[M - 1][0]
print(solve())
| 1 | 75,948,386,381,472 | null | 224 | 224 |
a,b,k=map(int,input().split())
if a>k:
a=a-k
b=b
elif a+b<=k:
b=0
a=0
elif a<=k:
b=b+(a-k)
a=0
print(a,b)
| from math import floor
from fractions import Fraction
a,b=map(str,input().split())
a=int(a)
b=Fraction(b)
print(int(a*b))
| 0 | null | 60,190,759,135,712 | 249 | 135 |
n, m = map(int, input().split())
m2 = m
A = [list(map(int,input().split())) for i in range(n)]
b=[]
while m2 > 0:
b_data = int(input())
b.append(b_data)
m2 = m2-1
for i in range(0,n):
c = 0
for j in range(0,m):
c = c + (A[i][j] * b[j])
print(c)
| n, m = [int(_) for _ in input().split()]
a = []
for _ in range(n):
a.append([int(_) for _ in input().split()])
b = []
for _ in range(m):
b.append(int(input()))
for ai in a:
print(sum([a_c * b_c for a_c, b_c in zip(ai, b)])) | 1 | 1,178,436,402,360 | null | 56 | 56 |
import sys
import math
from collections import deque
import heapq
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()
N = I()
a = IL()
b = []
for i in range(N):
b.append((a[i], i))
b.sort(reverse=True)
dp = [[0]*(N+1) for i in range(N+1)] # dp[i番目まで][左からj番目]
for i in range(N):
happy, ID = b[i]
#print(b[i])
for l in range(i+1):
lcnt = l
rcnt = i - lcnt
r = N-1 - rcnt
if i > l-1:
dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + happy*abs(ID - l))
dp[i+1][l] = max(dp[i+1][l], dp[i][l] + happy*abs(ID - r))
else:
break
#print(l,r)
#print(dp)
#print("---")
print(max(dp[-1])) | def rec_z():
for i in range(N):
for j in range(Hmax+1):
if j >= a[i]:
dp[j] = min(dp[j], dp[j-a[i]] + b[i])
return min(dp[H:Hmax+1])
H, N = map(int,input().split())
a = []
b = []
for i in range(N):
aa, bb = map(int, input().split())
a.append(aa)
b.append(bb)
Hmax = H + max(a)
INF = 10**9
dp = [INF for _ in range(Hmax+1)]
dp[0] = 0
print(rec_z()) | 0 | null | 57,322,934,544,410 | 171 | 229 |
from fractions import gcd
def lcm(a,b):
return a*b//gcd(a,b)
n,m=map(int,input().split())
a=list(map(int,input().split()))
h=list(map(lambda x:x//2,a))
l=1
for i in range(n):
l=lcm(l,h[i])
for i in range(n):
if (l//h[i])%2==0:
print(0)
exit()
print((m//l+1)//2) | import sys
read = sys.stdin.read
def main():
h, n = map(int, input().split())
m = map(int, read().split())
mm = zip(m, m)
large_num = 10**9
dp = [large_num] * (h + 10**4 + 1)
dp[0] = 0
for a, b in mm:
for i1 in range(h + 1):
if dp[i1 + a] > dp[i1] + b:
dp[i1 + a] = dp[i1] + b
r = min(dp[h:])
print(r)
if __name__ == '__main__':
main() | 0 | null | 91,433,305,152,868 | 247 | 229 |
n, x, y = map(int, input().split())
ans = [0 for i in range(n-1)]
for i in range(1, n):
for j in range(i+1, n+1):
t = min(abs(j-i), abs(x-i)+1+abs(y-j))
ans[t-1] += 1
for a in ans:
print(a)
| N = input()
N = int(N)
for a in range(0,100):
n = N-10*a
if n <=9:
break;
if n == 2 or n == 4 or n == 5 or n == 7 or n == 9:
print('hon')
elif n == 0 or n == 1 or n == 6 or n == 8:
print('pon')
elif n == 3:
print('bon') | 0 | null | 31,882,196,107,762 | 187 | 142 |
'''
Created on 2020/09/10
@author: harurun
'''
def main():
import sys
pin=sys.stdin.readline
pout=sys.stdout.write
perr=sys.stderr.write
N,M=map(int,pin().split())
if N==3:
ans=[1,0,0]
for _ in [0]*M:
s,c=map(int,pin().split())
if s==1 and c==0:
print(-1)
return
if s!=1 and ans[s-1]!=0 and ans[s-1]!=c:
print(-1)
return
ans[s-1]=c
a=""
for i in ans:
a+=str(i)
print(a)
return
elif N==2:
ans=[1,0]
for _ in [0]*M:
s,c=map(int,pin().split())
if s==1 and c==0:
print(-1)
return
if s!=1 and ans[s-1]!=0 and ans[s-1]!=c:
print(-1)
return
ans[s-1]=c
a=""
for i in ans:
a+=str(i)
print(a)
return
else:
if M==0:
print(0)
return
s,c=map(int,pin().split())
ans=c
for j in range(M-1):
s,c=map(int,pin().split())
if c!=ans:
print(-1)
return
print(ans)
return
main() | N, M = map(int, input().split())
s = [0] * M
c = [0] * M
for i in range(M):
s[i], c[i] = map(int, input().split())
ran = []
for i in range(10 ** (N - 1), 10 ** N):
ran.append(i)
if N == 1:
ran.append(0)
ran.sort()
minimum = 10 ** N
for r in ran:
st = str(r)
ok = True
for j in range(M):
if st[s[j] - 1] != str(c[j]):
ok = False
if ok == True:
minimum = min(minimum, r)
break
if minimum == 10 ** N:
print(-1)
else:
print(minimum)
| 1 | 60,903,510,127,580 | null | 208 | 208 |
s = input()
n = len(s)
k = int(input())
start = True
start_count = 0
start_char = s[0]
inside_num = 0
end_count = 0
end_char = ""
memory = s[0]
count = 1
for i in range(1,n):
if s[i] == memory:
count += 1
else:
if start:
start_count = count
start = False
else:
inside_num += count//2
count = 1
memory = s[i]
end_count = count
end_char = memory
ans = 0
if start_char == end_char:
if end_count == n:
ans = n*k//2
else:
ans += inside_num*k
ans += (start_count+end_count)//2*(k-1)+start_count//2+end_count//2
else:
inside_num += start_count//2
inside_num += end_count//2
ans += inside_num*k
print(ans) | def solve(a, v, b, w, t):
return "YES" if 0 < (v-w) and abs(b-a) / (v-w) <= t else "NO"
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
print(solve(a, v, b, w, t)) | 0 | null | 94,882,501,676,950 | 296 | 131 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.