code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,629B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import heapq
N,M=map(int,input().split())
G=[[] for i in range(N)]
for i in range(M):
a,b=map(int,input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
dist=[10**15 for i in range(N)]
prev=[-1 for i in range(N)]
dist[0]=0
q=[]
heapq.heappush(q,(0,0))
while(len(q)>0):
c,v=heapq.heappop(q)
if dist[v]<c:
continue
for d in G[v]:
if dist[d]>dist[v]+1:
dist[d]=dist[v]+1
heapq.heappush(q,(dist[d],d))
prev[d]=v
print("Yes")
for i in range(1,N):
print(prev[i]+1)
| from collections import deque
n,m=map(int,input().split())
e=[[] for _ in range(n+1)]
lndmrk=[-1]*(n+1)
for i in range(m):
a,b=map(int,input().split())
e[a].append(b)
e[b].append(a)
q=deque()
lndmrk[1]=0
q.append(1)
while(len(q) !=0):
v=q.popleft()
for i in e[v]:
if lndmrk[i]==-1:
lndmrk[i]=v
q.append(i)
f=True
for i in range(2,n+1):
if lndmrk[i]==-1:
f=False
break
if f:
print('Yes')
for i in range(2,n+1):
print(lndmrk[i])
else:
print('No')
| 1 | 20,578,229,364,000 | null | 145 | 145 |
main=list(map(int,input().split()));count=0
for i in range(main[0],main[1]+1):
if(i%main[2]==0): count=count+1
print(count) | L,R,d = list(map(int, input().split()))
print(R//d - (L-1)//d)
| 1 | 7,616,950,373,640 | null | 104 | 104 |
import math
n = int(input())
while 1:
flg=1
for i in range(2,int(math.sqrt(n))+1):
if n % i == 0:
flg=0
break
if flg==1:
print(n)
break
n+=1
| r = int(input())
print(r*2*314/100) | 0 | null | 68,547,211,328,820 | 250 | 167 |
K=int(input())
S=input()
if K>=len(S):
print(S)
else:
for i in range(K):
print(S[i],end='')
print("...") | K=int(input())
S=input()
if len(S)<=K:
print(S)
else:
for i in range(K):
print(S[i],end="")
print("...") | 1 | 19,668,467,081,088 | null | 143 | 143 |
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def S(): return sys.stdin.readline().rstrip()
N,T = map(int,S().split())
A = [0]
B = [0]
for i in range(N):
a,b = LI()
A.append(a)
B.append(b)
dp1 = [[0]*(T+1) for i in range(N+1)]
# dp[i][j] = 1~i番目から選んでT分以内に食べるときの美味しさの和の最大値
dp2 = [[0]*(T+1) for i in range(N+2)]
# dp[i][j] = i~N番目から選んでT分以内に食べるときの美味しさの和の最大値
for i in range(1,N+1):
for j in range(T+1):
if j >= A[i]:
dp1[i][j] = max(dp1[i-1][j],dp1[i-1][j-A[i]]+B[i])
else:
dp1[i][j] = dp1[i-1][j]
for i in range(N,0,-1):
for j in range(T+1):
if j >= A[i]:
dp2[i][j] = max(dp2[i+1][j],dp2[i+1][j-A[i]]+B[i])
else:
dp2[i][j] = dp2[i+1][j]
ans = dp1[N][T]
for i in range(1,N+1): # 最後にi番目の料理を食べる
for j in range(T):
ans = max(ans,dp1[i-1][j]+dp2[i+1][T-1-j]+B[i])
print(ans) | N, T = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)])
dp = [[0] * N for _ in range(T)]
for i in range(1, T):
for j in range(N-1):
if AB[j][0] <= i:
dp[i][j] = max(dp[i][j-1], dp[i-AB[j][0]][j-1] + AB[j][1])
else:
dp[i][j] = dp[i][j-1]
ans = 0
for j in range(1, N):
ans = max(ans, dp[-1][j-1] + AB[j][1])
print(ans) | 1 | 151,465,384,479,760 | null | 282 | 282 |
x = list(map(int,input().split()))
i = 0
while (x[i] != 0):
i+=1
print(i+1) | n,m = map(int,input().split())
dac = {}
for i in range(1,n+1):
dac[str(i)] = 0
dwa = {}
for i in range(1,n+1):
dwa[str(i)] = 0
for i in range(m):
p,s = input().split()
if s == 'AC':
dac[p] = 1
if s == 'WA' and dac[p] == 0:
dwa[p] += 1
ac = 0
wa = 0
for k,v in dac.items():
ac += v
for k,v in dwa.items():
if dac[k] == 1:
wa += v
print(ac,wa) | 0 | null | 53,509,617,925,630 | 126 | 240 |
s = list(input())
q = int(input())
l1 = []
l2 = []
rev = False
for i in range(q):
inp = input()
if inp[0] == "1":
if rev:
rev = False
else:
rev = True
elif inp[2] == "1":
a = inp[4]
if rev:
l2.append(a)
else:
l1.append(a)
else:
a = inp[4]
if rev:
l1.append(a)
else:
l2.append(a)
l1.reverse()
ans = l1 + s + l2
if rev:
ans.reverse()
print("".join(ans)) | n = int(input())
s = input()
out = ""
for i in s:
if ord(i) + n > 90:
out += chr(64+n-(90-ord(i)))
else:
out += chr(ord(i)+n)
print(out) | 0 | null | 96,053,728,891,420 | 204 | 271 |
N = int(input())
X = list(map(int, input().split()))
a = int(sum(X) / N)
b = a + 1
A, B = 0, 0
for x in X:
A += (x - a) ** 2
B += (x - b) ** 2
ans = min(A, B)
print(ans) | import math
while True:
s = int(input())
if s == 0:
break
r = list(map(float, input().split()))
x = sum(r) / s
sigtwo = sum(list(map(lambda y: (y-x)**2, r))) / s
sig = math.sqrt(sigtwo)
print("{:.8f}".format(sig))
| 0 | null | 32,957,713,422,068 | 213 | 31 |
from collections import deque
s=deque(input())
q=int(input())
qq=[list(input().split()) for i in range(q)]
rl=1
fleft=deque()
fright=deque()
for i in qq:
if i[0]=='1':
rl*=-1
else:
if i[1]=='1':
if rl==1:
fleft.appendleft(i[2])
else:
fright.append(i[2])
else:
if rl==1:
fright.append(i[2])
else:
fleft.appendleft(i[2])
fleft.reverse()
s.extendleft(fleft)
s.extend(fright)
if rl==-1:
s.reverse()
print(''.join(s)) | from sys import exit
N,K = map(int,input().split())
town = [None]+list(map(int,input().split()))
flag = [None]+[0]*N
remain = K
piece = 1
cnt = 1
while remain > 0:
if flag[town[piece]] != 0:
q = cnt-flag[town[piece]]
piece = town[piece]
remain -= 1
break
else:
piece = town[piece]
flag[piece] = cnt
remain -= 1
cnt += 1
if remain == 0:
print(piece)
exit(0)
remain %= q
while remain > 0:
piece = town[piece]
remain -= 1
print(piece) | 0 | null | 40,172,873,409,280 | 204 | 150 |
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
total = sum(a)
flg = True
for i in range(m):
if a[i] * 4 * m < total:
flg = False
print('Yes') if flg else print('No') | #176-A Takoyaki
takoyaki = input().split()
totalTime = -(-int(takoyaki[0]) // int(takoyaki[1])) * int(takoyaki[2])
print(totalTime) | 0 | null | 21,312,095,652,508 | 179 | 86 |
import itertools
n=int(input())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
t=[int(i) for i in range(1,n+1)]
a=b=0
for i,j in enumerate(list(itertools.permutations(t,n))):
w=[1,1]
for x,y in enumerate(j):
if p[x]!=y: w[0]=0
if q[x]!=y: w[1]=0
if w[0]: a=i
if w[1]: b=i
print(abs(a-b)) | import sys
readline = sys.stdin.buffer.readline
N = int(readline())
P = list(map(int, readline().split()))
Q = list(map(int, readline().split()))
from itertools import permutations
p = list(permutations(range(1, N+1)))
# print(p)
a = p.index(tuple(P))
b = p.index(tuple(Q))
print(abs(a-b))
| 1 | 100,454,240,602,880 | null | 246 | 246 |
def insertion_sort(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 -= g
cnt += 1
A[j + g] = v
return cnt
def shell_sort(A, n):
cnt = 0
G = [1]
for i in range(1, 100):
# https://web.archive.org/web/20170212072405/http://www.programming-magic.com/20100507074241/
# 間隔
delta = 4 ** i + 3 * 2 ** (i - 1) + 1
if delta >= n:
break
G.append(delta)
G.reverse()
m = len(G)
for i in range(m):
cnt += insertion_sort(A, n, G[i])
return m, G, cnt
n = int(input())
A = [int(input()) for _ in range(n)]
m, G, cnt = shell_sort(A, n)
print(m)
print(*G)
print(cnt)
print(*A, sep='\n')
| import sys
n = int(input())
A = list(map(int,sys.stdin))
cnt = 0
G = [int((2.15**i-1)/1.15)for i in range(17,0,-1)]
G = [v for v in G if v <= n]
def insertionSort(A, n, g):
global cnt
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
for g in G:
insertionSort(A, n, g)
print(len(G))
print(*G)
print(cnt)
print('\n'.join(map(str,A)))
| 1 | 31,151,123,740 | null | 17 | 17 |
import itertools
N, M, Q = list(map(int, input().split()))
a = []
b = []
c = []
d = []
for i in range(Q):
ai, bi, ci, di = list(map(int, input().split()))
a.append(ai)
b.append(bi)
c.append(ci)
d.append(di)
ans = 0
for A in list(itertools.combinations_with_replacement(range(1, M+1), N)):
# print(A)
score = 0
for i in range(Q):
if A[b[i]-1] - A[a[i]-1] == c[i]:
score += d[i]
if ans < score:
ans = score
# print(A, score)
print(ans)
| from itertools import combinations_with_replacement
n,m,q=map(int,input().split())
A=list(range(1,m+1))
s=(list(combinations_with_replacement(A,n)))
a=[0]*q
b=[0]*q
c=[0]*q
d=[0]*q
ans=0
for i in range(q):
a[i],b[i],c[i],d[i]=map(int,input().split())
for i in s:
total=0
for j in range(q):
if i[b[j]-1]-i[a[j]-1]==c[j]:
total+=d[j]
ans=max(ans,total)
print(ans) | 1 | 27,424,037,405,860 | null | 160 | 160 |
n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if arr[i] != arr[j] and arr[i] != arr[k] and arr[j] != arr[k]:
if (arr[i] + arr[j]) > arr[k] and (arr[i] + arr[k]) > arr[j] and (arr[j] + arr[k]) > arr[i]:
ans += 1
print(ans)
| #!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys
INF = float('inf')
def solve(N: int, L: "List[int]"):
L.sort()
return sum(L[i] not in [L[j], L[k]] and L[j] != L[k] and L[i] + L[j] > L[k] for i in range(N) for j in range(i+1, N) for k in range(j+1, N))
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
L = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
print(f'{solve(N, L)}')
if __name__ == '__main__':
main()
| 1 | 5,069,889,751,230 | null | 91 | 91 |
def gcd(a,b):
while b:
a,b = b,a%b
return abs(a)
K=int(input())
#余裕でTLE?
ans=0
for a in range(1, K+1):
for b in range(1, K+1):
for c in range(1, K+1):
temp=a
temp=gcd(temp, b)
temp=gcd(temp, c)
ans+=temp
print(ans) | # coding: utf-8
# Your code here!
class UnionFind:
def __init__(self, size):
self.rank = [0 for i in range(size)]
self.parent = [-1 for i in range(size)]
self.children = [[i] for i in range(size)]
def Find(self, x):
parent = self.parent[x]
while parent >= 0:
x = parent
parent = self.parent[x]
return x
def Union(self, x, y):
root_x = self.Find(x)
root_y = self.Find(y)
if root_x == root_y:
return
else:
if self.rank[root_x] >= self.rank[root_y]:
self.parent[root_x] += self.parent[root_y]
self.parent[root_y] = root_x
self.rank[root_x] = max(self.rank[root_y] + 1, self.rank[root_x])
self.children[root_x] += self.children[root_y]
else:
self.parent[root_y] += self.parent[root_x]
self.parent[root_x] = root_y
self.rank[root_y] = max(self.rank[root_x] + 1, self.rank[root_y])
self.children[root_y] += self.children[root_x]
def Same(self, x, y):
return self.Find(x) == self.Find(y)
def FindRootAndSizeAndChildren(self):
return [(idx, -val, self.children[idx]) for idx, val in enumerate(self.parent) if val<0 ]
def print_lists(self):
print(self.rank)
print(self.parent)
print()
N, M = map(int, input().split())
unionfind = UnionFind(N)
for i in range(M):
a, b = map(int, input().split())
unionfind.Union(a-1, b-1)
max_size = 0
for idx, size, children in unionfind.FindRootAndSizeAndChildren():
if size > max_size:
max_size = size
print(max_size)
| 0 | null | 19,702,352,900,256 | 174 | 84 |
n, k = map(int, input().split())
positions = [int(i) for i in input().split()]
scores = [int(i) for i in input().split()]
if max(scores) <= 0:
print(max(scores))
else:
positions = [0] + positions
scores = [0] + scores
max_point = -pow(10, 9)
#print(max_point)
dic = {}
for i in range(1, n + 1):
f_pos = i
#print('f_pos',f_pos)
if f_pos not in dic:
point_loop = 0
loop = 0
pos_set = set([f_pos])
while True:
f_pos = positions[f_pos]
point_loop += scores[f_pos]
#print('pos', f_pos)
#print(point)
loop += 1
if f_pos in pos_set:
for j in pos_set:
dic[j] = [loop, point_loop]
break
pos_set.add(f_pos)
else:
loop, point_loop = dic[f_pos]
#print(loop, point)
loop_c = k // loop
r = k % loop
if loop_c == 0 or point_loop <= 0:
if point_loop <= 0:
if loop_c > 0:
r = loop
point = 0
for _ in range(r):
f_pos = positions[f_pos]
point += scores[f_pos]
max_point = max(max_point, point)
else:
point1, point2 = 0, 0
point1 = loop_c * point_loop
max_point1 = point1
for _ in range(r):
f_pos = positions[f_pos]
point1 += scores[f_pos]
max_point1 = max(point1, max_point1)
f_pos = i
point2 = (loop_c - 1) * point_loop
max_point2 = point2
for _ in range(loop):
f_pos = positions[f_pos]
point2 += scores[f_pos]
max_point2 = max(point2, max_point2)
max_point = max(max_point, max(max_point1, max_point2))
print(max_point)
| N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
P=[i-1 for i in P]
idle_max=-10**27
for s in range(0,N):
k=K-1
score=0
s=P[s]
score+=C[s]
count=1
idle_max=max(idle_max,score)
visited={s:[count,score]}
while k and not P[s] in visited:
k-=1
count+=1
s=P[s]
score+=C[s]
visited[s]=[count,score]
idle_max=max(idle_max,score)
#print(visited)
if k:
c1,score1=visited[s]
c2,score2=visited[P[s]]
c1+=1
s=P[s]
score1+=C[s]
score+=C[s]
k-=1
n=c1-c2
score+=(score1-score2)*(k//n)
k%=n
idle_max=max(idle_max,score)
while k:
k-=1
s=P[s]
score+=C[s]
idle_max=max(idle_max,score)
print(idle_max)
| 1 | 5,347,575,871,122 | null | 93 | 93 |
A, B = map(int, input().split())
lb_a = int(-(-A // 0.08))
ub_a = int(-(-(A + 1) // 0.08) - 1)
lb_b = int(-(-B // 0.1))
ub_b = int(-(-(B + 1) // 0.1) - 1)
if ub_b < lb_a or ub_a < lb_b:
print(-1)
else:
print(max(lb_a, lb_b))
| import math
a,b = map(int,input().split())
c = math.ceil
min8, max8 = c(a*12.5), c((a+1)*12.5)
min10, max10 = c(b*10), c((b+1)*10)
l8, l10 = list(range(min8, max8)), list(range(min10, max10))
s8, s10 = set(l8), set(l10)
ss = s8 & s10
#print(min8, max8, min10, max10)
print(min(ss) if len(ss) >0 else -1)
#print(238*0.08) | 1 | 56,475,105,851,880 | null | 203 | 203 |
class CircleInaRectangle:
def __init__(self, W, H, x, y, r):
if 0 <= x - r and 0 <= y - r and x + r <= W and y + r <= H:
print "Yes"
else:
print "No"
if __name__ == "__main__":
W, H, x, y, r = map(int, raw_input().split())
CircleInaRectangle(W, H, x, y, r) | w, h, x, y, r = map(int, input().split())
print("Yes" if r <= x <= w - r and r <= y <= h -r else "No") | 1 | 454,221,243,798 | null | 41 | 41 |
import sys
while True:
data = sys.stdin.readline().strip().split(' ')
h = int(data[0])
w = int(data[1])
if h == 0 and w == 0:
break
for i in range(h):
for j in range(w):
print('#', end='')
print('')
print('') |
N = int(input())
L = len(str(N))
if L == 1:
print(N)
quit()
pair = [[(10**(L-2)-1)//9]*9 for _ in range(9)]
for i in range(9):
pair[i][i] += 1
for i in range(10**(L-1), N+1):
pair[int(str(i)[0])-1][int(str(i)[L-1])-1] += 1 if i%10 else 0
ans = 0
for i in range(9):
for j in range(9):
ans += pair[i][j] * pair[j][i]
print(ans) | 0 | null | 43,532,710,293,952 | 49 | 234 |
a,b,d=map(int,input().split())
cnt=0
for i in range(a,b+1):
if(i%d==0):
cnt+=1
print(cnt) | my_range = int(input())
data_in = list(map(int,input().split()))
n_of_odd = 0
for ind in range(my_range):
if (ind % 2) == 0:
rem = data_in[ind] % 2
n_of_odd = n_of_odd + rem
print(n_of_odd) | 0 | null | 7,625,780,492,370 | 104 | 105 |
#!/usr/bin python3
# -*- coding: utf-8 -*-
def main():
mod = 10**9+7
K = int(input())
S = input()
Nk = len(S)
ret = 0
nCk = 1
pw25 = 1
pw26 = pow(26,K,mod)
inv26 = pow(26,mod-2,mod)
for i in range(K+1):
ret += nCk * pw25 * pw26
ret %= mod
nCk *= (i+Nk) * pow(i+1,mod-2,mod)
nCk %= mod
pw25 = (pw25*25)%mod
pw26 = (pw26*inv26)%mod
print(ret)
if __name__ == '__main__':
main()
| n,m=list(map(int,input().split(' ')))
am=[[0 for j in range(m)] for i in range(n)]
bm=[0 for l in range(m)]
cm=[0 for i in range(n)]
for i in range(n):
am[i]=list(map(int,input().split(' ')))
for j in range(m):
bm[j]=int(input())
for i in range(n):
for j in range(m):
cm[i]=cm[i]+am[i][j]*bm[j]
for i in range(n):
print(cm[i])
| 0 | null | 7,058,467,562,508 | 124 | 56 |
n = int(input())
a = sorted(map(int, input().split()), reverse=True)
ans, que, cnt = 0, [a[0]], 0
for i in a[1:]:
ans += que[cnt]
que += [i, i]
cnt += 1
print(ans)
| n = int(input())
lst = [int(i) for i in input().split()]
min_n = min(lst)
max_n = max(lst)
min_count = 1000000000
for i in range(min_n, max_n + 1):
count = 0
for j in range(n):
count += (lst[j] - i) ** 2
if count < min_count:
min_count = count
print(min_count)
| 0 | null | 37,097,330,471,832 | 111 | 213 |
class Dice:
def __init__(self):
self.u=1
self.w=2
self.s=3
self.e=4
self.n=5
self.d=6
self.dic={"W":0,"S":1,"E":2,"N":3}
def __init__(self,u,w,s,e,n,d):
self.u=u
self.w=w
self.s=s
self.e=e
self.n=n
self.d=d
self.dic={"W":0,"S":1,"E":2,"N":3}
def rot(self,way):
if isinstance(way,str):
way=self.dic[way]
if(way==0):
c=self.u
self.u=self.e
self.e=self.d
self.d=self.w
self.w=c
elif way==1:
c=self.u
self.u=self.n
self.n=self.d
self.d=self.s
self.s=c
elif way==2:
c=self.u
self.u=self.w
self.w=self.d
self.d=self.e
self.e=c
else :
c=self.u
self.u=self.s
self.s=self.d
self.d=self.n
self.n=c
u,s,e,w,n,d=map(int,input().split())
dice=Dice(u,w,s,e,n,d)
st=input()
for c in st:
dice.rot(c)
print(dice.u) |
class Dice:
def __init__(self,a,b,c,d,e,f):
self.face1 = a
self.face2 = b
self.face3 = c
self.face4 = d
self.face5 = e
self.face6 = f
def above_face(self):
return self.face1
def roll(self, order):
if order == 'N':
tmp = self.face1
self.face1 = self.face2
self.face2 = self.face6
self.face6 = self.face5
self.face5 = tmp
elif order == 'S':
tmp = self.face1
self.face1 = self.face5
self.face5 = self.face6
self.face6 = self.face2
self.face2 = tmp
elif order == 'W':
tmp = self.face1
self.face1 = self.face3
self.face3 = self.face6
self.face6 = self.face4
self.face4 = tmp
else:
tmp = self.face1
self.face1 = self.face4
self.face4 = self.face6
self.face6 = self.face3
self.face3 = tmp
a,b,c,d,e,f = map(int,input().split())
dice1 = Dice(a,b,c,d,e,f)
l_order = list(input())
for i in range(len(l_order)):
dice1.roll(l_order[i])
print(dice1.above_face())
| 1 | 233,221,478,082 | null | 33 | 33 |
n, m = map(int, input().split())
if m > 0:
p, s = zip(* [input().split() for _ in range(m)])
else:
p = []
s = []
ac_dict = {str(i): False for i in range(1, n + 1)}
wa_dict = {str(i): 0 for i in range(1, n + 1)}
ac_num = 0
wa_num = 0
for p_i, s_i in zip(p, s):
if ac_dict[p_i] == False:
if s_i == 'AC':
ac_num += 1
wa_num += wa_dict[p_i]
ac_dict[p_i] = True
elif s_i == 'WA':
wa_dict[p_i] += 1
print('{} {}'.format(ac_num, wa_num))
| a, b, c = list(map(int, input().split()))
k = int(input())
for i in range(k+1) :
if a >= b :
b *= 2
elif b >= c :
c *= 2
else :
print("Yes")
exit()
print("No")
| 0 | null | 50,410,598,082,500 | 240 | 101 |
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=998244353
N,M,K=MI()
def cmb(n, r, mod):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return (fact[n] * factinv[r] * factinv[n-r])%mod
fact=[1,1]
factinv=[1,1]
inv=[0,1]
for i in range(2, N + 1):
fact.append((fact[-1] * i) % mod)
inv.append((-inv[mod % i] * (mod // i)) % mod)
factinv.append((factinv[-1] * inv[-1]) % mod)
p=[1]*(N+3)#(M-1)^i
for i in range(N+2):
p[i+1]=(p[i]*(M-1))%mod
ans=0
for i in range(K+1):
temp=M*p[N-1-i]*cmb(N-1,i,mod)
temp%=mod
ans=(ans+temp)%mod
print(ans)
main()
| num1 = int(input())
print(num1 **3)
| 0 | null | 11,629,514,302,560 | 151 | 35 |
def med(l):
t = len(l)
if t%2:
return l[t//2]
else: return (l[t//2]+l[t//2-1])/2
n = int(input())
a = []
b = []
for i in range(n):
x,y = map(int,input().split())
a+=[x]
b+=[y]
a.sort()
b.sort()
if n%2==0:
print(int((med(b)-med(a))*2)+1)
else:
print(med(b)-med(a)+1)
| import sys
readline = sys.stdin.readline
N = int(readline())
A = []
B = []
for _ in range(N):
a, b = map(int, readline().split())
A.append(a)
B.append(b)
A.sort()
B.sort()
if N % 2 == 0:
mi = A[N//2-1]+A[N//2]
ma = B[N//2-1]+B[N//2]
else:
mi = A[N//2]
ma = B[N//2]
print(ma-mi+1)
| 1 | 17,344,101,039,370 | null | 137 | 137 |
x = list(map(int, input().split()))
h = list(map(int, input().split()))
count = 0
for i in h:
if x[1] <= i:
count += 1
print(count) | 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)
| 0 | null | 89,362,110,693,720 | 298 | 21 |
def comb_mod(n,r):
mod = 10**9+7
ans = 1
for i in range(r):
ans *= n-i
ans %= mod
for i in range(1,r+1):
ans *= pow(i,-1,mod)
ans %= mod
return ans
def solve():
n, a, b = map(int, input().split())
mod = 10**9+7
ans = pow(2,n,mod)-comb_mod(n,a)-comb_mod(n,b)-1
ans %= mod
return ans
print(solve()) | import collections
def resolve():
n = int(input())
s = [input() for _ in range(n)]
c = collections.Counter(s)
max_count = c.most_common()[0][1]
ans = [i[0] for i in c.items() if i[1]==max_count]
for i in sorted(ans): print(i)
resolve() | 0 | null | 68,188,775,712,960 | 214 | 218 |
import sys
input = sys.stdin.readline
import numpy as np
from numba import njit
def read():
D = int(input().strip())
C = np.fromstring(input().strip(), dtype=np.int32, sep=" ")
S = np.empty((D, 26), dtype=np.int32)
for i in range(D):
s = np.fromstring(input().strip(), dtype=np.int32, sep=" ")
S[i, :] = s[:]
M = 1000
RD = np.random.randint(D, size=(M*2, ), dtype=np.int32)
RQ = np.random.randint(26, size=(M*2, ), dtype=np.int32)
DQ = np.stack([RD, RQ]).T
return D, C, S, M, DQ
@njit
def diff_satisfaction(C, S, d, p, last):
"""d日目にコンテストpを開催するときの、満足度の更新量を求める
"""
v = 0
for i in range(26):
v -= C[i] * (d - last[i])
v += C[p] * (d - last[p])
v += S[d, p]
return v
@njit
def change_schedule(D, C, S, T, d, q, cumsat):
"""d日目のコンテストをqに変更する
"""
p = T[d]
dp1, dq1 = -1, -1
dp3, dq3 = D, D
for i in range(0, d):
if T[i] == p:
dp1 = i
if T[i] == q:
dq1 = i
for i in range(D-1, d, -1):
if T[i] == p:
dp3 = i
if T[i] == q:
dq3 = i
cumsat = cumsat - S[d, p] + S[d, q] - C[p] * (dp3-d) * (d-dp1) + C[q] * (dq3-d) * (d-dq1)
return cumsat
@njit
def greedy(D, C, S):
T = np.zeros(D, dtype=np.int32)
last = -np.ones(26, dtype=np.int32)
cumsat = 0
for d in range(D):
max_p = 0
max_diff = -999999999
# select contest greedily
for p in range(26):
diff = diff_satisfaction(C, S, d, p, last)
if diff > max_diff:
max_p = p
max_diff = diff
# update schedule
cumsat += max_diff
T[d] = max_p
last[max_p] = d
return cumsat, T
@njit
def solve(D, C, S, M, DQ):
cumsat, T = greedy(D, C, S)
for i in range(M):
d1, q1 = DQ[i*2, :]
d2, q2 = DQ[i*2+1, :]
newsat = cumsat
newsat = change_schedule(D, C, S, T, d1, q1, newsat)
t1 = T[d1]
T[d1] = q1
newsat = change_schedule(D, C, S, T, d2, q2, newsat)
t2 = T[d2]
T[d2] = q2
if newsat > cumsat:
cumsat = newsat
else:
T[d2] = t2
T[d1] = t1
for t in T:
print(t+1)
if __name__ == '__main__':
inputs = read()
outputs = solve(*inputs)
if outputs is not None:
print("%s" % str(outputs))
| # i>j とする.
# |i-j|=Ai+Aj
# i-j = Ai+Aj
N=int(input())
A=list(map(int,input().split()))
cnt=[0]*300000
ans=0
for i in range(N):
if i-A[i]>=0:
ans=ans+cnt[i-A[i]]
if i+A[i]<len(cnt):
cnt[i+A[i]]=cnt[i+A[i]]+1
print(ans)
| 0 | null | 17,910,179,300,408 | 113 | 157 |
import sys
for x in sys.stdin:
if "?" in x:
break
print(eval(x.replace("/", "//"))) | N, M = map(int, input().split())
to = [[] for i in range(100010)]
for i in range(M):
a, b = map(int, input().split())
to[a].append(b)
to[b].append(a)
dist = [0] * (N+1)
q = [1]
dist[0] = -1
#print(to[:4])
dist = [0] * N
pre = [0] * N
while len(q) != 0:
a = q.pop(0)
for i in to[a]:
if dist[i-1] == 0:
dist[i-1] = dist[a-1] + 1
pre[i-1] = a
q.append(i)
#print(q)
print("Yes")
for i in range(1, N):
print(pre[i])
| 0 | null | 10,636,077,823,122 | 47 | 145 |
#coding:utf-8
data = [int(i) for i in input().split()]
a = data[0]
b = data[1]
if a == b:
print("a == b")
elif a > b:
print("a > b")
else:
print("a < b") | from collections import defaultdict
h, w, m = map(int, input().split())
targets = defaultdict(int)
targets_count_yoko = defaultdict(int)
targets_count_tate = defaultdict(int)
for _ in range(m):
y, x = map(int, input().split())
y -= 1
x -= 1
targets_count_yoko[x] += 1
targets_count_tate[y] += 1
targets[(y, x)] = -1
max_row = max(targets_count_yoko.values())
max_line = max(targets_count_tate.values())
y_idx = defaultdict(bool)
x_idx = defaultdict(bool)
max_count_x = 0
max_count_y = 0
for i in range(w):
if targets_count_yoko[i] == max_row:
x_idx[i] = True
max_count_x += 1
for i in range(h):
if targets_count_tate[i] == max_line:
y_idx[i] = True
max_count_y += 1
ans = max_line + max_row
kumi = max_count_x*max_count_y
for key_y, key_x in targets.keys():
if y_idx[key_y] and x_idx[key_x]:
kumi -= 1
if kumi == 0:
break
if kumi == 0:
print(ans-1)
else:
print(ans)
| 0 | null | 2,559,293,210,738 | 38 | 89 |
print('ABC' if input() == 'ARC' else 'ARC')
| print(input()[1] == "B" and "ARC" or "ABC") | 1 | 24,284,888,632,158 | null | 153 | 153 |
# coding: utf-8
a, b = input().split()
aaa = ""
bbb = ""
for i in range(int(b)):
aaa += a
for i in range(int(a)):
bbb += b
if aaa > bbb:
print(bbb)
else:
print(aaa) | first=0
second=0
third=0
for i in range(10):
mountain=int(input())
if mountain>first:
third=second
second=first
first=mountain
elif mountain>second:
third=second
second=mountain
elif mountain>third:
third=mountain
print(first)
print(second)
print(third) | 0 | null | 42,184,177,231,152 | 232 | 2 |
s = input()
ans = 'Yes'
if s[2] != s[3]:
ans = 'No'
if s[4] != s[5]:
ans = 'No'
print(ans) | from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
d = inp()
c = inpl()
s = [inpl() for _ in range(d)]
res = 0
last = [0]* 26
for day in range(d):
t = inp(); t -= 1
res += s[day][t]
for i in range(26):
last[i] += 1
if i == t:
last[t] = 0
res -= c[i] * last[i]
print(res) | 0 | null | 26,073,203,639,340 | 184 | 114 |
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
ans=1
mod=10**9+7
C=[0,0,0]
for i in range(n):
cur=A[i]
ct=0
Chk=True
for i,c in enumerate(C):
if cur==c:
ct+=1
if Chk:
C[i]+=1
Chk=False
ans=(ans*ct)%mod
print(ans)
|
n = int(input())
AC,WA,TLE,RE = 0,0,0,0
for x in range(n):
s = input()
if s == 'AC':
AC = AC + 1
elif s == 'WA':
WA = WA + 1
elif s == 'TLE':
TLE = TLE + 1
elif s == 'RE':
RE = RE + 1
print('AC x ' + str(AC))
print('WA x ' + str(WA))
print('TLE x ' + str(TLE))
print('RE x ' + str(RE))
| 0 | null | 69,641,277,663,462 | 268 | 109 |
while True:
card=input()
if card=='-': break
loop=int(input())
for i in range(loop):
ch=int(input())
card=card[ch:]+card[:ch]
print(card) | # 回答を読んで作成
# 全体の半分に到達可能、ただし1行もしくは1列の時は動けない
h,w = map(int, input().split())
if h==1 or w==1:
ans = 1
else:
ch = (h*w)/2
ans = (ch//1)+((h*w)%2)
print(int(ans)) | 0 | null | 26,371,060,983,642 | 66 | 196 |
n = int(input())
m = 10**9+7
a = 10 ** n
b = 9 ** n
c = 9 ** n
d = 8 ** n
ans = (a-(b+c-d)) % m
print(ans) | S = list(input())
if S[2]==S[3]:
if S[4] == S[5]:
print("Yes")
else:
print("No")
else:
print("No") | 0 | null | 22,667,586,784,348 | 78 | 184 |
import sys
dice = [int(i) for i in sys.stdin.readline().split()]
command = sys.stdin.readline().strip()
for c in command:
if c == "N":
dice = (dice[1], dice[5], dice[2], dice[3], dice[0], dice[4])
elif c == "S":
dice = (dice[4], dice[0], dice[2], dice[3], dice[5], dice[1])
elif c == "W":
dice = (dice[2], dice[1], dice[5], dice[0], dice[4], dice[3])
elif c == "E":
dice = (dice[3], dice[1], dice[0], dice[5], dice[4], dice[2])
print(dice[0]) | n=int(input())
a=list(map(int,input().split()))
count=1
chk=False
for i in range(1,n+1):
if a[i-1]==count:
chk=True
count+=1
print(n-count+1 if chk else -1) | 0 | null | 57,374,416,221,806 | 33 | 257 |
s = int(input())
if s%1000 == 0:
c=0
else:
c = 1000 - ( s%1000 )
print(c)
| x = int(input())
print((1000 - x % 1000) % 1000) | 1 | 8,410,537,088,188 | null | 108 | 108 |
h,w,k=map(int,input().split())
G=[["."]*w for i in range(h)]
for i in range(h):
G[i]=list(input())
ans=[[0]*w for i in range(h)]
GG=[[0] for i in range(h)]
for i in range(h):
for j in range(w):
if G[i][j]=="#":
GG[i].append(j)
GG[i].append(w)
B=[0]
a=1
for i in range(h):
if len(GG[i])==2:
continue
B.append(i)
ans[i][GG[i][0]:GG[i][2]]=[a]*(GG[i][2]-GG[i][0])
a=a+1
for j in range(len(GG[i])-3):
ans[i][GG[i][j+2]:GG[i][j+3]]=[a]*(GG[i][j+3]-GG[i][j+2])
a=a+1
B.append(h)
for i in range(B[2]-B[0]):
ans[i]=ans[B[1]]
for i in range(B[2],h):
if i not in B:
ans[i]=ans[i-1]
for i in range(h):
for j in range(w):
print(ans[i][j],end=" ")
print() | H, W, K = map(int, input().split())
a = [input() for _ in range(H)]
ans = [[-1 for _ in range(W)] for _ in range(H)]
i_cut_time = 0
j_cut_time = 0
for i in range(H):
if '#' in a[i]:
i_cut_time += 1
i_cut = []
start = 0
goal = -1
for i in range(H):
if len(i_cut) == i_cut_time-1:
break
if '#' in a[i]:
goal = i
i_cut.append([start, goal, i])
start = goal+1
for i in range(start, H):
if '#' in a[i]:
i_cut.append([start, H-1, i])
# print(i_cut)
cnt = 1
for s, g, p in i_cut:
j_cut = []
jstart = 0
jgoal = -1
for i in range(W):
if len(j_cut) == a[p].count('#')-1:
break
if a[p][i] == '#':
jgoal = i
j_cut.append([jstart, jgoal])
jstart = jgoal+1
j_cut.append([jstart, W-1])
# print(s, g, p, j_cut)
# for i in range(s, g+1):
for js, jg in j_cut:
for j in range(js, jg+1):
for i in range(s, g+1):
# print(i, j, cnt)
ans[i][j] = cnt
cnt += 1
# print(*ans, sep='\n')
for i in range(H):
print(*ans[i]) | 1 | 143,458,954,009,822 | null | 277 | 277 |
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
from collections import deque
p = sorted(p)[a-x:]
q = sorted(q)[b-y:]
t = deque(sorted(p+q))
r = deque(reversed(sorted(r)))
# print(t,r)
while r:
if t[0] < r[0]:
t.popleft()
w = r.popleft()
t.append(w)
else:
break
ans = sum(t)
print(ans) | from collections import Counter
X,Y,A,B,C=map(int,input().split())
#3logN
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort()
q.sort()
p_list=p[-X:]
q_list=q[-Y:]
r.extend(p_list)
r.extend(q_list)
r.sort()
print(sum(r[-(X+Y):])) | 1 | 44,689,535,736,140 | null | 188 | 188 |
N,M=input().split()
N=int(N); M=int(M);
if(N==M):
print("Yes")
else:
print("No") | def main():
N = list(map(int, list(input())))
N = N[::-1] + [0]
ans = 0
for i in range(len(N)-1):
if N[i] < 5:
ans += N[i]
elif N[i] > 5:
ans += 10 - N[i]
N[i+1] += 1
else:
ans += 5
if N[i+1] >= 5:
N[i+1] += 1
print(ans + N[len(N)-1])
main() | 0 | null | 77,200,993,807,858 | 231 | 219 |
def gacha():
# 入力
N = int(input())
S = [input() for _ in range(N)]
# 重複排除
freebie_tuple = set(S)
# 景品の種類
return len(freebie_tuple)
result = gacha()
print(result) | n = int(input())
ans = set()
for _ in range(n):
ans.add(input())
print(len(ans)) | 1 | 30,437,801,246,232 | null | 165 | 165 |
s = input()
print("{}".format(s+"es" if s[-1] == "s" else s+"s")) | from sys import stdin
a, b = (int(n) for n in stdin.readline().rstrip().split())
sign = "==" if a == b else ">" if a > b else "<"
print("a {} b".format(sign))
| 0 | null | 1,392,180,855,780 | 71 | 38 |
#!/usr/bin/env python3
YES = "Yes" # type: str
NO = "No" # type: str
def solve(A: int, B: int, C: int, D: int):
while True:
# 高橋
C -= B
if C <= 0:
return YES
# 青木
A -= D
if A <= 0:
return NO
def main():
A, B, C, D = map(int, input().split())
answer = solve(A, B, C, D)
print(answer)
if __name__ == "__main__":
main()
| s=input()
if len(s)%2!=0:
print("No")
exit()
for i,c in enumerate(s):
if (i%2==0 and c=="h") or (i%2==1 and c=="i"):
continue
else:
print("No")
exit()
print("Yes") | 0 | null | 41,609,011,315,350 | 164 | 199 |
n = int(input())
p = list(map(int,input().split()))
m = n
q = 2 * (10 ** 5)
for i in range(n):
q = min(p[i],q)
if not p[i] <= q:
m -= 1
print(m) | W,H,x,y,r=map(int,input().split())
print('Yes'if(False if x<r or W-x<r else False if y<r or H-y<r else True)else'No')
| 0 | null | 42,736,596,931,028 | 233 | 41 |
import math
res = 100
N = int(input())
for i in range(N):
res = math.ceil(res * 1.05)
print("%d000" % res)
| import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
# sys.stdout.write(str(n)+"\n")
MODULO = 1000000007
# INF = 1e18+10
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
n = int(input())
a_b = [() for i in range(n)]
m_b_a = [() for i in range(n)]
# a_b_value_indices = {}
# m_b_a_value_indices = {}
a_b_value_count = {}
m_b_a_value_count = {}
a_b_value_rep = {}
m_b_a_value_rep = {}
used = [False for _ in range(n)]
zeroes = 0
is_zero = [False for _ in range(n)]
for i in range(n):
a, b = map(int, input().split())
if a == 0 and b == 0:
zeroes += 1
used[i] = True
is_zero[i] = True
else:
if a == 0:
a_b[i] = (0, 1)
m_b_a[i] = (1, 0)
elif b == 0:
a_b[i] = (1, 0)
m_b_a[i] = (0, 1)
else:
neg = False
if a*b < 0:
neg = True
d = gcd(abs(a), abs(b))
a = abs(a) // d
b = abs(b) // d
if neg:
a_b[i] = (-a, b)
m_b_a[i] = (b, a)
else:
a_b[i] = (a, b)
m_b_a[i] = (-b, a)
a_b_value_count[a_b[i]] = a_b_value_count.get(a_b[i], 0) + 1
# a_b_value_indices[a_b[i]] = a_b_value_indices.get(a_b[i], []) + [i]
m_b_a_value_count[m_b_a[i]] = m_b_a_value_count.get(m_b_a[i], 0) + 1
# m_b_a_value_indices[m_b_a[i]] = m_b_a_value_indices.get(m_b_a[i], []) + [i]
if a_b[i] not in a_b_value_rep:
a_b_value_rep[a_b[i]] = i
if m_b_a[i] not in m_b_a_value_rep:
m_b_a_value_rep[m_b_a[i]] = i
ans = 1
for i in range(n):
if not is_zero[i] and not used[a_b_value_rep[a_b[i]]]:
# if not connected
if a_b[i] not in m_b_a_value_count:
ans *= pow(2, a_b_value_count[a_b[i]], MODULO)
ans %= MODULO
used[a_b_value_rep[a_b[i]]] = True
# for j in a_b_value_indices[a_b[i]]:
# used[j] = True
else:
s = a_b_value_count[a_b[i]]
t = m_b_a_value_count[a_b[i]]
ans *= ((1*pow(2, s, MODULO) + 1*pow(2, t, MODULO) -1) % MODULO)
ans %= MODULO
used[a_b_value_rep[a_b[i]]] = True
used[m_b_a_value_rep[a_b[i]]] = True
# for j in m_b_a_value_indices[a_b[i]]:
# used[j] = True
# used_a_b_keys.add(a_b_key)
# used_a_b_keys.add(-1/a_b_key)
# -1 is for empty
print((ans-1+zeroes) % MODULO) | 0 | null | 10,466,732,115,740 | 6 | 146 |
while True:
a, b = map(int, input().split())
if(a == 0 and b == 0):
break
for i in range(a):
for j in range(b):
if(0 < i < a-1 and 0 < j < b-1):
print(".", end = "")
else:
print("#", end = "")
print()
print()
| while True:
H,W = map(int,raw_input().split())
if (H,W) == (0,0):
break
for k in range(H):
if k==0 or k==H-1:
print "#"*W
else:
print "#" + "." * (W-2) + "#"
print "" | 1 | 811,963,519,922 | null | 50 | 50 |
N=int(input())
A=list(map(int,input().split()))
L={}
R={}
ans=0
for i in range(N):
if i+A[i] in L:
L[i+A[i]]+=1
else:
L[i+A[i]]=1
if A[i]<=i:
if i-A[i] in R:
R[i-A[i]]+=1
else:
R[i-A[i]]=1
for r in R:
if r in L:
ans+=L[r]*R[r]
print(ans) | n = int(input())
A = list(map(int, input().split()))
freq = [0] * (max(A)+1)
for a in A:
freq[a] += 1
total = 0
for f in freq:
total += f*(f-1)//2
for a in A:
print (total - max(freq[a]-1, 0)) | 0 | null | 36,869,140,204,380 | 157 | 192 |
# coding=utf-8
class Dice(object):
def __init__(self, label):
self.label = label
def rotateS(self):
s1, s2, s3, s4, s5, s6 = self.label
self.label = [s5, s1, s3, s4, s6, s2]
def rotateN(self):
s1, s2, s3, s4, s5, s6 = self.label
self.label = [s2, s6, s3, s4, s1, s5]
def rotateE(self):
s1, s2, s3, s4, s5, s6 = self.label
self.label = [s4, s2, s1, s6, s5, s3]
def rotateW(self):
s1, s2, s3, s4, s5, s6 = self.label
self.label = [s3, s2, s6, s1, s5, s4]
def rotate(self, r):
if r == 'S':
self.rotateS()
elif r == 'N':
self.rotateN()
elif r == 'E':
self.rotateE()
elif r == 'W':
self.rotateW()
else:
print 'Cannot rotate into ' + r + 'direction.'
def getTopLabel(self):
return self.label[0]
if __name__ == '__main__':
d = Dice(map(int, raw_input().split()))
rotations = raw_input()
for r in rotations:
d.rotate(r)
print d.getTopLabel() | import collections
def resolve():
n = int(input())
a = tuple(map(int,input().split()))
A = collections.Counter(a)
cnt_0 = 0
for i in A.values():
cnt_0 += int(i*(i-1)/2)
for i in a:
cnt_1 = A[i]-1
print(cnt_0-cnt_1)
resolve() | 0 | null | 23,918,600,115,452 | 33 | 192 |
#!/usr/bin/env python3
from pprint import pprint
import sys
sys.setrecursionlimit(10 ** 6)
H, W, M = map(int, input().split())
num_bombs_each_row = [0] * H
num_bombs_each_col = [0] * W
bombs = set()
for _ in range(M):
h, w = map(int, input().split())
# 0-index
h -= 1
w -= 1
bombs.add((h, w))
num_bombs_each_row[h] += 1
num_bombs_each_col[w] += 1
max_bombs_in_row = max(num_bombs_each_row)
max_bombs_in_col = max(num_bombs_each_col)
rows_with_max_bombs = []
for row in range(H):
if num_bombs_each_row[row] == max_bombs_in_row:
rows_with_max_bombs.append(row)
cols_with_max_bombs = []
for col in range(W):
if num_bombs_each_col[col] == max_bombs_in_col:
cols_with_max_bombs.append(col)
found = False
for row in rows_with_max_bombs:
for col in cols_with_max_bombs:
if (row, col) not in bombs:
found = True
break
ans = max_bombs_in_row + max_bombs_in_col
if found:
print(ans)
else:
print(ans - 1)
| H, W, M = map(int, input().split())
h = [0 for _ in range(H+1)]
w = [0 for _ in range(W+1)]
hmax = 0
wmax = 0
Q = []
for _ in range(M):
a, b = map(int, input().split())
Q.append((a, b))
h[a] += 1
hmax = max(hmax, h[a])
w[b] += 1
wmax = max(wmax, w[b])
h_ok = False
w_ok = False
hm = [False for _ in range(H+1)]
wm = [False for _ in range(W+1)]
h_cnt = 0
w_cnt = 0
hw_cnt = 0
for a, b in Q:
if not hm[a] and h[a] == hmax:
hm[a] = True
h_cnt += 1
if not wm[b] and w[b] == wmax:
wm[b] = True
w_cnt += 1
if h[a] == hmax and w[b] == wmax:
hw_cnt += 1
if h_cnt*w_cnt - hw_cnt > 0:
print(hmax+wmax)
else:
print(hmax+wmax-1)
| 1 | 4,723,182,777,250 | null | 89 | 89 |
n, a, b = map(int, input().split())
set_num = n // (a + b)
remain_num = n - (a + b) * set_num
print(set_num * a + min(remain_num, a))
| N, A, B = map(int, input().split())
r = (N//(A+B))*A
N = N%(A+B)
print(r+N if N<=A else r+A)
| 1 | 55,647,286,594,592 | null | 202 | 202 |
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N,S = MI()
A = [0] + LI()
mod = 998244353
dp = [[0]*(S+1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(1,N+1):
for j in range(S+1):
if j >= A[i]:
dp[i][j] = 2*dp[i-1][j] + dp[i-1][j-A[i]]
dp[i][j] %= mod
else:
dp[i][j] = 2*dp[i-1][j]
dp[i][j] %= mod
print(dp[-1][-1])
| #!/usr/bin/python
# coding: utf-8
def maze(string, h):
return string[h:] + string[:h]
while True:
string = raw_input()
if string == "-":
break
m = int(raw_input())
for i in range(m):
h = int(raw_input())
string = maze(string, h)
#print "h: {}, string: {}".format(h, string)
print string | 0 | null | 9,777,333,335,000 | 138 | 66 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def solve(p, t, A, result):
if A[p] == t:
result[(p,t)] = True
return True
if p == len(A) - 1:
return False
if (p,t) in result:
# print('Cache Hit')
return result[(p,t)]
if solve(p+1, t-A[p], A, result):
result[(p,t)] = True
return True
if solve(p+1, t, A, result):
result[(p,t)] = True
return True
result[(p,t)] = False
return False
import sys
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
num_q = int(sys.stdin.readline())
query = list(map(int, sys.stdin.readline().split()))
result = dict()
for m in query:
if solve(0, m, A, result):
print('yes')
else:
print('no')
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
def main():
while True:
try:
num = eval(input().replace(" ","+"))
except:
return
else:
print(len(str(num)))
if __name__ == '__main__':
main() | 0 | null | 47,063,866,148 | 25 | 3 |
N, K = map(int, input().split())
H = list(map(int, input().split()))
if N <= K:
print(0)
exit()
H.sort(reverse=True)
print(sum(H[K:])) |
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
N, K = read_ints()
H = read_ints()
if K >= N:
return 0
H.sort(reverse=True)
return sum(H[K:])
if __name__ == '__main__':
print(solve())
| 1 | 78,941,693,297,152 | null | 227 | 227 |
while True:
a,b=map(int,input().split())
if (a==0 and b==0):
break
if a>b:
swap=a
a=b
b=swap
print('%d %d'%(a,b))
| x = 1
y = 1
while x != 0 or y != 0:
n = map(int,raw_input().split())
x = n[0]
y = n[1]
if x == 0 and y == 0:
break
elif x <= y:
print x,y
elif x > y:
print y,x | 1 | 511,759,695,320 | null | 43 | 43 |
N, K = map(int, input().split())
d = [0 for i in range(K)]
A = []
for i in range(K):
d[i] = int(input())
A.append(list(map(int, input().split())))
candy = [0 for i in range(N)]
for i in range(K):
for j in range(d[i]):
candy[A[i][j]-1] += 1
print(candy.count(0)) | # -*- 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
"""
| 0 | null | 38,200,083,684,068 | 154 | 197 |
# coding=utf-8
if __name__ == '__main__':
N, M = map(int, input().split())
li = list(map(int, input().split()))
lisum = sum(li)
cnt = 0
for i in range(N):
if lisum / (M * 4) <= li[i]:
cnt += 1
if M <= cnt:
print('Yes')
else:
print('No')
| n, m = list(map(int, input().split()))
nums = list(map(int, input().split()))
nums.sort(reverse=True)
ss = sum(nums)
if nums[m-1] * (4 * m) < ss:
print('No')
else:
print('Yes')
| 1 | 38,858,624,958,860 | null | 179 | 179 |
h1, m1, h2, m2, k = map(int, input().split())
h = h2 - h1
m = m2 - m1
m += 60 * h
print(m - k)
| import os
import sys
from collections import defaultdict
adj = defaultdict(list)
col = defaultdict(int)
sys.setrecursionlimit(1000000)
mx=0
edges = {}
def find_color(u , p):
global mx , now
cnt =0
for i in adj[u]:
cnt = cnt+1
if i==p:
continue
find_color(i , u)
mx = max(mx, cnt)
now =0
def dfs(u , p, prev):
global mx, now
for i in adj[u]:
if i==p:
continue
if now == prev:
now = (now+1)%mx
col[edges[i,u]]=now
now =(now+1)%mx
for i in adj[u]:
if i==p:
continue
dfs(i , u, col[edges[i, u]])
def main():
n = int(input())
for i in range(n-1):
x, y = map(int , input().split())
adj[x].append((y))
adj[y].append((x))
edges[x,y]=i
edges[y,x]=i
find_color(1, 0)
dfs(1, 0,-1)
print(mx)
for i in range(n-1):
print(col[i]+1)
if __name__ == "__main__":
main()
| 0 | null | 77,113,593,191,150 | 139 | 272 |
import sys
input = sys.stdin.readline
a = input()
n = int(a)
s = [[0] * 9 for _ in range(9)]
for i in range(1, n+1):
j = str(i)
k = int(j[0])
l = int(j[-1])
if l != 0:
s[k-1][l-1] += 1
ans = 0
#print(s)
for i in range(1, 10):
for j in range(1, 10):
ans += s[i-1][j-1] * s[j-1][i-1]
print(ans)
| N = 26
D = int(input())
c_list = list(map(int, input().split()))
s_table = []
for _ in range(D):
s_table.append(list(map(int, input().split())))
data = []
for _ in range(D):
data.append(int(input()) - 1)
def calc(data):
last = [-1] * N
satisfaction = 0
for i in range(D):
j = data[i]
satisfaction += s_table[i][j]
last[j] = i
for k in range(N):
satisfaction -= c_list[k] * (i - last[k])
print(satisfaction)
return satisfaction
calc(data)
| 0 | null | 48,058,142,896,540 | 234 | 114 |
# C Many Requirements
from itertools import combinations_with_replacement as com
N, M, Q = map(int, input().split())
C = []
for q in range(Q):
C.append(tuple(map(int, input().split())))
As = com([i for i in range(1, M+1)], N)
ans = 0
for A in As:
point = 0
for Ci in C:
if A[Ci[1]-1] - A[Ci[0]-1] == Ci[2]:
point += Ci[3]
ans = max(ans, point)
print(ans) | import math
H, W = map(int, input().split(' '))
if H == 1 or W == 1:
print(1)
exit()
res = math.ceil((H*W)/2)
print(res)
| 0 | null | 39,300,250,352,082 | 160 | 196 |
import sys
n = int(raw_input())
#n = 30
for i in range(1, n+1):
if i % 3 == 0:
sys.stdout.write(" {:}".format(i))
elif str(i).find('3') > -1:
sys.stdout.write(" {:}".format(i))
print("") | n = int(raw_input())
out = ""
for i in range(1, n + 1):
if (i % 3 == 0) or (i % 10 == 3):
out += " " + str(i)
continue
if "3" in str(i):
out += " " + str(i)
print out | 1 | 921,995,096,100 | null | 52 | 52 |
import math
def sieve_of_erastosthenes(num):
is_prime = [True for i in range(num+1)]
is_prime[0] = False
is_prime[1] = False
for i in range(2, int(num**0.5) + 1):
if not is_prime[i]:
continue
for j in range(i * 2, num + 1, i):
is_prime[j] = False
return [i for i in range(num + 1) if is_prime[i]]
N = int(input())
A = [int(i) for i in input().split()]
ans = math.gcd(A[0], A[1])
tmp = math.gcd(A[0], A[1])
for i in range(2, N):
ans = math.gcd(ans, A[i])
if ans > 1:
print('not coprime')
exit()
maxA = max(A)
l = sieve_of_erastosthenes(maxA)
B = [False for i in range(maxA+1)]
for i in range(N):
B[A[i]] = True
flag = False
for each in l:
tmp = each
counter = 0
while True:
if B[tmp]:
counter += 1
tmp += each
if tmp > maxA:
break
if counter > 1:
flag = True
break
if flag:
print('setwise coprime')
else:
print('pairwise coprime') | N = int(input())
A = [int(x) for x in input().split()]
# M以下の整数の素因数分解を高速に
# 前処理O(MloglogM)
# 一計算O(logK)
def mae_syori(M):
D = [0] * (M + 1)
for i in range(2, M+1):
if D[i] != 0: continue
# print(i, list(range(i*2, M + 1, i)))
for j in range(i*2, M+1, i):
if D[j] == 0:
D[j] = i
return D
def p_bunkai(K):
assert 2 <= K <= len(D)-1
k = K
ret = []
while True:
if k == 1:
break
if D[k] == 0:
ret.append((k, 1))
break
else:
p = D[k]
count = 0
while k % p == 0:
count += 1
k //= p
ret.append((p, count))
return ret
# 最大公約数
# ユークリッドの互除法
def my_gcd(a, b):
if b == 0:
return a
else:
return my_gcd(b, a%b)
c_gcd = A[0]
for a in A:
c_gcd = my_gcd(c_gcd, a)
if c_gcd == 1:
max_A = max(A)
D = mae_syori(max_A)
yakusu = set()
ok = True
for a in A:
if a == 1: continue
p_bunkai_result = p_bunkai(a)
p_list = [p[0] for p in p_bunkai_result]
for p in p_list:
if p not in yakusu:
yakusu.add(p)
else:
ok = False
if not ok:
break
if ok:
print('pairwise coprime')
else:
print('setwise coprime')
else:
print('not coprime') | 1 | 4,090,979,465,056 | null | 85 | 85 |
MOD = 10**9+7
MAX = 1000010
finv = [0] * MAX
inv = [0] * MAX
def COMinit():
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2, MAX):
inv[i] = MOD - inv[MOD%i] * (MOD//i) % MOD
finv[i] = finv[i-1] * inv[i] % MOD
def COM(n, k):
res = 1
for i in range(k):
res = res * (n-i) % MOD
return res * finv[k] % MOD
x, y = map(int, input().split())
if (x+y)%3 != 0:
ans = 0
else:
s, t = (-x+2*y)//3, (2*x-y)//3
if s < 0 or t < 0:
ans = 0
else:
COMinit()
ans = COM((s+t), s)
print(ans) | import sys
def input(): return sys.stdin.readline().rstrip()
def main():
mod = 10**9+7
x, y = map(int, input().split())
x2 = max(x, y)
y2 = min(x, y)
m = y2-(x2-y2)
if m < 0 or m % 3 != 0:
print(0)
else:
c = m // 3
n = c*2+(x2-y2)
bunshi = 1
bunbo = 1
for i in range(1, c+1):
bunbo= bunbo*i % mod
bunshi= bunshi*(n-i+1) % mod
ans = bunshi * pow(bunbo,-1,mod)
print(ans%mod)
if __name__ == '__main__':
main() | 1 | 150,490,001,416,560 | null | 281 | 281 |
def main():
_ = int(input())
num_tuple = tuple(map(int, input().split()))
count = 0
for i, num in enumerate(num_tuple):
if ((i+1)*num) % 2 == 1:
count += 1
print(count)
if __name__ == '__main__':
main()
| x=int(input())
li=list(map(int, input().split()))
res=0
i=0
while i<x:
if li[i]%2!=0:
res+=1
i+=2
print(res) | 1 | 7,745,938,858,736 | null | 105 | 105 |
def main():
n = list(input())
k = int(input())
N = len(n)
for i in range(N):
n[i] = int(n[i])
dp1 = [[0 for _ in range(k+1)] for _ in range(N+1)]
dp2 = [[0 for _ in range(k+1)] for _ in range(N+1)]
dp1[0][0] = 1
for i in range(N):
for j in range(k+1):
dp2[i+1][j] += dp2[i][j]
if j < k:
dp2[i+1][j+1] += dp2[i][j]*9
if n[i]:
dp2[i+1][j] += dp1[i][j]
if j == k:
continue
dp1[i+1][j+1] += dp1[i][j]
dp2[i+1][j+1] += dp1[i][j] * (n[i]-1)
else:
dp1[i+1][j] += dp1[i][j]
print(dp1[N][k]+dp2[N][k])
if __name__ == "__main__":
main() | X = int(input())
N = X // 100
for n in range(N + 1):
M = X - n * 100
if M > n * 5:
continue
else:
print(1)
break
else:
print(0) | 0 | null | 101,777,103,965,488 | 224 | 266 |
S=input()
Q=int(input())
p=0
u=0
l=""
r=""
for i in range(Q):
q=list(input().split())
if q[0]=="1":
u+=1
else:
if q[1]=="1" and u%2==0 or q[1]=="2" and u%2==1:
l=l+q[2]
else:
r=r+q[2]
if u%2==0:
l=l[::-1]
S=l+S+r
else:
S=S[::-1]
r=r[::-1]
S=r+S+l
print(S)
| data = []
while True:
in_line = raw_input().split()
m = int(in_line[0])
f = int(in_line[1])
r = int(in_line[2])
if m == -1 and f == -1 and r == -1:
break
if m == -1 or f == -1:
data.append("F")
elif m + f >= 80:
data.append("A")
elif m + f >= 65:
data.append("B")
elif m + f >= 50:
data.append("C")
elif m + f >= 30:
if r >= 50:
data.append("C")
else:
data.append("D")
else:
data.append("F")
for n in data:
print n | 0 | null | 29,304,573,089,470 | 204 | 57 |
def divisor(n):
res = []
i = 1
while i*i <= n:
if not n % i:
res.append(i)
if (i*i != n): res.append(n//i)
i += 1
return res
N = int(input())
ans = 0
for d in divisor(N):
if d == 1: continue
n = N
while not n % d:
n //= d
if n%d == 1: ans += 1
print(ans + len(divisor(N-1))-1) |
N = int(input())
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
# divisors.sort()
return divisors
ans = len(make_divisors(N-1)) - 1
#print(ans)
divisors = make_divisors(N)
def dfs(x,N):
while N % x == 0:
N //= x
# print(N)
N %= x
# print(N)
return N == 1
counta = 0
for i in range(1,len(divisors)):
if dfs(divisors[i],N):
counta += 1
#print(counta)
ans += counta
print(ans) | 1 | 41,625,521,300,868 | null | 183 | 183 |
while True:
temp = input()
if temp == '-':
break
else:
cards = temp
for i in range(int(input())):
h = int(input())
cards = cards[h:] + cards[:h]
print(cards) | X = int(input())
num = X//100
amari = X%100
if num*5 >= amari:
print(1)
else:
print(0) | 0 | null | 64,227,194,326,592 | 66 | 266 |
import math
def resolve():
import sys
input = sys.stdin.readline
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
abm = [int(x) for x in input().rstrip().split(" ")]
a_list = [int(x) for x in input().rstrip().split(" ")]
b_list = [int(x) for x in input().rstrip().split(" ")]
# 最小値
ans = min(a_list) + min(b_list)
for i in range(abm[2]):
xyc = [int(x) for x in input().rstrip().split(" ")]
price = a_list[xyc[0] - 1] + b_list[xyc[1] - 1] - xyc[2]
if price < ans:
ans = price
print(ans)
if __name__ == "__main__":
resolve()
| A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = min(a) + min(b)
for i in range(M):
x, y, c = map(int, input().split())
p = a[x-1] + b[y-1] - c
if p < ans:
ans = p
print(ans) | 1 | 54,048,623,464,122 | null | 200 | 200 |
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')
N = int(input())
A = list(map(int, input().split()))
import math
# 葉から調査し、範囲を求める
limits = [(A[-1], A[-1])]
# for i in reversed(range(0, len(A)-1)):
for i in reversed(range(0, N)):
min_ = math.ceil(limits[-1][0] / 2)
max_ = limits[-1][1]
# print("min_ {} max_ {} leaf {}".format(min_, max_, A[i]))
limits.append((min_ + A[i], max_ + A[i]))
# print("limits: {}".format(limits))
if limits[-1][0] != 1:
print(-1)
quit()
# 根からにする
limits.reverse()
# 根から制約に基づき最大値を計算する
tree = [1]
for i in range(1, N+1):
nodes = min(2 * (tree[-1] - A[i-1]), limits[i][1])
tree.append(nodes)
# print(limits)
# print(tree)
ans = sum(tree)
print(ans) | n = int(input())
a = list(map(int, input().split()))
s = sum(a)
l = [1]
for i in range(n + 1):
s -= a[i]
m = min(s, (l[i] - a[i]) * 2)
if m < 0:
print(-1)
exit()
l.append(m)
print(sum(l)) | 1 | 18,857,410,686,240 | null | 141 | 141 |
n,m = map(int, input().split())
a = [[] for x in range(0, n)]
b = [0 for x in range(0, m)]
for x in range(0, n):
a[x] = list(map(int, input().split()))
for x in range(0, m):
b[x] = int(input())
for i in range(0, n):
c = 0
for j in range(0, m):
c += a[i][j] * b[j]
print(c) | matrix = []
vector = []
n, m = [int(x) for x in input().split()]
for i in range(n):
matrix.append([int(x) for x in input().split()])
for i in range(m):
vector.append(int(input()))
for i in range(n):
sum = 0
for j in range(m):
sum += matrix[i][j] * vector[j]
print(sum) | 1 | 1,150,276,105,440 | null | 56 | 56 |
N, K = map(int, input().split())
arr = list(map(int, input().split()))
jj = 0
koho=[]
for i in range(K):
jj+=arr[i]
koho.append(jj)
for i in range(N-K):
jj+=arr[i+K]
jj-=arr[i]
koho.append(jj)
a=max(koho)
print(a/2+(K/2))
| def abc154d_dice_in_line():
n, k = map(int, input().split())
p = list(map(lambda x: (int(x)+1)*(int(x)/2)/int(x), input().split()))
ans = sum(p[0:k])
val = ans
for i in range(k, len(p)):
val = val - p[i-k] + p[i]
ans = max(ans, val)
print(ans)
abc154d_dice_in_line() | 1 | 75,163,884,399,088 | null | 223 | 223 |
int = int(input())
print(int*int) | def resolve():
import math
n, m = list(map(int, input().split()))
ans1 = math.factorial(n) // (math.factorial(n-2) * 2) if n > 1 else 0
ans2 = math.factorial(m) // (math.factorial(m-2) * 2) if m > 1 else 0
print(ans1 + ans2)
resolve() | 0 | null | 95,880,466,371,438 | 278 | 189 |
a = input()
b = input()
n = len(a)
count = 0
for i in range(n):
if a[i] != b[i]:
count = count +1
print(count) | N = int(input())
primes = {}
for num in range(2, int(N**0.5)+1):
while N%num == 0:
if num in primes:
primes[num] += 1
else:
primes[num] = 1
N //= num
ans = 0
for p in primes.values():
n = 1
while n*(n+1)//2 <= p:
n += 1
ans += n-1
if N > 1:
ans += 1
print(ans) | 0 | null | 13,610,978,281,420 | 116 | 136 |
n = int(input())
D = list(map(int, input().split()))
sum = 0
for i in range(n):
for j in range(n):
if i != j:
sum += D[i] * D[j]
print(sum // 2) | string = input()
h = 0
A = [h]
for c in string:
if c == "/": h += 1
elif c == "\\": h -= 1
A.append(h)
flg = False
ans1 = []
ans2 = []
forward = True
while len(A) > 1:
h = 0
B = [0]
for i in range(len(A) - 1):
if not flg: B = [0]
d = A[i + 1] - A[i]
if d < 0: flg = True
h += d
if h <= 0:
B.append(h)
if h == 0:
if len(B) > 2:
if forward: ans1.append(abs(sum(B)))
else: ans2.append(abs(sum(B)))
B = []
flg = False
else:
h = 0
A = B.copy()
A.reverse()
forward = (forward + 1) % 2
ans2.reverse()
ans = ans1 + ans2
print(sum(ans))
print(len(ans), *ans)
| 0 | null | 84,571,366,823,228 | 292 | 21 |
import sys
input = sys.stdin.readline
N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())),reverse = 1)
print('Yes' if A[M-1]>=sum(A)/(4*M) else 'No')
| n,m=map(int, input().split())
a_list=[int(i) for i in input().split()]
a_list.sort(reverse=True)
#print(a_list)
standard=sum(a_list)/(4*m)
#print(a_list[m-1])
#print(standard)
if a_list[m-1]<standard:
print("No")
else:
print("Yes")
| 1 | 38,521,379,885,000 | null | 179 | 179 |
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
t1,t2 = map(int, input().split())
a1,a2 = map(int, input().split())
b1,b2 = map(int, input().split())
a1 *= t1
a2 *= t2
b1 *= t1
b2 *= t2
if a1+a2==b1+b2:
ans = "infinity"
else:
if a1+a2 < b1+b2:
a1,a2,b1,b2 = b1,b2,a1,a2
# a1+a2が大きい
if a1>b1:
ans = 0
else:
ans = ((b1-a1) // (a1+a2-b1-b2)) * 2 + 1
if (b1-a1) % (a1+a2-b1-b2) ==0:
ans -= 1
print(ans) | from sys import stdin
input = stdin.readline
a,b,n = map(int,input().split())
maxpoint = 0
if b > n :
tmp = int((a * n) / b) - a * int(n/b)
maxpoint = max(tmp,maxpoint)
else:
k = int(n/b)*b -1
maxpoint = int((a * k) / b) - a * int(k/b)
if b == 1:
maxpoint = 0
print(maxpoint) | 0 | null | 79,722,800,289,230 | 269 | 161 |
import numpy as np
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n, t = map(int, input().split())
dp = np.zeros(t,dtype=int)
food = []
for _ in range(n):
a, b = map(int, input().split())
food.append([a, b])
food.sort(key=lambda x: x[0]*-1)
for j in range(n):
a, b = food[j][0], food[j][1]
a=min(a,t)
dptmp=np.zeros(t,dtype=int)
dptmp[a:] = np.maximum(dp[a:],dp[:-a]+b)
dptmp[:a] = np.maximum(np.full(a,b,dtype=int), dp[:a])
dp=dptmp
print(dp[-1])
if __name__ == '__main__':
main() | import sys
readline = sys.stdin.buffer.readline
n,t = map(int,readline().split())
dp = [0]*(t+1)
AB = [list(map(int,readline().split())) for i in range(n)]
AB.sort(reverse=True)
for i in range(n):
a,b = AB[i]
for j in range(t):
if j+a > t:
dp[j] = max(dp[j],b)
else:
dp[j] = max(dp[j],dp[j+a]+b)
print(dp[0]) | 1 | 152,067,468,663,228 | null | 282 | 282 |
n = str(input())
flag = 0
for i in range(3):
if n[i] == '7':
flag = 1
if flag == 1:
print('Yes')
else:
print('No') | flag = False
for i in input():
if i == '7':
print('Yes')
flag = True
break
if not flag:
print('No') | 1 | 34,319,074,910,858 | null | 172 | 172 |
x = int(input())
print(1 if x-x//100*100 <= x//100*5 else 0) | S,T = input().split()
a,b = map(int,input().split())
u = input()
if u == S:
a -= 1
elif u ==T:
b -= 1
print("{} {}".format(a,b)) | 0 | null | 99,657,715,950,026 | 266 | 220 |
def insertionSort(A, g, count):
N = len(A)
for i in range(g, N):
j = i - g
temp = A[i]
while j >= 0 and A[j] > temp:
A[j + g] = A[j]
j -= g
count += 1
A[j + g] = temp
return A, count
def shellSort(A):
n = len(A)
count = 0
h = 1
G = []
while (True):
if h > n:
break
G.append(h)
h = 3 * h + 1
m = len(G)
G.reverse()
for i in range(m):
A, count = insertionSort(A, G[i], count)
return m, G, count, A
def listFromInput():
N = int(input())
numbers = []
for i in range(N):
numbers.append(int(input()))
return numbers
A = listFromInput()
m, G, count, A = shellSort(A)
print(m)
print(" ".join(list(map(str, G))))
print(count)
for i in A:
print(i) | n=int(input())
p=list(map(int,input().split()))
min_val=10**18
ans=0
for i in range(n):
if i==0:
min_val=p[i]
ans+=1
continue
if min_val>=p[i]:
min_val=p[i]
ans+=1
print(ans)
| 0 | null | 42,966,271,210,692 | 17 | 233 |
from itertools import starmap
(lambda *_: None)(
*map(print,
(lambda board, pairs: starmap(board, pairs))(
(lambda h, w:
(lambda rows: ''.join(map(lambda i: rows[i%2], range(h))))(
(lambda row: (row[:w]+'\n', row[1:w+1]+'\n'))(
'#.'*(w//2+1)))),
map(lambda t: map(int, t),
map(str.split, iter(input, '0 0')))))) | from collections import Counter
c=Counter(list(map(int,input().split())))
if len(c)==2:print('Yes')
else:print('No') | 0 | null | 34,573,760,856,098 | 51 | 216 |
n, k = map(int, input().split())
count = [0] * n
for _ in range(k):
kosu = int(input())
pos = list(map(int, input().split()))
for i in pos:
count[i-1] +=1
ans = [True if i == 0 else False for i in count]
print(sum(ans)) | n = int(input())
a = set(map(int, input().split()))
print('YES' if len(a) == n else 'NO') | 0 | null | 49,491,146,676,468 | 154 | 222 |
n=int(input())
a=("")
for _ in range(n):
a=a+"ACL"
print(a) | N = int(input())
A_list = list(map(int, input().split()))
if A_list[0] > 1:
print(-1)
exit()
elif A_list[0] == 1:
if N == 0:
print(1)
else:
print(-1)
exit()
sumA = sum(A_list)
node = 1
count = 1
for i in range(len(A_list) - 1):
temp = min([node * 2, sumA])
count += temp
node = temp - A_list[i + 1]
if node < 0:
print(-1)
exit()
sumA -= A_list[i + 1]
print(count) | 0 | null | 10,511,760,197,680 | 69 | 141 |
s= input().strip()
print(s.replace("?","D",len(s))) | k,n=map(int,input().split())
A=sorted(list(map(int,input().split())))
A.append(A[0]+k)
a=A[0]
l=[]
for i in A:
l.append(i-a)
a=i
print(k-max(l)) | 0 | null | 31,024,376,631,830 | 140 | 186 |
A, V = map(int, input().split())
B, W = map(int, input().split())
T = int(input())
if B > A:
A = A + V*T
B = B + W*T
if A >= B:
print('YES')
else:
print('NO')
else:
A = A - V*T
B = B - W*T
if A <= B:
print('YES')
else:
print('NO') | A, V = map(int, input().split())
B, W = map(int, input().split())
T = int(input())
print("YES" if (V-W)*T >= abs(B-A) else "NO") | 1 | 15,117,990,050,908 | null | 131 | 131 |
n = int(input())
tp = ((1,1),(1,-1))
for i in range(200):
for j in range(200):
for k in tp:
a,b = i*k[0],j*k[1]
if((a**5 - b**5)==n):
print(a,b)
exit() | x=int(input())
for i in range(-118,120):
for j in range(-118,120) :
if i**5-j**5==x :
print(i,j)
exit()
else :
continue | 1 | 25,614,323,675,812 | null | 156 | 156 |
#!/usr/bin/env python
if __name__=='__main__':
a, b, c = map(int, raw_input().split())
print 'Yes' if a < b and b < c else 'No'
| (a,b,c) = input().rstrip().split()
a = int(a)
b = int(b)
c = int(c)
if a < b < c:
print("Yes")
else:
print("No") | 1 | 387,006,283,200 | null | 39 | 39 |
count = 0
N = int(input())
P = "".join(list(map(str, input().split())))
Q = "".join(list(map(str, input().split())))
ans = []
def dfs(curr, p, q, n):
if len(curr) == n:
global count
count+=1
if curr == p:
ans.append(count)
if curr == q:
ans.append(count)
return
for i in range(1, n+1):
if str(i) not in curr:
dfs(curr + str(i), p, q, n)
for i in range(1, N + 1):
dfs(str(i), P, Q, N)
print(abs(ans[0]-ans[1]))
| #!/usr/bin/env python3
import sys
import itertools
def solve(N: int, P: "List[int]", Q: "List[int]"):
l = []
for i in range(N):
l.append(i+1)
p = 0
q = 0
c = 0
for i in itertools.permutations(l, N):
c += 1
t = list(i)
if t == P:
p = c
if t == Q:
q = c
if p != 0 and q != 0:
print(abs(q - p))
break
return
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
P = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
Q = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
solve(N, P, Q)
if __name__ == '__main__':
main()
| 1 | 100,715,891,955,328 | null | 246 | 246 |
N = int(input())
k, l = divmod(N, 2)
if l == 1:
print(0)
else:
cnt = 0
while k > 1:
cnt += k // 5
k //= 5
print(cnt)
| n = int(input())
if n%2 ==1:
print(0)
exit()
cnt = 0
i = 1
if n%2 == 0:
while n//(2*(5**i)) > 0:
cnt += n//(2*(5**i))
i +=1
print(cnt) | 1 | 116,209,450,306,712 | null | 258 | 258 |
class Dice:
dice = {'N':2, 'E':4, 'S':5, 'W':3}
currTop = 1
def top(self):
return self.currTop
def rot(self, direction):
newTop = self.dice[direction]
currTop = self.currTop
self.currTop = newTop
if direction == 'N':
self.dice['N'] = 7 - currTop
self.dice['S'] = currTop
elif direction == 'S':
self.dice['N'] = currTop
self.dice['S'] = 7 - currTop
elif direction == 'E':
self.dice['E'] = 7 - currTop
self.dice['W'] = currTop
elif direction == 'W':
self.dice['E'] = currTop
self.dice['W'] = 7 - currTop
faces = list(map(int, input().split()))
cmd = input()
d = Dice()
for c in cmd:
d.rot(c)
print(faces[d.top()-1]) | s = ""
while True:
try:
s += input().lower()
except:
break
for c in [chr(i) for i in range(ord('a'), ord('z') + 1)]:
print(c, ":", s.count(c)) | 0 | null | 958,584,561,440 | 33 | 63 |
from operator import itemgetter
from bisect import bisect_right
n, d, a = map(int, input().split())
monster = [tuple(map(int, input().split())) for _ in range(n)]
monster.sort(key=itemgetter(0))
xx = [xi for xi, hi in monster]
ans = 0
imos = [0] * (n + 1)
for i, (xi, hi) in enumerate(monster):
hi -= imos[i]
if hi > 0:
c = (hi + a - 1) // a
ans += c
idx = bisect_right(xx, xi + 2 * d)
imos[i] += c * a
imos[idx] -= c * a
# update imos
imos[i + 1] += imos[i]
print(ans)
| import math
N=int(input())
A=list(map(int,input().split()))
a=max(A)
def era(n):
prime=[]
furui=list(range(2,n+1))
while furui[0]<math.sqrt(n):
prime.append(furui[0])
furui=[i for i in furui if i%furui[0]!=0]
return prime+furui
b=A[0]
for i in range(1,N):
b=math.gcd(b,A[i])
if b!=1:
print("not coprime")
exit()
furui=era(10**6+7)
minfac=list(range(10**6+7))
for i in furui:
for j in range(i,(10**6+7)//i):
if minfac[i*j]==i*j:
minfac[i*j]=i
dummy=[False]*(10**6+7)
for i in range(N):
k=A[i]
while k!=1:
if dummy[minfac[k]]==True:
print("setwise coprime")
exit()
else:
dummy[minfac[k]]=True
f=minfac[k]
while k%f==0:
k//=f
print("pairwise coprime") | 0 | null | 43,338,466,512,742 | 230 | 85 |
H,W,N = map(int, open(0).read().split())
ans = 0
while N > 0:
A = max(H, W)
tmp = N // A
N = N % A
if tmp == 0 and N:
N = 0
ans += 1
break
if H == A:
W -= tmp
else:
H -= tmp
ans += tmp
print(ans) | dic = {}
while True:
try:
line = raw_input()
except:
break
for i in line.lower():
if i in dic:
dic[i] += 1
else:
dic[i] = 1
for i in xrange(97, 123):
output = 0
if chr(i) in dic:
output = dic[chr(i)]
print "%s : %d" % (chr(i), output) | 0 | null | 45,375,214,585,792 | 236 | 63 |
operand = input().split()
stack = []
for op in operand:
if op.isdigit():
stack.append(op)
else:
val1 = stack.pop()
val2 = stack.pop()
stack.append(str(eval("("+val2+")" + op + "("+val1+")")))
print (stack[0]) | s=input()
t=input()
n=len(s)
if s==t[:n] and len(t)==n+1:
print('Yes')
else:
print('No') | 0 | null | 10,666,778,698,970 | 18 | 147 |
s = input()
if s[0] != s[1] or s[1] != s[2]:
print("Yes")
else:
print("No") | N = int(input())
A = input().split()
num_even = 0
num_approved = 0
for i in range(N):
if int(A[i]) % 2 == 0:
num_even += 1
if int(A[i]) % 3 == 0 or int(A[i]) % 5 == 0:
num_approved += 1
if num_even == num_approved:
print("APPROVED")
else:
print("DENIED")
| 0 | null | 61,691,166,213,080 | 201 | 217 |
C = input()
i = ord(C)
print(chr(i+1)) | print('abcdefghijklmnopqrstuvwxyz'['abcdefghijklmnopqrstuvwxyz'.index(input())+1]) | 1 | 91,961,683,126,048 | null | 239 | 239 |
while True:
r = list(map(int, input().split()))
if r[0] == -1 and r[1] == -1 and r[2] == -1:
break
elif r[0] == -1 or r[1] == -1:
print("F")
elif r[0] + r[1] >= 80:
print("A")
elif r[0] + r[1] >= 65 and r[0] + r[1] < 80:
print("B")
elif r[0] + r[1] >= 50 and r[0] + r[1] < 65:
print("C")
elif r[0] + r[1] >= 30 and r[0] + r[1] < 50:
if r[2] >= 50:
print("C")
else:
print("D")
else:
print("F") | N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[1]
C=["#"]*N
roop=False
for i in range(K):
if C[B[i]-1]=="#":
C[B[i]-1]="$"
else:
roop=True
break
B.append(A[B[i]-1])
if roop==True:
f=B.index(B[i])
T=i-f
if T==0:
print(B[f])
else:
r=(K-f)%T
print(B[f+r])
else:
print(B[K])
| 0 | null | 12,016,731,711,142 | 57 | 150 |
def INT():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(map(int, input().split()))
S = input()
if S[len(S) - 1] == "s":
S += "es"
else:
S += "s"
print(S) | #
# 179A
#
s = input()
s1 = list(s)
if s1[len(s)-1]=='s':
print(s+'es')
else:
print(s+'s') | 1 | 2,383,114,789,510 | null | 71 | 71 |
import math
R = int(input())
pi = math.pi
ans = 2*R * pi
print(ans) | import math
a,b,h,m=map(int,input().split())
lrad=6*m
srad=30*h+0.5*m
if abs(lrad-srad)<=180:
do=abs(lrad-srad)
else:
do=360-abs(lrad-srad)
rad=math.radians(do)
print(math.sqrt(a**2+b**2-2*a*b*math.cos(rad))) | 0 | null | 25,736,766,878,592 | 167 | 144 |