code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
178,025B
⌀ | question_pair_id
float64 27.1M
177,113B
⌀ | code1_group
int64 1
297
| code2_group
int64 1
297
|
---|---|---|---|---|---|---|
# def main():
# n, m = map(int, input().split())
# ans = [0] * n
# count = [0] * n
# for i in range(m):
# s, c = map(int, input().split())
# if s == 1 and c == 0 and n != 1:
# print(-1)
# exit()
# if count[s - 1] == 0 or ans[s - 1] == c:
# ans[s - 1] = c
# count[s - 1] += 1
# else:
# print(-1)
# exit()
# if ans[0] == 0 and n != 1:
# ans[0] = 1
# print(''.join(map(str, ans)))
# if __name__ == '__main__':
# main()
def main():
n, m = map(int, input().split())
C = [-1] * n
flag = 0
for i in range(m):
s, c = map(int, input().split())
if C[s - 1] == -1 or C[s - 1] == c:
C[s - 1] = c
else:
flag = 1
if flag or (C[0] == 0 and n != 1):
print(-1)
return
else:
C = [0 if i == -1 else i for i in C]
if C[0] == 0 and n != 1:
C[0] = 1
print(''.join(list(map(str, C))))
if __name__ == '__main__':
main()
|
N,M=map(int, input().split())
SC=[list(map(int, input().split())) for _ in range(M)]
num=[-1]*N
for sc in SC:
s,c=sc[0],sc[1]
if s==1 and c==0 and N>1:
print(-1)
exit()
elif num[s-1]==c or num[s-1]==-1:
num[s-1]=c
else:
print(-1)
exit()
if N==3:
if num[0]==-1:
num[0]=1
if num[1]==-1:
num[1]=0
if num[2]==-1:
num[2]=0
print(num[0]*100+num[1]*10+num[2])
elif N==2:
if num[0]==-1:
num[0]=1
if num[1]==-1:
num[1]=0
print(num[0]*10+num[1])
else:
if num[0]==-1:
num[0]=0
print(num[0])
| 1 | 60,918,075,463,680 | null | 208 | 208 |
def solve(string):
n, d, *xy = map(int, string.split())
return str(sum([1 if x**2 + y**2 <= d**2 else 0 for x, y in zip(*[iter(xy)] * 2)]))
if __name__ == '__main__':
import sys
print(solve(sys.stdin.read().strip()))
|
a = input()
if a.isupper():
print('A')
else:
print('a')
| 0 | null | 8,655,782,110,560 | 96 | 119 |
#AOJ ITP1_1_C
N = input().split()
area = 0
perimeter = 0
length=int(N[0])
breadth=int(N[1])
area = length * breadth
perimeter = (length + breadth) * 2
print(str(area)+" "+str(perimeter) )
|
import sys
prm = sys.stdin.readline()
prm = prm.split()
a = int(prm[0])
b = int(prm[1])
c = int(prm[2])
if a < b and b < c :
print 'Yes'
else :
print 'No'
| 0 | null | 348,409,002,592 | 36 | 39 |
N = int(input())
total = 0
for num in range(1, N+1):
if num % 15 == 0:
continue
if num % 3 == 0 or num % 5 == 0:
continue
total += num
print(total)
|
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
answer = 0
b_ind = 0
for x in range(N-1):
A[x+1] += A[x]
if A[x+1] > K:
A = A[:x+1]
break
for y in range(M-1):
B[y+1] += B[y]
if B[y+1] > K:
B = B[:y+1]
break
A = [0] + A
B = [0] + B
na = len(A)
nb = len(B)
if A[-1] + B[-1] <= K:
answer = len(A) + len(B) - 2
print(answer)
else:
for i in range(na):
for j in range(nb-b_ind-1, -1, -1):
if A[i] + B[j] <= K:
if answer < i+j:
answer = i+j
b_ind = nb-j-1
break
print(answer)
| 0 | null | 22,964,855,595,520 | 173 | 117 |
n, m, x = map(int, input().split())
c = [0] * n
a = [[0] * m for _ in range(n)]
for i in range(n):
tmp = list(map(int, input().split()))
c[i] = tmp[0]
a[i] = tmp[1:]
INF = 10 ** 9
ans = INF
for i in range(1 << n):
cost = 0
under = [0] * m
for j in range(n):
if not i >> j & 1: continue
cost += c[j]
for k in range(m):
under[k] += a[j][k]
ok = True
for k in range(m):
if under[k] < x: ok = False
if ok:
ans = min(ans, cost)
if ans == INF: print("-1")
else: print(ans)
|
n,m,x=map(int,input().split())
c=[list(map(int,input().split())) for _ in range(n)]
ans=float('inf')
for i in range(2**n):
data=[0]*(m+1)
for j in range(n):
if i&(1<<j):
for k in range(m+1):
data[k]+=c[j][k]
cnt=0
for l in range(1,m+1):
if data[l]>=x:
cnt+=1
if cnt==m:
ans=min(ans,data[0])
if ans==float('inf'):
print(-1)
exit()
print(ans)
| 1 | 22,289,769,306,980 | null | 149 | 149 |
S = input()
hi = []
for i in range(5):
hi.append("hi"*(i+1))
print("Yes" if S in hi else "No")
|
h,a=map(int,input().split())
if h%a!=0:
n=int(h/a)+1
print(n)
else:
n=int(h/a)
print(n)
| 0 | null | 65,002,692,432,488 | 199 | 225 |
H = int(input())
W = int(input())
N = int(input())
print(-(-N//max(H,W)))
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
mia = sorted([i for i in a if i < 0], reverse=True)
pla = sorted([i for i in a if i >= 0])
ans, num = 1, []
if len(pla) == 0 and k % 2 == 1:
for i in mia[:k]:
ans = ans * i % mod
print(ans)
exit()
for _ in range(k):
if (len(mia) * len(pla) > 0 and abs(mia[-1]) <= pla[-1]) or len(mia) == 0:
tmp = pla.pop()
elif (len(mia) * len(pla) > 0 and abs(mia[-1]) > pla[-1]) or len(pla) == 0:
tmp = mia.pop()
num.append(tmp)
cnt = sum(i < 0 for i in num)
for i in num:
ans = ans * i % mod
if cnt % 2 == 1:
p, q, r, s = 1, 0, -1, 0
if len(mia) > 0 and cnt != k:
p, q = min(i for i in num if i >= 0), mia[-1]
if len(pla) > 0:
r, s = max(i for i in num if i < 0), pla[-1]
if len(mia) > 0 or (len(pla) > 0 and cnt != k):
if q * r >= p * s:
ans *= q * pow(p, mod - 2, mod)
if q * r < p * s:
ans *= s * pow(r, mod - 2, mod)
ans %= mod
print(ans)
| 0 | null | 49,196,672,551,312 | 236 | 112 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, K = map(int, read().split())
X = list(map(int, str(N)))
L = len(X)
dp1 = [[0] * (K + 1) for _ in range(L + 1)]
dp2 = [[0] * (K + 1) for _ in range(L + 1)]
dp1[0][0] = 1
for i, x in enumerate(X):
for j in range(K + 1):
if x != 0:
if j > 0:
dp1[i + 1][j] = dp1[i][j - 1]
else:
dp1[i + 1][j] = dp1[i][j]
if j > 0:
dp2[i + 1][j] = dp2[i][j - 1] * 9
if x != 0:
dp2[i + 1][j] += dp1[i][j - 1] * (x - 1)
dp2[i + 1][j] += dp2[i][j]
if x != 0:
dp2[i + 1][j] += dp1[i][j]
print(dp1[L][K] + dp2[L][K])
return
if __name__ == '__main__':
main()
|
def binary_search(border, b):
ok = b
ng = n
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if L[mid] < border:
ok = mid
else:
ng = mid
return ok
n = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for a in range(0,n-1):
for b in range(a+1, n):
a_b = L[a] + L[b]
ans += binary_search(a_b, b) - b
print(ans)
| 0 | null | 123,790,032,890,258 | 224 | 294 |
import sys
n=int(input())
d=[]
cnt=0
for i in range(n):
D=[int(x) for x in input().split()]
d.append(D)
for d1,d2 in d:
if d1==d2:
cnt+=1
else:
cnt=0
if 3<=cnt:
print("Yes")
sys.exit()
print("No")
|
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
counter = 0
for d1, d2 in C:
if d1 == d2:
counter += 1
else:
counter = 0
if counter == 3:
print('Yes')
exit()
print('No')
| 1 | 2,506,858,818,048 | null | 72 | 72 |
N = int(input())
A = list(map(int, input().split()))
sum_value = 0
for i in range(len(A)-1):
if A[i] >= A[i+1]:
x = A[i] - A[i+1]
A[i+1] = x + A[i+1]
sum_value += x
print(sum_value)
|
n = int(input())
a = list(map(int,input().split()))
x = 0
total = 0
for i in range(n-1):
if a[i] >= a[i+1]:
x = a[i] - a[i+1]
total += x
a[i+1] = a[i]
i = i+1
else:
total += 0
print(total)
| 1 | 4,583,274,817,280 | null | 88 | 88 |
from pprint import pprint
n,m=map(int,input().split())
c=list(map(int,input().split()))
dp=[[10**5]*(n+1) for _ in range(m+1)]
dp[0][0]=0
for i in range(m):
for j in range(n+1):
if c[i]>j:
dp[i+1][j]=dp[i][j]
else:
dp[i+1][j]=min(dp[i][j],dp[i][j-c[i]]+1,dp[i+1][j-c[i]]+1)
# pprint(dp)
print(dp[m][n])
|
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
cs = list(map(int, input().split()))
f = dict() #k?????§??????????????????????°??????????????????¨?????°
f[0] = 0
for k in range(1,n+1):
f[k] = float('inf')
for c in cs:
if k - c < 0:
continue
f[k] = min(f[k], f[k - c] + 1)
print(f[n])
| 1 | 145,300,703,260 | null | 28 | 28 |
n = int(input())
ans = 0
for b in range(1, n+1):
y = n // b
ans += y*(y+1)*b/2
print(int(ans))
|
from collections import deque
n, m = map(int, input().split())
c = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a, b = a-1, b-1
c[a].append(b)
c[b].append(a)
if len(c[0]) == 0:
print('No')
exit()
l = [0]*n
l[0] = 1
q = deque()
for i in c[0]:
q.append(i)
l[i] = 1
while q:
p = q.pop()
for i in c[p]:
if l[i] == 0:
l[i] = p+1
q.appendleft(i)
print('Yes')
for i in range(1, n):
print(l[i])
| 0 | null | 15,869,871,263,248 | 118 | 145 |
def gcd_e(x, y):
if y == 0:
return x
else:
return gcd_e(y,x%y)
def lcm(x, y):
return (x * y) // gcd_e(x, y)
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
a,b=A[0],0
for i in range(1,n):
b = A[i]
a = lcm(a,b)
for i in set(A):
if (a // i) % 2 == 0:
print('0')
exit()
if a // 2 > m:
print('0')
else:
print((m-a//2)//a+1)
|
h,w,k = map(int,input().split())
m=[list(input()) for i in range(h)]
o=0
for h_bit in range(2**h):
for w_bit in range(2**w):
c=0
for i in range(h):
for j in range(w):
if (h_bit>>i)&1==0 and (w_bit>>j)&1==0 and m[i][j]=='#':
c+=1
if c==k:
o+=1
print(o)
| 0 | null | 55,659,472,571,780 | 247 | 110 |
#A - Payment
N = int(input())
#おつり
out = 1000 - (N % 1000)
if out == 1000: out = 0
# 出力
print(out)
|
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
N = INT()
S = "abcdefghijk"
# q = [("", -1)]
ans = 0
def dfs(a, mx):
# q = [(cur, mx)]
# while q:
# a, mx = q.pop()
if len(a) == N:
print(a)
return
for c in range(len(S[:mx+2])):
t = a
t += S[:mx+2][c]
dfs(t, max(c, mx))
dfs("", -1)
| 0 | null | 30,526,332,400,730 | 108 | 198 |
n = int(input())
*A, = map(int, input().split())
inf = 10**18
# DP[i][0]=i番目まででi//2個選んだ最大値
# DP[i][1]=i番目まででi//2+1個選んだ最大値
DP = [[-inf]*2 for i in range(n)]
DP[0][0] = 0
DP[0][1] = A[0]
DP[1][0] = 0
DP[1][1] = max(A[0], A[1])
for i in range(2, n):
if i % 2 == 0:
DP[i][0] = max(DP[i-2][0]+A[i], DP[i-1][1])
DP[i][1] = DP[i-2][1]+A[i]
else:
DP[i][0] = max(DP[i-2][0]+A[i], DP[i-1][0])
DP[i][1] = max(DP[i-2][1]+A[i], DP[i-1][1])
if n % 2 == 0:
print(DP[n-1][1])
else:
print(DP[n-1][0])
|
from itertools import accumulate
n = int(input())
A = list(map(int, input().split()))
L = [0]
R = []
min_L = float("inf")
for i, a in enumerate(A):
b = A[-(i+1)]
if i%2 == 0:
L.append(a)
else:
R.append(a)
R.append(0)
L = list(accumulate(L))
R = list(accumulate(R[::-1]))[::-1]
ans = -float("inf")
if n%2:
def f(A):
temp = 0
left = 0
right = 0
for i in range(2, n, 2):
temp += A[i]
res = max(ans, temp)
for i in range(1, n//2):
temp -= A[i*2]
left, right = left+A[2*(i-1)], max(left, right)+A[2*(i-1)+1]
res = max(res, temp+max(left, right))
return res
ans = max(f(A), f(A[::-1]))
temp = 0
for i in range(1, n, 2):
temp += A[i]
ans = max(ans, temp)
else:
for l, r in zip(L, R):
ans = max(ans, l+r)
print(ans)
| 1 | 37,452,106,241,630 | null | 177 | 177 |
N=int(input())
if N%1000==0:
print("0")
else:
n=((N-(N%1000))/1000)+1
print(int((1000*n)-N))
|
import sys
from bisect import bisect_right, bisect_left
from itertools import accumulate
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
def meguru_bisect(ok, ng):
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
def is_ok(x):
cnt = 0
for a in A:
t = x - a
idx = bisect_right(A, t)
cnt += n - idx
return cnt < m
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ng, ok = 0, 10 ** 15 + 1
mth = meguru_bisect(ok, ng)
R = [0] + list(accumulate(A))
res = 0
cnt = 0
for a in A:
s = mth - a
left = bisect_left(A, s)
res += (n - left) * a + R[-1] - R[left]
cnt += n - left
print(res - mth * (cnt - m))
if __name__ == '__main__':
resolve()
| 0 | null | 58,218,881,484,080 | 108 | 252 |
N = int(input())
A = list(map(int,input().split()))
# for i in range(N):
# A[i] = int(i)
cMoney = 1000
stock = 0
A.append(0)
for i in range(N):
stock = 0
if A[i] < A[i+1]:
stock = cMoney // A[i]
cMoney -= stock * A[i]
cMoney += A[i+1] * stock
print(cMoney)
# for i in range(N):
# stock = 0
# if A[i] < A[i+1]:
# stock = cMoney // A[i]
# cMoney += (A[i+1] - A[i]) * stock
# print(cMoney)
|
# まず底をみつけてそのあと山で売るを繰り返す
n = int(input())
arr = list(map(int, input().split()))
arr2 = []
cur = arr[0] if arr[0] < arr[1] else 10**10
f = 1
for i in range(1, n):
if f == 1:
# print("底値を求める")
if cur < arr[i]:
arr2.append(cur)
f *= -1
else:
# print("天井を求める")
if cur > arr[i]:
arr2.append(cur)
f *= -1
cur = arr[i]
if f == -1:
arr2.append(cur)
ans = 1000
stock = 0
for i, a in enumerate(arr2):
if i % 2 == 0:
# 買いまくる
stock = ans // a
ans %= a
else:
# うりまくる
ans += stock * a
stock = 0
print(ans)
| 1 | 7,315,233,740,440 | null | 103 | 103 |
import itertools
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
l = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in l:
w = 0
for j in abcd:
if i[j[1]-1] - i[j[0]-1] == j[2]:
w += j[3]
ans = max(ans, w)
print(ans)
|
#coding:UTF-8
n = input()
a = map(int,raw_input().split())
a.reverse()
for i in range(n):
print a[i],
| 0 | null | 14,201,660,490,160 | 160 | 53 |
# A - Number of Multiples
def main():
L, R, D = map(int, input().split())
print(R // D - (L - 1) // D)
if __name__ == "__main__":
main()
|
import bisect
from itertools import accumulate
N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
A2 = [0]+list(accumulate(A))
def judge(mid):
count = 0
for i in range(N):
border = mid - A[i]
ind = bisect.bisect_left(A,border)
count += N-ind
if count < M:
return True
else:
return False
mina = 0
maxa = 2*(10**5) + 1
while maxa-mina > 1:
mid = (maxa+mina)//2
if judge(mid):
maxa = mid
else:
mina = mid
count = 0
ans = 0
for i in range(N):
border = maxa - A[i]
ind = bisect.bisect_left(A,border)
count += N-ind
ans += A[i]*(N-ind) + A2[N]-A2[ind]
if count == M:
print(ans)
else:
rem = M-count
print(ans+rem*mina)
| 0 | null | 57,513,108,331,630 | 104 | 252 |
a = input()
print(a.replace('P?', 'PD').replace('?D', 'PD').replace('??', 'PD').replace('?', 'D'))
|
S = input()
ans=""
for i in range(len(S)):
if S[i] == "?":
ans += "D"
else:
ans += S[i]
print(ans)
| 1 | 18,390,127,921,900 | null | 140 | 140 |
# coding: UTF-8
CONTEST_NUM = 26
class Optimizer:
def __init__(self, days, s, c):
self._days = days
self._s = s
self._c = c
def optimize(self):
# 貪欲法による手法
t = []
for d in range(self._days):
max_d_value = 0
max_contest = 0
for i in range(CONTEST_NUM):
d_value = self._s[d][i] + self._c[i] * (d + 1)
if d_value > max_d_value:
max_d_value = d_value
max_contest = i
t.append(max_contest + 1)
return t
def main():
## 引数読み込み
days = int(input()) # D
c = tuple(map(int, input().split())) # c[i]
s = [] # s[d][i]
for i in range(days):
s.append(tuple(map(int, input().split())))
## 試しに指標計算
t = Optimizer(days, s, c).optimize()
## print out
for v in t:
print(v)
if __name__ == "__main__":
main()
|
# -*- config: utf-8 -*-
if __name__ == '__main__':
for i in range(int(raw_input())):
nums = map(lambda x:x**2,map(int,raw_input().split()))
nums.sort()
if nums[0]+nums[1] == nums[2]:
print "YES"
else :
print "NO"
| 0 | null | 4,873,325,088,928 | 113 | 4 |
import math
n,D = map(int,input().split())
cnt = 0
for i in range(n):
p,q = map(int,input().split())
d = math.sqrt(p**2 + q ** 2)
if D >= d:
cnt += 1
print(cnt)
|
import base64
exec(base64.b64decode(b'aW1wb3J0IHN1YnByb2Nlc3MKaW1wb3J0IHN5cwoKY29kZSA9IHIiIiIjcHJhZ21hIEdDQyBvcHRpbWl6ZSgidW5yb2xsLWxvb3BzIikKCiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxiaXRzZXQ+CiNpbmNsdWRlIDxjYXNzZXJ0PgojaW5jbHVkZSA8Y2N0eXBlPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxjb21wbGV4PgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGRlcXVlPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8bnVtZXJpYz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8c3RhY2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GlmIChuID49IChpbnQpMWU2KSByZXR1cm4gNzsKICAgICAgcmV0dXJuIDY7CiAgICB9IGVsc2UgewogICAgICBpZiAobiA+PSAoaW50KTFlNCkgcmV0dXJuIDU7CiAgICAgIGlmIChuID49IChpbnQpMWUzKSByZXR1cm4gNDsKICAgICAgaWYgKG4gPj0gKGludCkxZTIpIHJldHVybiAzOwogICAgICBpZiAobiA+PSAoaW50KTFlMSkgcmV0dXJuIDI7CiAgICAgIHJldHVybiAxOwogICAgfQogIH0KCiAgdGVtcGxhdGUgPGNsYXNzIFQsIHN0ZDo6ZW5hYmxlX2lmX3Q8c3RkOjppc19zYW1lPFQsIGludDY0Pjo6dmFsdWUsIGludD4gPSAwPgogIGlubGluZSBpbnQgZ2V0X2RpZ2l0KFQgbikgewogICAgaWYgKG4gPj0gKGludDY0KTFlMTApIHsKICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTQpIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxOCkgcmV0dXJuIDE5OwogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTE3KSByZXR1cm4gMTg7CiAgICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTYpIHJldHVybiAxNzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxNSkgcmV0dXJuIDE2OwogICAgICAgIHJldHVybiAxNTsKICAgICAgfSBlbHNlIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxNCkgcmV0dXJuIDE1OwogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTEzKSByZXR1cm4gMTQ7CiAgICAgICAgaWYgKG4gPj0gKGludDY0KTFlMTIpIHJldHVybiAxMzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxMSkgcmV0dXJuIDEyOwogICAgICAgIHJldHVybiAxMTsKICAgICAgfQogICAgfSBlbHNlIHsKICAgICAgaWYgKG4gPj0gKGludDY0KTFlNSkgewogICAgICAgIGlmIChuID49IChpbnQ2NCkxZTkpIHJldHVybiAxMDsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU4KSByZXR1cm4gOTsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU3KSByZXR1cm4gODsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU2KSByZXR1cm4gNzsKICAgICAgICByZXR1cm4gNjsKICAgICAgfSBlbHNlIHsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWU0KSByZXR1cm4gNTsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUzKSByZXR1cm4gNDsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUyKSByZXR1cm4gMzsKICAgICAgICBpZiAobiA+PSAoaW50NjQpMWUxKSByZXR1cm4gMjsKICAgICAgICByZXR1cm4gMTsKICAgICAgfQogICAgfQogIH0KCiAgdGVtcGxhdGUgPGNsYXNzIFQsIHN0ZDo6ZW5hYmxlX2lmX3Q8c3RkOjppc19zYW1lPFQsIGludD46OnZhbHVlLCBpbnQ+ID0gMD4KICBpbmxpbmUgdm9pZCB3cml0ZShUIG51bSkgewogICAgaWYgKF9fYnVpbHRpbl9leHBlY3QoY3VyICsgaW50ZWdlcl9zaXplID49IGJ1ZiArIGJ1Zl9zaXplLCAwKSkgZmx1c2goKTsKICAgIGlmIChudW0gPT0gMCkgewogICAgICB3cml0ZSgnMCcpOwogICAgICByZXR1cm47CiAgICB9CiAgICBpZiAobnVtIDwgMCkgewogICAgICB3cml0ZSgnLScpOwogICAgICBudW0gPSAtbnVtOwogICAgfQogICAgaW50IGxlbiA9IGdldF9kaWdpdChudW0pOwogICAgaW50IGRpZ2l0cyA9IGxlbjsKICAgIHdoaWxlIChudW0gPj0gMTAwMDApIHsKICAgICAgbWVtY3B5KGN1ciArIGxlbiAtIDQsIHRhYmxlICsgKG51bSAlIDEwMDAwKSAqIDQsIDQpOwogICAgICBudW0gLz0gMTAwMDA7CiAgICAgIGxlbiAtPSA0OwogICAgfQogICAgbWVtY3B5KGN1ciwgdGFibGUgKyBudW0gKiA0ICsgK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ID0gbWF4KGFucywgZHBbciAtIDFdW2MgLSAxXVtpXSk7CiAgZm91dC5wcmludGxuKGFucyk7CgogIHJldHVybiAwOwp9CiIiIgoKd2l0aCBvcGVuKCdzb2wuY3BwJywgJ3cnKSBhcyBmOgogICAgZi53cml0ZShjb2RlKQoKc3VicHJvY2Vzcy5Qb3BlbihbJ2crKycsICctc3RkPWMrKzE3JywgJy1PMicsICdzb2wuY3BwJ10pLmNvbW11bmljYXRlKCkKc3VicHJvY2Vzcy5Qb3BlbihbJy4vYS5vdXQnXSwgc3RkaW49c3lzLnN0ZGluLCBzdGRvdXQ9c3lzLnN0ZG91dCkuY29tbXVuaWNhdGUoKQ=='))
| 0 | null | 5,684,585,140,598 | 96 | 94 |
n = int(input())
x = list(map(int, input().split()))
ans = 1000000000
for p in range(min(x), max(x) + 1):
a = 0
for j in range(n):
a += (p - x[j]) ** 2
ans = min(a, ans)
print(ans)
|
n = int(input())
X = sorted(list(map(int,input().split())))
ans = 10**10
for i in range(X[0],X[-1]+1):
ans = min(ans, sum((i-x)**2 for x in X))
print(ans)
| 1 | 65,134,456,807,342 | null | 213 | 213 |
from operator import lt
def main():
N = int(input())
A = list(input().split(' '))
Ab = bubbleSort(A[:], N, lambda a, b: int(a[1]) < int(b[1]))
As = selectionSort(A[:], N, lambda a, b: int(a[1]) < int(b[1]))
print(' '.join([str(a) for a in Ab]))
print('Stable')
print(' '.join([str(a) for a in As]))
print('Stable' if Ab == As else 'Not stable')
def bubbleSort(A, N, comp=lt):
flag = True
while flag:
flag = False
for j in range(N-1, 0, -1):
if comp(A[j], A[j-1]):
A[j], A[j-1] = A[j-1], A[j]
flag = True
return A
def selectionSort(A, N, comp=lt):
for i in range(0, N-1):
minj = i
for j in range(i, N):
if comp(A[j], A[minj]):
minj = j
if i != minj:
A[i], A[minj] = A[minj], A[i]
return A
if __name__ == '__main__':
main()
|
s = input()
t = input()
sl = len(s)
tl = len(t)
ans = 1001
for i in range(sl-tl+1):
cnt = 0
for sv, tv in zip(s[i:i+tl], t):
if sv != tv:
cnt += 1
ans = min(cnt, ans)
print(ans)
| 0 | null | 1,868,788,555,650 | 16 | 82 |
(H,N) = map(int,input().split())
l = []
for i in range(N):
l.append([int(x) for x in input().split()])
dp = [10**10 for i in range(H+10**4+1)]
dp[0] = 0
for i in range(H+10**4+1):
for j in range(N):
if l[j][0] <= i:
dp[i] = min(dp[i],dp[i-l[j][0]]+l[j][1])
ans = dp[H]
for i in range(1,10**4+1):
ans = min(ans,dp[H+i])
print(ans)
|
s=input()
if s[0]==s[1] and s[1]==s[2] :print("No")
else: print("Yes")
| 0 | null | 68,133,384,974,688 | 229 | 201 |
n, k = list(map(int, input().split()))
mod = 10**9 + 7
# xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。
def modinv(x):
return pow(x, mod-2, mod)
# 二項係数の左側の数字の最大値を max_len とする。nとかだと他の変数と被りそうなので。
# factori_table = [1, 1, 2, 6, 24, 120, ...] 要は factori_table[n] = n!
# 計算時間はO(max_len * log(mod))
max_len = 2 * n - 1 #適宜変更する
factori_table = [1] * (max_len + 1)
factori_inv_table = [1] * (max_len + 1)
for i in range(1, max_len + 1):
factori_table[i] = factori_table[i-1] * (i) % mod
factori_inv_table[i] = modinv(factori_table[i])
def binomial_coefficients(n, k):
# n! / (k! * (n-k)! )
if k <= 0 or k >= n:
return 1
return (factori_table[n] * factori_inv_table[k] * factori_inv_table[n-k]) % mod
if k >= n-1:
# nHn = 2n-1 C n
print(binomial_coefficients(2 * n - 1, n))
else:
# 移動がk回←→ 人数0の部屋がk個以下
# 人数0の部屋がちょうどj個のものは
# nCj(人数0の部屋の選び方) * jH(n-j) (余剰のj人を残りの部屋に入れる)
ans = 0
for j in range(k+1):
if j == 0:
ans += 1
else:
ans += binomial_coefficients(n, j) * binomial_coefficients(n-1, j)
ans %= mod
print(ans)
|
M=10**9+7;n,k=map(int,input().split());a=c=1
for i in range(1,min(n,k+1)):m=n-i;c=c*-~m*m*pow(i,M-2,M)**2%M;a+=c
print(a%M)
| 1 | 67,112,779,218,342 | null | 215 | 215 |
n,a,b=map(int,input().split())
mod=10**9+7
def power_func(a,n,mod):
bi=str(format(n,"b"))
res=1
for i in range(len(bi)):
res=(res*res)%mod
if bi[i]=='1':
res=(res*a)%mod
return res
def cmb1(x,y,mod):
l=1
for g in range(x+1-y,x+1):
l*=g
l%=mod
for k in range(1,y+1):
l*=power_func(k,mod-2,mod)
l%=mod
return l
return ans
ans=power_func(2,n,mod)-1
ans1=cmb1(n,a,mod)
ans2=cmb1(n,b,mod)
print((ans-ans1-ans2)%mod)
|
n,a,b=map(int,input().split())
mod=10**9+7
def modpow(a,n):
if n<1:
return 1
ans=modpow(a,n//2)
ans=(ans*ans)%mod
if n%2==1:
ans*=a
return ans%mod
def cmb(n,i):#逆元
inv,ans=1,1
for j in range(1,i+1):
ans=ans*(n-j+1)%mod
inv=inv*j%mod
return (ans*modpow(inv,mod-2))%mod
ans_n=pow(2,n,mod)-1
ans_a=cmb(n,a)%mod
ans_b=cmb(n,b)%mod
print((ans_n-ans_a-ans_b)%mod)
| 1 | 66,174,493,945,274 | null | 214 | 214 |
x, y = map(int, input().split())
ans = any(2 * i + 4 * (x - i) == y for i in range(x + 1))
print('Yes' if ans else 'No')
|
x, y = map(int, input().split())
for a in range(0, 101):
for b in range(0, 101):
if a + b == x and 2*a + 4*b == y:
print('Yes')
exit()
print('No')
| 1 | 13,808,088,973,562 | null | 127 | 127 |
#E
H,N=map(int,input().split())
A=[0 for i in range(N)]
dp=[float("inf") for i in range(10**4+1)]
dp[0]=0
for i in range(N):
a,b=map(int,input().split())
A[i]=a
for j in range(10**4+1):
if j+a>10**4:
break
dp[j+a]=min(dp[j]+b,dp[j+a])
if H+max(A)>10**4:
print(min(dp[H:]))
else:
print(min(dp[H:H+max(A)]))
|
import math
a,b,x=map(int,input().split())
h=x/a/a
A=h*a
if A<=b*a/2:
L=A/b*2
ans=math.degrees(math.atan(b/L))
else:
L=A*2/a-b
ans=math.degrees(math.atan((b-L)/a))
print(ans)
| 0 | null | 122,122,735,914,806 | 229 | 289 |
def g(k):
gans = k*(k+1)/2
return gans
N = int(input())
ans = 0
for i in range(1,N+1):
ans += i*g(N//i)
# print(ans)
print(int(ans))
|
n = int(input())
answer = 0
for i in range(1, n+1):
md = n // i
answer += i * (md *(md+1) // 2)
print(answer)
| 1 | 10,990,257,211,226 | null | 118 | 118 |
while True:
H, W = map(int, input().split())
if not(H or W):
break
print(('#' * W + '\n') * H)
|
n = int(input())
P = list(map(int,input().split()))
S={}
ans="YES"
for p in P:
if p in S:
ans = "NO"
break
else:
S[p]=1
print(ans)
| 0 | null | 37,233,683,823,228 | 49 | 222 |
a,b = raw_input().split(" ")
x = int(a)*2+int(b)*2
y = int(a)*int(b)
print "%d %d"%(y,x)
|
a = input()
s = list(a)
for i in range(len(s)):
if (a[i] == '?'):
s[i] = 'D'
s = "".join(s)
print(s)
| 0 | null | 9,453,969,339,120 | 36 | 140 |
A = []
for _ in range(3):
a = [int(a) for a in input().split()]
A.append(a)
N = int(input())
for _ in range(N):
num = int(input())
for i in range(3):
for j in range(3):
if A[i][j]==num:
A[i][j] = -1
# Rows
for row in range(3):
bingo_row = True
for col in range(3):
if A[row][col]!=-1:
bingo_row = False
if bingo_row:
print('Yes')
exit()
# Columns
for col in range(3):
bingo_col = True
for row in range(3):
if A[row][col]!=-1:
bingo_col = False
if bingo_col:
print('Yes')
exit()
# Naname
bingo_naname = True
for i in range(3):
col, row = i, i
if A[row][col] != -1:
bingo_naname = False
if bingo_naname:
print('Yes')
exit()
bingo_naname = True
for i in range(3):
col, row = 2-i, i
if A[row][col] != -1:
bingo_naname = False
if bingo_naname:
print('Yes')
exit()
print('No')
|
def mod_max(loop, mod):
if mod == 0:
return 0
loop_len = len(loop)
mm = min(loop)
for start in range(loop_len):
current_sum = 0
for i in range(mod):
current_sum += loop[(start + i) % loop_len]
mm = max(current_sum, mm)
return mm
def main():
n, k = map(int, input().split())
p = [int(i) - 1 for i in input().split()]
c = [int(i) for i in input().split()]
seen = set()
loop_list = []
for start in range(n):
v = start
loop = []
while v not in seen:
seen.add(v)
loop.append(c[v])
v = p[v]
seen.add(v)
if loop:
loop_list.append(loop)
ans = min(c)
for loop in loop_list:
loop_len = len(loop)
loop_num = k // loop_len
loop_mod = k % loop_len
loop_sum = sum(loop)
ans = max(mod_max(loop, min(loop_len, k)), ans)
if loop_num > 0:
ans = max(loop_sum * loop_num + mod_max(loop, loop_mod), ans)
ans = max(loop_sum * (loop_num - 1) + mod_max(loop, loop_len), ans)
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 32,651,348,287,900 | 207 | 93 |
K=int(input())
s =input()
x = len(s)
if x<=K:
print(s)
else:
print(s[:K]+'...')
|
n = int(input())
s = input()
if len(s) > n:
print(s[:n] + '...')
else:
print(s)
| 1 | 19,534,837,408,768 | null | 143 | 143 |
h,w = list(map(int, input().split()))
board = []
dp = []
INF=10**18
for _ in range(h):
board.append(input())
dp.append([INF]*w)
dxy=[(0,1), (0,-1), (1,0), (-1,0)]
dp[0][0]=1 if board[0][0] == '#' else 0
for y in range(h):
for x in range(w):
dp_list=[dp[y][x]]
if x>0:
dp_left = dp[y][x-1]
if board[y][x-1] != board[y][x] and board[y][x] == '#':
dp_left += 1
dp_list.append(dp_left)
if y>0:
dp_up=dp[y-1][x]
if board[y-1][x] != board[y][x] and board[y][x] == '#':
dp_up += 1
dp_list.append(dp_up)
dp[y][x]=min(dp_list)
print(dp[h-1][w-1])
|
import numpy as np
H, W = map(int, input().split())
masu = [input() for i in range(H)]
#temp = [list(input().replace("#","0").replace(".","1")) for i in range(H)]
#masu = [[int(temp[i][j]) for j in range(W)] for i in range(H)]
#dp = np.zeros((H, W))
dp = [[0]*W for i in range(H)]
if masu[0][0] == "#":
dp[0][0] = 1
for i in range(0,H):
for j in range(0,W):
if i == 0:
a = 100000
else:
if masu[i][j] == "#" and masu[i-1][j] == ".":
a = dp[i-1][j] + 1
else:
a = dp[i-1][j]
if j == 0:
b = 100000
else:
if masu[i][j] == "#" and masu[i][j-1] == ".":
b = dp[i][j-1] + 1
else:
b = dp[i][j-1]
if a != 100000 or b != 100000:
dp[i][j] = min([a, b])
print(int(dp[H-1][W-1]))
| 1 | 49,114,491,352,462 | null | 194 | 194 |
#151_E
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
mod = 10 ** 9 + 7
f = [1 for _ in range(n)]
f_inv = [1 for _ in range(n)]
for i in range(2, n):
f[i] = (f[i-1] * i) % mod
f_inv[i] = pow(f[i], mod-2, mod)
def comb(n,k):
return f[n]*f_inv[k]*f_inv[n-k]%mod
ans = 0
for i in range(n-k+1):
ans = (ans + comb(n-i-1, k-1) * (a[-i-1]-a[i]))%mod
print(ans)
|
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod=10**9+7
MAX=10**5+100
g1=[1,1]
g2=[1,1]
for i in range(2,MAX+1):
num_1=g1[-1]*i%mod
g1.append(num_1)
g2.append(pow(num_1,mod-2,mod))
def cmb(n,r):
return g1[n]*g2[r]*g2[n-r]%mod
N,K = I()
A = l()
A.sort()
ans = 0
for i in range(N-K+1):
ans -= A[i]*cmb(N-i-1,K-1)
ans %= mod
A = A[::-1]
for i in range(N-K+1):
ans += A[i]*cmb(N-i-1,K-1)
ans %= mod
print(ans%mod)
| 1 | 96,004,748,187,100 | null | 242 | 242 |
n = int(input())
c = input().strip()
c = list(c)
ans = 0
low = 0
high = n-1
while low<high:
if c[low] == 'W':
while low<high and c[high]=='W':
high -= 1
if c[high] == 'R':
ans+=1
c[high], c[low] = c[low], c[high]
low += 1
print(ans)
|
h,w,k= map(int, input().split())
s = [[int(i) for i in input()] for i in range(h)]
for i in range(h):
for j in range(1,w):
s[i][j]+=s[i][j-1]
for i in range(w):
for j in range(1,h):
s[j][i]+=s[j-1][i]
# 1行目、一列目をゼロにする。
s=[[0]*w]+s
for i in range(h+1):
s[i]=[0]+s[i]
ans=float('inf')
#bit 全探索
for i in range(2**(h-1)):
line=[]
for j in range(h-1):
if (i>>j)&1:
line.append(j+1)
cnt=len(line)
line=[0]+line+[h]
x=0
flag=True
for k1 in range(1,w+1):
v=0
for l in range(len(line)-1):
# どう分割してもダメな奴に✖︎のフラグ立てる
if s[line[l+1]][k1]-s[line[l+1]][k1-1]-s[line[l]][k1]>k:
flag=False
v=max(s[line[l+1]][k1]-s[line[l+1]][x]-s[line[l]][k1]+s[line[l]][x],v)
if v>k:
cnt+=1
x=k1-1
if flag:
ans=min(cnt,ans)
print(ans)
| 0 | null | 27,260,513,534,940 | 98 | 193 |
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)
|
N, K = map(int,input().split())
tele = list(map(int, input().split()))
now = 0
goal,loop,visit = [],[],[0]*N
while visit[now]!=2:
if visit[now]==0:
goal.append(now)
else:
loop.append(now)
visit[now] += 1
now = tele[now]-1
if len(goal)>K:
print(goal[K]+1)
else:
res= len(goal)-len(loop)
print(loop[(K-res)%len(loop)]+1)
| 1 | 22,828,388,356,928 | null | 150 | 150 |
a, b, n = map(int, input().split())
x = (n//b)*b - 1
if x == -1:
x = n
ans = a*x//b - a*(x//b)
print(ans)
|
def main():
N = int(input())
A = list(map(int, input().split()))
if 1 not in A:
print(-1)
exit()
next = 1
for a in A:
if a == next:
next += 1
print(N - next + 1)
if __name__ == '__main__':
main()
| 0 | null | 71,308,419,045,126 | 161 | 257 |
number = list(map(int,input().split()))
score = list(map(int,input().split()))
score.sort()
answer = 0
if number[1] > number [0]:
number[1] = number[0]
for i in range(number[1]):
score[number[0]-1-i] = 0
for j in range(number[0]):
answer += score[j]
print(answer)
|
import math
N, K = map(int,input().split())
A = list(map(int,input().split()))
L = 1
R = 10**9+1
ans = float("INF")
while True: # midの長さの丸太に切るとして2分探索
mid = (L + R) // 2
cur = 0
for i in range(N):
cur += math.ceil(A[i]/mid) - 1 # 切る必要回数
if cur > K: #K回より大きい時は、L=midとして長くする
if L == mid:
break
L = mid
else: #K回以下の時は、R=midとして短くする
ans = min(ans, mid)
if R == mid:
break
R = mid
print(ans)
| 0 | null | 42,894,711,479,208 | 227 | 99 |
def check(P):
global k, T, n
i = 0
for j in range(k):
s = 0
while s + T[i] <= P:
s += T[i]
i += 1
if (i == n):
return n
return i
def solve():
left = 0
right = 100000 * 10000
while right - left > 1:
mid = (left + right) // 2
v = check(mid)
if (v >= n):
right = mid
else:
left = mid
return right
if __name__ == '__main__':
n, k = map(int, input().split())
T = [int(input()) for i in range(n)]
ans = solve()
print(ans)
|
def min_item_num(weights, num_item, num_vehicle, capacity):
item_idx = 0
for _ in range(num_vehicle):
load = 0
while load + weights[item_idx] <= capacity:
load += weights[item_idx]
item_idx += 1
if item_idx == num_item:
return num_item
return item_idx
def main():
num_item, num_vehicle = list(map(int, input().split(" ")))
weights = [int(input()) for _ in range(num_item)]
left = 0
right = 10000 * 100000
while right - left > 1:
center = (left + right) // 2
min_item = min_item_num(weights, num_item, num_vehicle, center)
if num_item <= min_item:
right = center
else: # min_vehicle > num_item:
left = center
print(right)
if __name__ == "__main__":
main()
| 1 | 88,238,585,970 | null | 24 | 24 |
#!/usr/bin/env python
#-*- coding:utf-8 -*-
N = int(raw_input())
A = map(int,raw_input().split())
counter = 0
for i in range(len(A)):
for j in range(len(A) - 1, 0, -1):
if A[j] < A[j-1]:
A[j],A[j-1] = A[j-1], A[j]
counter += 1
print ' '.join(map(str,A))
print counter
|
N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N):
for j in range(N - 1, i, -1):
if A[j] < A[j-1]:
A[j], A[j - 1] = A[j -1], A[j]
count += 1
print(*A)
print(count)
| 1 | 16,563,748,178 | null | 14 | 14 |
import sys
sys.setrecursionlimit(10**5)
n,m,k = map(int, input().split())
F = [[] for _ in range(n+1)]
B = [[] for _ in range(n+1)]
for _ in range(m):
x,y = map(int, input().split())
F[x].append(y)
F[y].append(x)
for _ in range(k):
x,y = map(int, input().split())
B[x].append(y)
B[y].append(x)
belongs = [-1 for _ in range(n+1)]
group_ID = 0
def f_search(root, group_ID):
for j in F[root]:
if belongs[j] == -1:
belongs[j] = group_ID
f_search(j, group_ID)
for i in range(n+1):
if belongs[i] == -1:
belongs[i] = group_ID
f_search(i, group_ID)
group_ID += 1
cnt_members = [0 for _ in range(len(set(belongs)))]
for i in belongs[1:]:
cnt_members[i] += 1
ans = [0 for _ in range(n+1)]
for i in range(1, n+1):
tmp = 0
for j in B[i]:
if belongs[j] == belongs[i]:
tmp += 1
ans[i] = cnt_members[belongs[i]] - 1 - len(F[i]) - tmp
print(*ans[1:])
|
n, k = map(int, input().split())
MOD = 10 ** 9 + 7
def factorial(n):
global fct
global invfct
fct = [0] * (n + 1)
fct[0] = 1
for i in range(1, n+1):
fct[i] = fct[i-1] * i % MOD
invfct = [0] * (n + 1)
invfct[n] = pow(fct[n], MOD - 2, MOD)
for i in range(n-1, -1, -1):
invfct[i] = invfct[i+1] * (i + 1) % MOD
def binomial(n, k):
return fct[n] * invfct[n-k] % MOD * invfct[k] % MOD
factorial(2 * n)
if k >= n:
print(binomial(2 * n - 1, n) % MOD)
else:
answer = 0
for i in range(k+1):
answer += binomial(n, i) * binomial(n-1, i)
answer %= MOD
print(answer)
| 0 | null | 64,352,795,651,580 | 209 | 215 |
N = int(input())
a_array = []
b_array = []
for i in range(N):
a, b = map(int, input().split())
a_array.append(a)
b_array.append(b)
a_array.sort()
b_array.sort()
if N % 2 != 0:
mid_min = a_array[(N - 1) // 2]
mid_max = b_array[(N - 1) // 2]
print(mid_max - mid_min + 1)
else:
mid_min = (a_array[N // 2 - 1] + a_array[N // 2]) / 2
mid_max = (b_array[N // 2 - 1] + b_array[N // 2]) / 2
print(int((mid_max - mid_min) * 2) + 1)
|
s = input()
t = input()
len_s = len(s)
len_t = len(t)
ans = len_t
for i in range(len_s - len_t + 1):
tmp_cnt = 0
tmp_s = s[i:i + len_t]
for c1, c2 in zip(tmp_s, t):
if c1 != c2:
tmp_cnt += 1
ans = min(ans, tmp_cnt)
print(ans)
| 0 | null | 10,396,449,594,630 | 137 | 82 |
def main():
import sys
input = sys.stdin.readline
h,w,m = map(int, input().split())
h_count = [0 for _ in range(h)]
w_count = [0 for _ in range(w)]
pairs = set()
for i in range (m):
hi, wi = map(int, input().split())
hi -= 1
wi -= 1
h_count[hi] += 1
w_count[wi] += 1
pairs.add((hi,wi))
h_max = max(h_count)
w_max = max(w_count)
hk = list()
wk = list()
for index , hj in enumerate(h_count):
if hj == h_max:
hk.append(index)
for index, wj in enumerate(w_count):
if wj == w_max:
wk.append(index)
ans = h_max + w_max
for _h in hk:
for _w in wk:
hw = (_h, _w)
if hw in pairs:
continue
print(ans)
exit()
print(ans -1)
if __name__ == '__main__':
main()
|
h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = set()
maxX = maxY = 0
for i in range(m):
i,j = map(lambda x:int(x)-1, input().split())
bomb.add((i,j))
bombX[i] += 1
bombY[j] += 1
maxX = max(maxX,bombX[i])
maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
for j in maxY_index:
if (i,j) in bomb: continue
print(maxX+maxY)
exit()
print(maxX+maxY-1)
| 1 | 4,727,293,809,428 | null | 89 | 89 |
def sell(rate, kabu):
return rate * kabu
def buy(rate, money):
kabu = money//rate
otsuri = money % rate
return kabu, otsuri
N,*A = map(int, open(0).read().split())
money = 1000
have_kabu = 0
for i in range(N):
#売る
money += sell(A[i], have_kabu)
have_kabu = 0
#買う
if i != N-1 and A[i] < A[i+1]:
have_kabu, money = buy(A[i], money)
print(money)
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def main():
N = int(input())
A = list(map(int, input().split()))
ans = 1000
for i in range(1, N):
if A[i - 1] < A[i]:
stock = ans // A[i - 1]
ans += stock * (A[i] - A[i - 1])
print(ans)
if __name__ == "__main__":
main()
| 1 | 7,257,017,833,892 | null | 103 | 103 |
k = int(input())
a, b = map(int, input().split())
check = False
for i in range(a, b + 1):
if i % k == 0:
check = True
print("OK" if check else "NG")
|
H, W, K = map(int, input().split())
S = []
for _ in range(H):
S.append(input())
hsummation = [[int(S[0][k])]for k in range(W)]
for k in range(W):
for j in range(1, H):
hsummation[k].append(hsummation[k][j-1] + int(S[j][k]))
#print(hsummation)
ans = float('inf')
anskouho = 0
h = 2**(H-1)
for j in range(2**(H-1)):
h -= 1
binh = "0"*(H-len(str(bin(h)))+1)+str(bin(h))[2:]
hlist=[]
for k in range(H-1):
if binh[-k-1] == '1':
hlist.append(k)
hlist.append(-1)
#print(hlist)
anskouho = len(hlist)-1
now = 0
while now < W:
counter = [0 for _ in range(len(hlist))]
while now < W:
for l in range(len(hlist)):
if l == 0:
counter[l] += hsummation[now][int(hlist[l])]
continue
else:
counter[l] += - hsummation[now][hlist[l-1]] + hsummation[now][hlist[l]]
if max(counter) > K:
anskouho += 1
break
else:
now += 1
if anskouho > ans:
break
else:
ans = min(ans, anskouho)
#print(counter)
#print(anskouho)
print(ans)
| 0 | null | 37,611,924,351,882 | 158 | 193 |
from bisect import bisect_left
N = int(input())
Lli = list(map(int,input().split()))
Lli.sort()
res = 0
for i in range(N-2):
for j in range(i+1,N-1):
ab = Lli[i]+Lli[j]
res += bisect_left(Lli,ab)-j-1
print(res)
|
s = "1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51"
S = s.split(", ")
k = int(input())
print(S[k-1])
| 0 | null | 111,007,942,883,790 | 294 | 195 |
# -*- coding: utf-8 -*-
def selection_sort(seq, l):
cnt = 0
for head_i in range(l):
flag = False
min_i = head_i
for target_i in range(head_i+1, l):
if seq[target_i] < seq[min_i]:
min_i = target_i
flag = True
if flag:
seq[head_i], seq[min_i] = seq[min_i], seq[head_i]
cnt += 1
print(' '.join([str(n) for n in seq]))
print(cnt)
def to_int(v):
return int(v)
def to_seq(v):
return [int(c) for c in v.split()]
if __name__ == '__main__':
l = to_int(input())
seq = to_seq(input())
selection_sort(seq, l)
|
n = int(input())
print(n//2 + n%2)
| 0 | null | 29,307,780,922,320 | 15 | 206 |
import sys
n = int(sys.stdin.readline())
lst = list(map(int, sys.stdin.readline().split()))
lst.sort(reverse = True)
ans = lst[0]
cnt = 0
idx = 1
for _ in range(1, n-1):
if cnt <= 1:
ans += lst[idx]
cnt += 1
else:
idx += 1
cnt = 1
ans += lst[idx]
print(ans)
|
import math
N=int(input())
A=[int(x) for x in input().split()]
ans=0
A=sorted(A)
ans+=A[N-1]
for i in range(1,N-1):
ans+=A[N-1-math.ceil(i/2)]
print(ans)
| 1 | 9,206,625,745,230 | null | 111 | 111 |
a = input()
b = input()
n = len(a)
count = 0
for i in range(n):
if a[i] != b[i]:
count = count +1
print(count)
|
#!/usr/bin/env python3
S = input()
T = input()
count = 0
for s, t in zip(S, T):
if s != t:
count += 1
ans = count
print(ans)
| 1 | 10,466,646,392,072 | null | 116 | 116 |
import math
a,b,n=map(int,input().split())
if n<b :
ans=math.floor(a*n/b)
else :
ans=math.floor(a*(b-1)/b)
print(ans)
|
n = int(input())
A = list(map(int, input().split()))
a = []
for i in range(len(A)):
a.append([A[i], i])
a.sort(key = lambda x: x[0], reverse = True)
dp = [[0 for i in range(n+1)] for j in range(n+1)]
for i in range(n):
for l in range(i+1):
dp[i+1][l] = max(dp[i+1][l], dp[i][l] + a[i][0]*(n-1-(i-l)-a[i][1]))
dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + a[i][0] * (a[i][1] - l))
ans = 0
for now in dp[n]:
ans = max(now, ans)
print(ans)
| 0 | null | 30,996,000,887,260 | 161 | 171 |
print("Yes" if (lambda x: x[2] == x[3] and x[4] == x[5])(input()) else "No")
|
def resolve():
S = input()
print('Yes' if S[2]==S[3] and S[4]==S[5] else 'No')
resolve()
| 1 | 42,083,830,209,510 | null | 184 | 184 |
N = int(input())
MODZ = 10**9 + 7
ans = (10**N - 2*9**N + 8**N) % MODZ
print(ans)
|
def main():
num = int(input())
con = 10**9 + 7
a, b, c = 1, 1, 2
for i in range(num):
a *= 10
a %= con
b *= 8
b %= con
c *= 9
c %= con
if(a+b-c < 0):
print(a+b-c+con)
return 0
print(a+b-c)
if __name__ == '__main__':
main()
| 1 | 3,175,399,454,900 | null | 78 | 78 |
class Stack(list):
def __init__(self):
self.length = 0
def __len__(self):
return self.length
def push(self,x):
self.insert(len(self),x)
self.length += 1
def pop(self):
try:
if(len(self)==0):
raise NameError("stack is empty")
tmp = self[len(self)-1]
self.length -= 1
return tmp
except:
print("stack is empty!!!!")
return '#'
data = Stack()
s = input()
x = s.split()
for i in x:
if i == '+':
a = data.pop()
b = data.pop()
data.push(b+a)
elif i == '-':
a = data.pop()
b = data.pop()
data.push(b-a)
elif i == '*':
a = data.pop()
b = data.pop()
data.push(b*a)
else:
data.push(int(i))
print(data.pop())
|
class Stack():
def __init__(self):
self.el = []
def add(self, el):
self.el.append(el)
def pop(self):
return self.el.pop()
def isEmpty(self):
if len(self.el) == 0:
return True
else:
return False
a = raw_input().split()
#print a
st = Stack()
for index in range(len(a)):
#print a[index]
if a[index] in ['+', '-', '*']:
num1 = st.pop()
num2 = st.pop()
if a[index] == '+':
st.add(int(num1) + int(num2))
elif a[index] == '-':
st.add(int(num2) - int(num1))
else:
st.add(int(num1) * int(num2))
else:
st.add(a[index])
#print st.el
ans = st.pop()
print ans
| 1 | 34,695,487,804 | null | 18 | 18 |
import sys
X, Y = map(int, input().split())
mod = int(1e9) + 7
N = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
# 組み合わせ(1e9+7で割る場合)
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
# 方針:Xについてすべての動かし方を列挙する。Yに符合すれば組み合わせ計算、それ以外であれば没
kake = 0
ans = 0
while X >= 0:
temp = X
if temp*2 + kake*1 == Y:
ans += cmb(temp + kake, kake, mod)
kake += 1
X -= 2
print(ans)
|
x,y = map(int,input().split())
#xをベースに
r = 0 #→2 ↑1
u = x #→1 ↑2
while y != r + u*2:
u -= 2
r += 1
if u < 0: #到達する方法がない
print(0)
exit()
#modありのコンビネーション計算
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9 + 7
N = u+r+1 #必要な数
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
print(cmb(u+r,r,mod))
| 1 | 149,369,899,950,300 | null | 281 | 281 |
N = int(input())
ans = pow(10, N, mod=1000000007) - pow(9, N, mod=1000000007) - pow(9, N, mod=1000000007) + pow(8, N, mod=1000000007)
ans %= 1000000007
print(ans)
|
def sell(rate,kabu):
money = rate * kabu
return money
def buy(rate,money):
kabu = money//rate
otsuri = money % rate
return kabu,otsuri
N,*A = map(int,open(0).read().split())
max_money = 1000
have_money = 1000
have_kabu = 0
for i in range(N-1):
if A[i] < A[i+1] and have_money > A[i]: #買う
have_kabu,have_money = buy(A[i],have_money)
if A[i] > A[i+1]: #売る
have_money += sell(A[i],have_kabu)
have_kabu = 0
max_money = max(max_money,have_money)
have_money += sell(A[-1],have_kabu)
max_money = max(max_money,have_money)
print(max_money)
| 0 | null | 5,222,086,492,040 | 78 | 103 |
N = int(input())
A = list(map(int, input().split()))
D = [[0] * 2 for _ in range(N)]
for i in range(N):
D[i] = [i, A[i]]
D.sort(key = lambda x: x[1], reverse = True)
DP = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N):
n, x = D[i]
t = i + 1
for j in range(t + 1):
l = j
r = t - j
rr = N - 1 - r
if l != 0:
DP[l][r] = DP[l - 1][r] + abs(n - l + 1) * x
if r != 0:
DP[l][r] = max(DP[l][r], DP[l][r - 1] + abs(rr + 1 - n) * x)
ans = 0
for i in range(N + 1):
ans = max(ans, DP[i][N - i])
print(ans)
|
N,M,X=[int(s) for s in input().split()]
Book=[[int(s) for s in input().split()] for _ in range(N)]
INF=10**7
ans=set()
ans.add(INF)
#深さ優先探索
def DFS(n,cost,Xls):
if n==N:
if min(Xls)>=X:
ans.add(cost)
else:
Xnext=[Xls[i]+Book[n][i+1] for i in range(M)]
DFS(n+1,cost+Book[n][0],Xnext)
DFS(n+1,cost,Xls)
DFS(0,0,[0 for _ in range(M)])
if min(ans)==INF:
print(-1)
else:
print(min(ans))
| 0 | null | 28,043,137,821,088 | 171 | 149 |
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")
|
n = input()
l = map(int,raw_input().split())
print min(l), max(l), sum(l)
| 0 | null | 2,402,657,244,052 | 85 | 48 |
s=input()
q=int(input())
quary=[]
for i in range(q):
temp=list(input().split())
quary.append(temp)
switch=False
first=''
last=''
for i in range(q):
if quary[i][0]=='1':
if switch:
switch=False
else:
switch=True
else:
if quary[i][1]=='1':
if switch:
last+=quary[i][2]
else:
first+=quary[i][2]
else:
if switch:
first+=quary[i][2]
else:
last+=quary[i][2]
s=first[::-1]+s+last
if switch:
print(s[::-1])
else:
print(s)
|
s=input()
q=int(input())
cnt=0
front=[]
rear=[]
for qq in range(q):
l=list(input().split())
if l[0]=='1':
cnt+=1
else:
if (cnt+int(l[1]))%2==1:
front.append(l[2])
else:
rear.append(l[2])
front=''.join(front[::-1])
rear=''.join(rear)
s=front+s+rear
print(s if cnt%2==0 else s[::-1])
| 1 | 57,238,179,919,392 | null | 204 | 204 |
def warshall_floyd(edge):
e=edge
for k in range(len(e)):
for i in range(len(e)):
for j in range(len(e)):
e[i][j]=min(e[i][j],e[i][k]+e[k][j])
return e
n,m,l=map(int,input().split())
edge=[n*[10**18]for _ in range(n)]
for i in range(n):
edge[i][i]=0
for i in range(m):
a,b,c=map(int,input().split())
a-=1
b-=1
edge[a][b]=edge[b][a]=c
edge=warshall_floyd(edge)
edge2=[n*[10**18]for _ in range(n)]
for i in range(n):
edge2[i][i]=0
for j in range(n):
if edge[i][j]<=l:
edge2[i][j]=1
edge2=warshall_floyd(edge2)
q=int(input())
for i in range(q):
s,t=map(int,input().split())
s-=1
t-=1
ans=edge2[s][t]
if ans==10**18:
print(-1)
else:
print(ans-1)
|
class Card:
def __init__(self, inf):
self.num = int(inf[1])
self.inf = inf
n, card = int(input()), [Card(i) for i in input().split()]
card1 = card[:]
for i in range(n-1):
for j in range(n-1, i, -1):
if card[j].num < card[j-1].num:
card[j], card[j-1] = card[j-1], card[j]
print(" ".join([i.inf for i in card]))
print("Stable")
for i in range(n-1):
for j in range(i+1, n):
if j == i+1:
idx = j
elif card1[idx].num > card1[j].num:
idx = j
if card1[i].num > card1[idx].num:
card1[i], card1[idx] = card1[idx], card1[i]
print(" ".join([i.inf for i in card1]))
if card == card1:
print("Stable")
else:
print("Not stable")
| 0 | null | 86,674,933,686,748 | 295 | 16 |
N, X, T = map(int, input().split())
ans = T * (N // X)
ans = ans if (N % X) is 0 else ans + T
print(ans)
|
n, x, t = map(int, input().split())
print(n//x*t) if n%x==0 else print((n//x+1)*t)
| 1 | 4,279,571,315,584 | null | 86 | 86 |
n = int(input())
a = (10**n) % (10**9+7)
b = (9**n) % (10**9+7)
c = (8**n) % (10**9+7)
print((a-2*b+c) % (10**9+7))
|
N=int(input())
x=(10**N-2*(9**N)+8**N)%(10**9+7)
print(x)
| 1 | 3,126,639,315,968 | null | 78 | 78 |
import math
a, b, k = list(map(int, input().split(' ')))
if k > a*a*b / 2:
# k = a*a*b - a*a*p/2 | pは斜めの水の内側の高さの方の辺, p = b で丁度半分
# p = 2b - 2k/(a*a)
p = 2*b - 2*k / (a*a)
# 三角形の長い方をqとして qq = pp + aa
q = (p**2 +a**2) ** 0.5
# q * sin(r) = p
# r = ... わからんので 2分探索
left = 0.0
right = 90.0
while right > left + 1e-12:
r = (left + right) / 2.0
if q * math.sin(math.radians(r)) < p:
left = r
else:
right = r
else:
# k = a*b*p/2
# p = 2*k/(a*a)
p = 2*k/(a*b)
# 三角形の長い方をqとして qq = pp + aa
q = (p**2 +b**2) ** 0.5
# r = ... わからんので 2分探索
left = 0.0
right = 90.0
while right > left + 1e-12:
r = (left + right) / 2.0
if q * math.sin(math.radians(r)) < b:
left = r
else:
right = r
print(r)
|
import sys
#import time
from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
import math
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
a,b,x = rl()
if x<=a*a*b/2:
print(180*math.atan(b**2*a/x/2)/math.pi)
else:
t = 2*(a*a*b-x)/a/a
print(180/math.pi*math.atan(t/a))
| 1 | 162,568,072,384,300 | null | 289 | 289 |
# 17-Character-Counting_Characters.py
# ?????????????????????
# ?????????????????±??????????????????????????¢???????????????????????°?????°??????????????°?????????????????????????????????
# ??????????°?????????¨??§??????????????\???????????????
# Input
# ?????°????????????????????????????????±???????????????????????????
# Output
# ?????????????????±???????????????????????¢???????????????????????°?????\??????????????¢?????§????????????????????????
# a : a????????°
# b : b????????°
# c : c????????°
# .
# .
# z : z????????°
# Constraints
# ??±???????????????????????° < 1200
# Sample Input
# This is a pen.
# Sample Output
# a : 1
# b : 0
# c : 0
# d : 0
# e : 1
# f : 0
# g : 0
# h : 1
# i : 2
# j : 0
# k : 0
# l : 0
# m : 0
# n : 1
# o : 0
# p : 1
# q : 0
# r : 0
# s : 2
# t : 1
# u : 0
# v : 0
# w : 0
# x : 0
# y : 0
# z : 0
import sys
s = str()
alphabet = "abcdefghijklmnopqrstuvwxyz"
for line in sys.stdin:
s += line.lower()
# print(list(s))
for c in alphabet:
print("{} : {}".format(c, s.count(c)) )
|
import sys
#fin = open("test.txt", "r")
fin = sys.stdin
sentence = fin.read()
sentence = sentence.lower()
num_alphabet_list = [0 for i in range(26)]
for c in sentence:
if ord(c) < ord('a') or ord(c) > ord('z'):
continue
num_alphabet_list[ord(c) - ord('a')] += 1
for i in range(0, 26):
print(chr(i + ord('a')), end="")
print(" : ", end="")
print(num_alphabet_list[i])
| 1 | 1,640,067,960,960 | null | 63 | 63 |
from collections import deque
S=deque(input())
Q=int(input())
ans=0
for i in range(Q):
q=input()
if q[0]=='2':
if ans%2==0:
if q[2]=='1':
S.appendleft(q[4])
else:
S.append(q[4])
else:
if q[2]=='1':
S.append(q[4])
else:
S.appendleft(q[4])
else:
ans+=1
S="".join(S)
print(S if ans%2==0 else S[::-1])
|
from collections import deque
def main():
s = list(input())
q = int(input())
query = []
que = deque(s)
f = False
for i in range(q):
query.append(input().split(" "))
for i in range(q):
if query[i][0] == '1':
f = not f
else:
if f:
if query[i][1] == '2':
que.appendleft(query[i][2])
else:
que.append(query[i][2])
else:
if query[i][1] == '1':
que.appendleft(query[i][2])
else:
que.append(query[i][2])
if f:
que.reverse()
print("".join(que))
if __name__ == "__main__":
main()
| 1 | 57,103,238,342,880 | null | 204 | 204 |
A, B = [x for x in input().split(" ")]
print(int(A) * int(float(B) * 100 + 0.5) // 100)
|
from decimal import Decimal as D
a,b=map(str,input().split())
print(int(D(a)*D(b)))
| 1 | 16,450,570,364,768 | null | 135 | 135 |
from math import ceil
n = int(input())
dept = 100000
for i in range(n):
dept += dept*0.05
dept = int(int(ceil(dept/1000)*1000))
print(dept)
|
N, S = map(int, input().split())
A = list(map(int, input().split()))
mod = 998244353
dp = [[0 for j in range(S+1)] for i in range(N+1)]
dp[0][0] = 1
for i in range(N):
for j in range(S+1):
dp[i+1][j] += 2*dp[i][j]
dp[i+1][j] %= mod
if j + A[i] <= S:
dp[i+1][j+A[i]] += dp[i][j]
dp[i+1][j+A[i]] %= mod
print(dp[N][S])
| 0 | null | 8,812,588,142,140 | 6 | 138 |
N = int(input())
A = list(map(int, input().split()))
if 0 in A:
ans = 0
else:
ans = 1
max_limit = 10 ** 18
for i in range(N):
if ans * A[i] > max_limit:
ans = -1
break
else:
ans *= A[i]
print(str(ans))
|
def check(x, A, K):
import math
sumA = 0
for a in A:
if a > x:
sumA += math.ceil(a / x) - 1
if sumA <= K:
return True
else:
return False
def resolve():
_, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ok = max(A) # maxVal when minimize
ng = -1 # maxVal when maximize
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if mid > 0 and check(mid, A, K):
ok = mid
else:
ng = mid
print(ok)
resolve()
| 0 | null | 11,329,296,493,922 | 134 | 99 |
ab = input().split()
print(int(ab[0]) * int(ab[1]))
|
def main():
A, B = map(int, input().split())
print(A * B)
if __name__ == '__main__':
main()
| 1 | 15,851,729,748,948 | null | 133 | 133 |
from sys import stdin
n, m, l = [int(x) for x in stdin.readline().rstrip().split()]
a = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(n)]
b = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(m)]
c = [[0] *l for _ in range(n)]
for i in range(n):
for j in range(l):
for k in range(m):
c[i][j] += a[i][k]*b[k][j]
for i in range(n):
print(*c[i])
|
n, m, l = map(int, input().split())
a = []
b = []
ans = [[0 for _ in range(l)] for _ in range(n)]
for _ in range(n):
a.append(list(map(int, input().split())))
for _ in range(m):
b.append(list(map(int, input().split())))
for i in range(n):
for j in range(l):
for k in range(m):
ans[i][j] = ans[i][j] + a[i][k] * b[k][j]
for ans_row in ans:
print(*ans_row)
| 1 | 1,439,869,254,450 | null | 60 | 60 |
import sys
for e in sys.stdin:
a, b = map(int, e.split())
p = a * b
while b:
a, b = b, a%b
pass
print (a,p//a)
|
def gcd(a,b):
while a>0 and b>0:
if a<b:
b -= a
elif a>b:
a -= b
else:
return a
raise
def lcm(a,b):
return a*b/gcd(a,b)
while True:
try:
inp = raw_input()
except EOFError:
break
a, b = map((lambda x: int(x) ), inp.split())
print gcd(a,b),lcm(a,b)
| 1 | 607,962,432 | null | 5 | 5 |
n = int(input())
a = list(map(int,input().split()))
plus = dict()
minus = dict()
for i in range(n):
num = i+1
p = a[i] + num
m = num - a[i]
if p in plus:
plus[p] += 1
else:
plus[p] = 1
if (m) in minus:
minus[m] += 1
else:
minus[m] = 1
c = 0
for v in plus.keys():
if v in minus:
c += plus[v] * minus[v]
print(c)
|
#bubble
N=int(input())
A=[int(i) for i in input().split()]
fl=1
C=0
while fl==1:
fl=0
for j in range(N-1):
if A[N-1-j]<A[N-2-j]:
t=A[N-1-j]
A[N-1-j]=A[N-2-j]
A[N-2-j]=t
C+=1
fl=1
for j in range(N):
A[j]=str(A[j])
print(" ".join(A))
print(C)
| 0 | null | 12,994,196,592,160 | 157 | 14 |
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()
def resolve():
N,K,S=map(int,input().split())
if S>1:
print(*[S]*(K)+[S-1]*(N-K))
else:
print(*[S]*(K)+[S+1]*(N-K))
resolve()
|
N,K,S = map(int,input().split())
cur = 1 if S==10**9 else S+1
ans = [S]*K +[cur]*(N-K)
print(*ans)
| 1 | 91,044,483,135,540 | null | 238 | 238 |
import math
n = input()
money = 100000
for i in range(0, n):
money = money*1.05
money /= 1000
money = int(math.ceil(money)*1000)
print money
|
n = int(input())
if n % 2 == 0:
print(f'{(n/2) / n:.10f}')
else:
print(f'{(n//2 + 1) / n:.10f}')
| 0 | null | 88,705,330,620,690 | 6 | 297 |
h,a = [int(i) for i in input().split()]
cont=0
while h > 0:
h-=a
cont+=1
print(cont)
|
import sys
import math
H,A = map(int,input().split())
if not ( 1 <= H <= 10**4 and 1 <= A <= 10**4 ): sys.exit()
if not ( isinstance(H,int) and isinstance(A,int)): sys.exit()
print(math.ceil(H/A))
| 1 | 77,078,614,854,592 | null | 225 | 225 |
from sys import stdin
readline = stdin.readline
payment, _ = map(int, readline().split())
coin = list(map(int, readline().split()))
dp = [float('inf')] * (payment + 1)
dp[0] = 0
for ci in coin:
for pi in range(ci, len(dp)):
if dp[pi] > dp[pi - ci] + 1:
dp[pi] = dp[pi - ci] + 1
print(dp[-1])
|
n, m = map(int, input().split())
*C, = map(int, input().split())
# n, m = 15, 6
# C = [1, 2, 7, 8, 12, 50]
# DP[i][j]=i種類以内でj円払う最小枚数
inf = 10**10
DP = [[inf for j in range(n+1)] for i in range(m+1)]
DP[0][0] = 0
for i in range(m):
for j in range(n+1):
if j < C[i]:
DP[i+1][j] = DP[i][j]
else:
DP[i+1][j] = min(DP[i][j], DP[i+1][j-C[i]]+1)
print(DP[m][n])
| 1 | 141,761,850,954 | null | 28 | 28 |
n=int(input())
a=list(map(int,input().split()))
p=[]
m=[]
for i in range(n):
p.append(i+a[i])
m.append(i-a[i])
x=[0]*(2*n)
y=[0]*(2*n)
ans=0
for i in range(n):
if 0<=p[i]<=(2*n)-1:
x[p[i]]+=1
if 0<=m[i]<=(2*n)-1:
y[m[i]]+=1
for i in range(n):
ans+=x[i]*y[i]
print(ans)
|
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
s = [i+a for i, a in enumerate(A)]
t = [j-a for j, a in enumerate(A)]
d = Counter(s)
ans = 0
for i in t:
ans += d[i]
print(ans)
| 1 | 25,977,309,304,362 | null | 157 | 157 |
n = int(input())
A = [0] + sorted(list(map(int, input().split())), reverse=True)
res = A[1] + sum(A[2:(n//2)+1]) * 2
if n % 2 == 0:
print(res)
else:
res += A[n//2 + 1]
print(res)
|
a, b, c = map(int, input().split())
if (a**2 + b**2 + c**2) > 2*(a*b + a*c + b*c) and (c - a -b) >= 0:print('Yes')
else:print('No')
| 0 | null | 30,444,475,054,720 | 111 | 197 |
n = int(input())
def yakusu(x):
xx = 2
y = []
while xx**2 < x:
if x%xx == 0:
y.append(xx)
y.append(x//xx)
xx += 1
if xx**2 == x:
y.append(xx)
y.append(x)
return y
if n == 2:
ans = 0
else:
ans = len(yakusu(n-1))
for i in yakusu(n):
nn = n
while True:
if nn%i == 0:
nn = nn//i
else:
break
if nn%i == 1:
ans += 1
print(ans)
|
# coding: utf-8
s = input()
ans = "x" * len(s)
print(ans)
| 0 | null | 57,147,319,673,860 | 183 | 221 |
x, y = map(int, input().split())
a = []
for i in range(0, x+1):
a.append(2 * i + 4 * (x - i))
print("Yes" if y in a else "No")
|
X, Y= map(int, input().strip().split())
if Y%2==0 and 2*X<=Y and Y<=4*X:print('Yes')
else:print('No')
| 1 | 13,757,030,576,992 | null | 127 | 127 |
MOD = 10**9 + 7
dp = {0: 1}
S = int(input())
for s in range(3, S+1):
for term in range(3, s+1):
if s - term < 0:
break
else:
dp[s] = dp.get(s-term, 0) + dp.get(s, 0) % MOD
print(dp.get(S, 0))
|
a,b = map(int, input().split())
d = a/b
r = a%b
print('%d %d %f' % (d,r,d))
| 0 | null | 1,914,170,172,030 | 79 | 45 |
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
return ''.join(['x']*len(input()))
if __name__ == '__main__':
print(solve())
|
def main():
a, b, c = map(int, input().split())
ans = 'No'
if c - a - b >= 0 and 4 * a * b < (c - a - b) ** 2:
ans = 'Yes'
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 62,548,641,043,680 | 221 | 197 |
A1=list(map(int,input().split()))
A2=list(map(int,input().split()))
A3=list(map(int,input().split()))
n = int(input())
B = [int(input()) for i in range(n)]
A = [A1,A2,A3]
ans = [[0,0,0],[0,0,0],[0,0,0]]
for b in B:
for i in range(3):
for j in range(3):
if A[i][j] == b:
ans[i][j] = 1
for i in range(3):
if ans[i][0] and ans[i][1] and ans[i][2]:
print("Yes")
quit()
if ans[0][i] and ans[1][i] and ans[2][i]:
print("Yes")
quit()
if ans[0][0] and ans[1][1] and ans[2][2]:
print("Yes")
quit()
if ans[0][2] and ans[1][1] and ans[2][0]:
print("Yes")
quit()
print("No")
|
n = int(input())
a = list(map(int,input().split(" ")))
answer = 0
for i in range(1,n):
if a[i] < a[i - 1]:
answer += a[i-1] - a[i]
a[i]= a[i - 1]
print(answer)
| 0 | null | 32,243,738,424,380 | 207 | 88 |
wd = ['SUN','MON','TUE','WED','THU','FRI','SAT']
s = input()
print(str(7 - wd.index(s) % 7))
|
n = int(input())
li_a = list(map(int, input().split()))
dic_a = {}
for a in li_a:
dic_a[a] = dic_a.get(a, 0) + 1
q = int(input())
li_bc = list()
for i in range(q):
li_bc.append(tuple(map(int, input().split())))
answer = sum(li_a)
for l in li_bc:
b = l[0]
c = l[1]
diff = (c - b) * dic_a.get(b, 0)
if b in dic_a.keys():
dic_a[c] = dic_a.get(c, 0) + dic_a.get(b, 0)
dic_a[b] = 0
answer += diff
else:
pass
print(answer)
| 0 | null | 72,625,511,904,160 | 270 | 122 |
S = input()
T = input()
t = len(T)
T = T[0:t-1]
if T == S:
print('Yes')
else:
print('No')
|
def main():
n = int(input())
S, T = [], []
for _ in range(n):
s, t = input().split()
S.append(s)
T.append(int(t))
x = input()
print(sum(T[S.index(x) + 1:]))
if __name__ == '__main__':
main()
| 0 | null | 59,424,190,053,924 | 147 | 243 |
def resolve():
N = int(input())
print(1000 * ((1000 + (N-1)) // 1000) - N)
resolve()
|
l=input("").split(" ")
n=int(l[0])
k=int(l[1])
s=n%k
ss=k-s
if(s<ss):
print(s)
else:
print(ss)
| 0 | null | 23,881,711,367,360 | 108 | 180 |
def resolve():
import math as m
N = int(input())
nn = m.ceil(N / 1.08)
if m.floor(nn * 1.08) == N:
print(nn)
else:
print(":(")
resolve()
|
def bfs(n,e,fordfs,D):
#点の数、スタートの点、有向グラフ
W = [-1]*(n-1)
#各点の状態量、最短距離とか,見たかどうかとか
used = defaultdict(bool)
que = deque()
que.append(e)
while que:
now = que.popleft()
i = 1
for ne in fordfs[now]:
l = min(now,ne); r = max(now,ne)
if W[D[(l,r)]] == -1:
while(True):
if not used[(now,i)]:
used[(now, i)] = True
used[(ne, i)] = True
break
i +=1
que.append(ne)
W[D[(l,r)]] = i
return W
def examD():
N = I()
V = [[]for _ in range(N)]
d = defaultdict(int)
for i in range(N-1):
a, b = LI()
V[a-1].append(b-1)
V[b-1].append(a-1)
d[(a-1,b-1)] = i
ans = bfs(N,0,V,d)
print(max(ans))
for v in ans:
print(v)
return
def examF():
N, M = LI()
S = SI()
ans = []
cur = N
while(cur>0):
k = min(M,cur)
while(S[cur-k]=="1"):
k -=1
if k==0:
print("-1")
return
ans.append(k)
cur -=k
for v in ans[::-1]:
print(v)
return
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]
if __name__ == '__main__':
examF()
| 0 | null | 132,577,811,949,528 | 265 | 274 |
x,y,a,b,c=map(int, input().split())
*p,=map(int, input().split())
*q,=map(int, input().split())
*r,=map(int, input().split())
from heapq import *
p=[-i for i in p]
q=[-i for i in q]
r=[-i for i in r]
heapify(p)
heapify(q)
heapify(r)
ans=0
cnt=0
pt=heappop(p)
qt=heappop(q)
rt=heappop(r)
n=x+y
while (x>=0 or y>=0) and cnt<n:
if rt==0:
if pt<=qt:
ans-=pt
x-=1
cnt+=1
if p and x>0: pt=heappop(p)
else: pt=0
else:
ans-=qt
y-=1
cnt+=1
if q and y>0: qt=heappop(q)
else: qt=0
elif pt<=rt:
ans-=pt
x-=1
cnt+=1
if p and x>0: pt=heappop(p)
else: pt=0
elif qt<=rt:
ans-=qt
y-=1
cnt+=1
if q and y>0: qt=heappop(q)
else: qt=0
else:
ans-=rt
cnt+=1
if r: rt=heappop(r)
else: rt=0
print(ans)
|
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()))
p.sort(reverse=True)
q.sort(reverse=True)
eat_list = []
for i in range(x):
eat_list.append(p[i])
for i in range(y):
eat_list.append(q[i])
for i in range(c):
eat_list.append(r[i])
eat_list.sort(reverse=True)
ans = 0
for i in range(x+y):
ans += eat_list[i]
print(ans)
| 1 | 45,148,022,322,572 | null | 188 | 188 |
while True:
try:
a = map(int,raw_input().split())
n = [a[0],a[1]]
while True:
if a[0] < a[1]:
tmp = a[0]
a[0] = a[1]
a[1] = tmp
a[0] = a[0] - a[1]
if a[0] == 0:
break
b = n[0]*n[1]/a[1]
print "%d %d" % (a[1],b)
except EOFError:
break
|
while True:
try:
a, b = map(int, input().split(" "))
if a < b:
key = a
a = b
b = key
A = a
B = b
while a % b != 0:
key = a % b
a = b
b = key
key = int(A * B / b)
print(b, key)
except:
break
| 1 | 550,518,798 | null | 5 | 5 |
import math
def cin():
in_ = list(map(int,input().split()))
if len(in_) == 1: return in_[0]
else: return in_
a, b, x = cin()
if a * a * b == x: print(0)
else:
if 2 * x <= a ** 2 * b:
p = 2 * x / (a * b)
print(math.degrees(math.atan(b / p)))
else:
S = x / a
p = 2 * (a * b - S) / a
print(90 - math.degrees(math.atan(a / p)))
|
#coding:utf-8
#????????¢?????????
def draw_square(h, w):
for i in range(h):
a = ""
for j in range(w):
a += "#"
print a
while 1:
HW = raw_input().split()
H = int(HW[0])
W = int(HW[1])
if H == 0 and W == 0:
break
draw_square(H, W)
print ""
| 0 | null | 82,424,164,424,842 | 289 | 49 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
N = INT()
A = LIST()
if reduce(gcd, A) != 1:
print("not coprime")
exit()
ma = max(A)
p = list(range(ma+1))
for x in range(2, int(ma ** 0.5) + 1):
if p[x]:
# xは素数,それがpの要素にあるうちは続ける
for i in range(2 * x, ma + 1, x):
p[i] = x
s = [set() for _ in range(N)]
t = set()
for i, x in enumerate(A):
while x != 1:
s[i].add(x)
t.add(x)
# 割り続けるとs[i]にその素数がたまっていく
x //= p[x]
l = sum(len(x) for x in s)
print("pairwise coprime" if l == len(t) else "setwise coprime")
|
s = int(input())
if s%1000 == 0:
c=0
else:
c = 1000 - ( s%1000 )
print(c)
| 0 | null | 6,267,091,720,988 | 85 | 108 |
N=int(input())
S=input()
count=0
for i in range(N):
if S[i]=='A' and i!=N-1:
if S[i+1]=='B' and i!=N-2:
if S[i+2]=='C':
count+=1
print(count)
|
n = input()
print(len(input().split("ABC")) - 1)
| 1 | 99,952,654,698,248 | null | 245 | 245 |
H, W, k = map(int, input().split())
a = [list(input()) for _ in range(H)]
res = [[0] * W for _ in range(H)]
cnt = 0
for h in range(H):
for w in range(W):
if a[h][w] == "#":
cnt += 1
res[h][w] = cnt
for h in range(H):
for w in range(W):
if w > 0 and res[h][w] == 0:
res[h][w] = res[h][w - 1]
for h in range(H):
for w in reversed(range(W)):
if w < W - 1 and res[h][w] == 0:
res[h][w] = res[h][w + 1]
for h in range(H):
for w in range(W):
if h > 0 and res[h][w] == 0:
res[h][w] = res[h - 1][w]
for h in reversed(range(H)):
for w in range(W):
if h < H - 1 and res[h][w] == 0:
res[h][w] = res[h + 1][w]
for i in res:
print(*i)
|
r, c = map(int, raw_input().split())
data = [0 for i in range(r)]
temp = [0 for i in range(c)]
for i in range(r):
data[i] = map(int, raw_input().split())
for i in range(r):
data[i].append(0)
data[i][c] = sum(data[i])
for i in range(c):
for j in range(r):
temp[i] += data[j][i]
temp.append(sum(temp))
data.append(temp)
for i in range(r+1):
print " ".join(map(str, data[i]))
| 0 | null | 72,660,861,393,180 | 277 | 59 |
import numpy as np
N, S = map(int, input().split())
A = tuple(map(int, input().split()))
MOD = 998244353
dp = np.zeros((N + 1, S + 1), dtype=np.int64)
dp[0][0] = 1
for i in range(N):
dp[i + 1] = dp[i] * 2
dp[i + 1, A[i]:] += dp[i, :-A[i]]
dp[i + 1] %= MOD
print(dp[N][S])
|
from collections import deque
K = int(input())
Q = deque()
for i in range(1, 10):
Q.append(i)
temp = 0
for i in range(K):
temp = Q.popleft()
if temp%10 != 0:
Q.append(temp*10+temp%10-1)
Q.append(temp*10+temp%10)
if temp%10 != 9:
Q.append(temp*10+temp%10+1)
print(temp)
| 0 | null | 28,914,099,479,420 | 138 | 181 |
mod = 10**9+7
MAX_N = 10 ** 6
fact = [1]
fact_inv = [0] * (MAX_N + 4)
for i in range(MAX_N + 3):
fact.append(fact[-1] * (i + 1) % mod)
fact_inv[-1] = pow(fact[-1], mod - 2, mod)
for i in range(MAX_N + 2, -1, -1):
fact_inv[i] = fact_inv[i + 1] * (i + 1) % mod
def mod_comb_k(n, k, mod):
return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod
x,y=map(int,input().split())
if (2*x-y)>=0 and (2*y-x)>=0 and (x+y)%3==0:
print(mod_comb_k((x+y)//3,(2*y-x)//3,mod))
else:print(0)
|
intl=map(int, raw_input().split())
a=intl[0]
b=intl[1]
if a==b:
print "a == b"
elif a>b:
print "a > b"
else:
print "a < b"
| 0 | null | 75,134,427,374,578 | 281 | 38 |
a=100
for _ in[0]*int(input()):a=int(a*1.05)+(a%20>0)
print(a*1000)
|
n = input()
s = 100000
for i in range(n):
s = s * 1.05
if s % 1000 != 0:
s = ((s // 1000) + 1) * 1000
print "%d" % s
| 1 | 1,100,292,380 | null | 6 | 6 |
h, w = map(int, input().split())
if h==1 or w == 1:
print(1)
else:
ans = h * w //2 + (h*w)%2
print(ans)
|
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7
h, w = LI()
if h == 1 or w == 1:
print(1)
else:
if (h * w) % 2 == 0:
print(int(h * w / 2))
else:
print(int(h * w / 2 + 1))
| 1 | 50,653,425,440,358 | null | 196 | 196 |
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
a, b = map(list, zip(*ab))
a.sort()
b.sort(reverse=True)
a1, b1 = a[n//2], b[n//2]
a2, b2 = a[n//2-1], b[n//2-1]
if n % 2: print(b1 - a1 + 1)
else: print((b1+b2) - (a1+a2) + 1)
|
N, *AB = map(int, open(0).read().split())
A = sorted(a for a in AB[::2])
B = sorted(b for b in AB[1::2])
if N % 2:
print(B[N // 2] - A[N // 2] + 1)
else:
print(B[N // 2 - 1] + B[N // 2] - A[N // 2 - 1] - A[N // 2] + 1)
| 1 | 17,341,449,254,400 | null | 137 | 137 |
def resolve():
n,k = map(int,input().split())
ans = 0
while n!=0:
n = n//k
ans += 1
print(ans)
resolve()
|
#!/usr/bin/env python3
def main():
N, K = map(int, open(0).read().split())
i = 0
while N != 0:
N = N // K
i += 1
print(i)
main()
| 1 | 64,357,421,830,300 | null | 212 | 212 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.