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
|
---|---|---|---|---|---|---|
# Coding is about expressing your feeling, and
# there is always a better way to express your feeling_feelme
import sys
import math
# sys.stdin=open('input.txt','r')
# sys.stdout=open('output2.txt','w')
from sys import stdin,stdout
from collections import deque,defaultdict
from math import ceil,floor,inf,sqrt,factorial,gcd,log2
from copy import deepcopy
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
mod=int(1e9 + 7)
n=ii1()
if n==1:
print(1)
elif n%2==0:
print(1/2)
else:
print((n//2 +1)/n)
| from decimal import Decimal
N = int(input())
M = int(N/Decimal(1.08)) + 1
if N == int(M*Decimal(1.08)) :
print( int(M))
else :
print(':(') | 0 | null | 150,993,031,329,162 | 297 | 265 |
N=int(input())
k=N%1000
if k==0:
t=0
else:
t=1000-k
print(t) | N,M=map(int, input().split())
a=[0]*(N+1)
w=[0]*(N+1)
for _ in range(M):
p,s=input().split()
p=int(p)
if s=='AC':
a[p]=1
else:
if a[p]==0:
w[p]+=1
a2=0
w2=0
for n in range(N+1):
if a[n]==1:
a2+=a[n]
w2+=w[n]
print(a2, w2)
| 0 | null | 51,104,336,253,770 | 108 | 240 |
while True:
n, x = map(int, input().split())
if n == x == 0:
break
count = 0
for i in range(1,n+1):
for j in range(1,n+1):
for k in range(1,n+1):
if (i < j < k):
if i+j+k == x:
count += 1
print(count) | from itertools import combinations
def main():
while True:
n, x = map(int, input().split())
if (n, x) == (0, 0):
break
ans = 0
for nums in combinations(range(1, n + 1), 3):
if x == sum(nums):
ans += 1
print(ans)
if __name__ == "__main__":
main() | 1 | 1,287,125,174,830 | null | 58 | 58 |
h = int(input())
w = int(input())
n = int(input())
cnt = 0
ans = 0
while ans < n:
if h < w:
ans += w
h -= 1
else:
ans += h
w -= 1
cnt += 1
print(cnt) | H = int(input())
W = int(input())
N = int(input())
X = 0
if H <= W:
X = N // W
if N % W != 0:
X = X + 1
else :
X = N // H
if N % H != 0:
X = X + 1
print(X) | 1 | 89,224,680,114,986 | null | 236 | 236 |
def comb(n,r,m):
if r == 0: return 1
return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)
def mempow(a,b):
temp = 1
yield temp
for i in range(b):
temp = temp * a % 998244353
yield temp
def memfact(a):
temp = 1
yield temp
for i in range(1,a+1):
temp = temp * i % 998244353
yield temp
N,M,K = (int(x) for x in input().split())
memp = []
memf = []
mpappend = memp.append
mfappend = memf.append
for x in mempow(M-1,N-1):
mpappend(x)
for x in memfact(N-1):
mfappend(x)
ans = 0
if M == 1:
if K + 1 < N:
print('0')
else:
print('1')
else:
i = 0
for i in range(K+1):
ans = (ans + (comb(N-1,i,998244353)*M*memp[N-i-1])) % 998244353
print(ans) | n,s = map(int,input().split())
a = list(map(int,input().split()))
if n == 1:
if a[0] == s:
print(1)
quit()
else:
print(0)
quit()
mod = 998244353
dp = [[0]*(s+1) for _ in range(n+1)]
dp[0][0] = 1
for i in range(n):
dp[i+1][0] = 2*dp[i][0]%mod
for j in range(s+1):
dp[i+1][j] = dp[i][j]*2%mod
if a[i] <= s:
for j in range(a[i],s+1):
dp[i+1][j] += dp[i][j-a[i]]
dp[i+1][j] %= mod
print(dp[-1][-1]%mod) | 0 | null | 20,413,034,856,420 | 151 | 138 |
import sys
# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math
# from numba import jit
# from scipy import
# import numpy as np
# import networkx as nx
# import matplotlib.pyplot as plt
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
class SegmentTree:
def __init__(self, array, operator, identity):
self.identity = identity
self.operator = operator
self.array_size = len(array)
self.tree_height = (self.array_size - 1).bit_length()
self.tree_size = 2 ** (self.tree_height + 1)
self.leaf_start_index = 2 ** self.tree_height
self.tree = [self.identity] * self.tree_size
for i in range(self.array_size):
x = 1 << (ord(array[i]) - 96)
self.tree[self.leaf_start_index + i] = x
for i in range(self.leaf_start_index - 1, 0, -1):
self.tree[i] = self.operator(self.tree[i << 1], self.tree[i << 1 | 1])
def update(self, index, val):
x = 1 << (ord(val) - 96)
cur_node = self.leaf_start_index + index
self.tree[cur_node] = x
while cur_node > 1:
self.tree[cur_node >> 1] = self.operator(self.tree[cur_node], self.tree[cur_node ^ 1])
cur_node >>= 1
def query(self, begin, end):
if begin < 0:
begin = 0
elif begin > self.array_size:
return self.identity
if end > self.array_size:
end = self.array_size
elif end < 1:
return self.identity
res = self.identity
left = begin + self.leaf_start_index
right = end + self.leaf_start_index
while left < right:
if left & 1:
res = self.operator(res, self.tree[left])
left += 1
if right & 1:
right -= 1
res = self.operator(res, self.tree[right])
left >>= 1
right >>= 1
return res
def main():
from operator import or_
n = int(input())
s = input()
q = int(input())
seg = SegmentTree(s, or_, 0)
for i in range(q):
q1, q2, q3 = readline().split()
q1, q2 = int(q1), int(q2)
if q1 == 1:
seg.update(q2-1, q3.rstrip("\n"))
else:
q3 = int(q3)
print(bin(seg.query(q2-1, q3)).count("1"))
if __name__ == '__main__':
main()
| b=[]
i=-1
while True:
a=input()
if a=="-":
break
else:
b.append(a)
i+=1
c=int(input())
for j in range(c):
a=int(input())
d=b[i][a:]
b[i]=b[i][0:a]
b[i]=d+b[i]
for i in b:
print(i)
| 0 | null | 32,254,524,037,308 | 210 | 66 |
n, m, k = map(int, input().split())
def bin(a, b, p):
res = 1
while b > 0:
if b & 1 > 0:
res = res * a % p
a = a * a % p
b >>= 1
return res
MOD = 998244353
N = 200000 + 50
f = [0 for i in range(N)]
invf = [0 for i in range(N)]
f[0] = invf[0] = 1
for i in range(1, N):
f[i] = f[i-1] * i % MOD
invf[n] = bin(f[n], MOD-2, MOD)
for ri in range(1, N-1):
i = n - ri
invf[i] = invf[i+1] * (i+1) % MOD
def binom(n, m):
if n < m or n < 0 or m < 0:
return 0
return f[n] * invf[m] % MOD * invf[n-m] % MOD
def g(n, m):
return m * bin(m-1, n-1, MOD)
ans = 0
for i in range(0, k+1):
# print(n-i, i, binom(n-1, i), bin(n-i, m, MOD), n-i, m)
ans = (ans + binom(n-1, i) * g(n-i, m)) % MOD
print(ans) | 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=998244353
n,m,k=map(int,input().split())
g1=[1,1]
g2=[1,1]
inverse=[0,1]
for i in range(2,n):
g1.append((g1[-1]*i)%mod)
inverse.append((-inverse[mod%i]*(mod//i))%mod)
g2.append((g2[-1]*inverse[-1]%mod))
ans=(m*(m-1)**(n-1))%mod
for i in range(1,k+1):
ans +=cmb(n-1,i,mod)*m*pow(m-1,n-1-i,mod)
ans %=mod
print(ans) | 1 | 23,038,935,462,158 | null | 151 | 151 |
a, b, c = map(int, raw_input().split())
counter = 0
for i in xrange(a, b+1):
if c % i == 0:
counter += 1
print counter | N = int(input())
M = [""] * N
T = [0] * N
for i in range(N):
m, t = input().split(" ")
M[i] = m
T[i] = int(t)
X = input()
id = M.index(X)
s = sum(T[id+1:])
print(s) | 0 | null | 48,842,029,067,778 | 44 | 243 |
class Dice(object):
def __init__(self,num):
self.f=num[0]
self.s=num[1]
self.e=num[2]
self.w=num[3]
self.n=num[4]
self.b=num[5]
def create(self,order):
if order=='N':
return [self.s,self.b,self.e,self.w,self.f,self.n]
elif order=='S':
return [self.n,self.f,self.e,self.w,self.b,self.s]
elif order=='E':
return [self.w,self.s,self.f,self.b,self.n,self.e]
else:
return [self.e,self.s,self.b,self.f,self.n,self.w]
@classmethod
def Dice_create(cls,num):
return cls(num)
num=list(map(int,input().split()))
q=eval(input())
Q=[]
dice=Dice(num)
T={0:'N',1:'E',2:'S',3:'W'}
for i in range(q):
Q.append(list(map(int,input().split())))
for i in range(q):
while True:
t=[dice.s,dice.w,dice.n,dice.e]
if dice.f!=Q[i][0]:
if dice.b!=Q[i][0]:#??´??¢????????¢?????\?????????
dice=Dice.Dice_create(dice.create(T[t.index(Q[i][0])]))
else:#?????¢????????¢?????\?????????
dice=Dice.Dice_create(dice.create('N'))
else:
print(t[t.index(Q[i][1])-1])#?????¢?????£???????????¢?????\?????????
break | (X,N) = map(int,input().split())
if N == 0:
print(X)
else:
p = list(map(int,input().split()))
for i in range(N+1):
if (X - i) not in p:
print(X-i)
break
elif(X+i) not in p:
print(X+i)
break | 0 | null | 7,130,754,219,898 | 34 | 128 |
# input here
_INPUT = """\
5 3
1 2
3 4
5 1
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def main():
n, m = map(int, input().split())
xy = [list(map(int, input().split())) for i in range(m)]
uf = UnionFind(n)
for i in range(len(xy)):
uf.union(xy[i][0]-1,xy[i][1]-1)
print(-(min(uf.parents)))
if __name__ == '__main__':
import io
import sys
import math
import itertools
from collections import deque
# sys.stdin = io.StringIO(_INPUT)
main() | class UnionFind():
def __init__(self, n):
self.r = [-1 for _ in range(n)]
def root(self, x):
if self.r[x] < 0:
return x
self.r[x] = self.root(self.r[x])
return self.r[x]
def unite(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return False
if self.r[x] > self.r[y]:
x, y = y, x
self.r[x] += self.r[y]
self.r[y] = x
return True
def size(self, x):
return -self.r[self.root(x)]
n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
a, b = map(int, input().split())
uf.unite(a - 1, b - 1)
max_friend = -min(uf.r)
print(max_friend) | 1 | 3,991,794,304,498 | null | 84 | 84 |
n,a,b = map(int,input().split())
ab = a + b
m = n // ab
n %= ab
print(m*a+min(n,a)) | N=int(input())
S=input()
indices = list(map(ord, S))
indices2 = []
for idx in indices:
if idx + N > 90: indices2.append(idx - 26 + N)
else: indices2.append(idx + N)
S2 = list(map(chr,indices2))
print(''.join(S2)) | 0 | null | 95,213,104,850,956 | 202 | 271 |
import math
r = input()
area = r * r * math.pi * 1.0
cir = 2 * r * math.pi * 1.0
print '%f %f' % (area, cir) | S = input()
days = ['SUN','MON','TUE','WED','THU','FRI','SAT']
i = days.index(S)
print (7-i) | 0 | null | 67,111,406,784,940 | 46 | 270 |
n = int(input())
count = [0 for i in range(n+1)]
for x in range(1, 101):
for y in range(1, 101):
for z in range(1, 101):
f = x ** 2 + y ** 2 + z ** 2 + x*y + y*z + z*x
if f <= n:
count[f] += 1
for i in count[1:]:
print(i) | import sys
txt = sys.stdin.read().lower()
for i in "abcdefghijklmnopqrstuvwxyz":
print(i,":",txt.count(i))
| 0 | null | 4,785,988,706,102 | 106 | 63 |
import sys
x,k,d=map(int,input().split())
x=abs(x)
a=x//d
if a>=k:
ans=x-k*d
elif (k-a)%2==0:
ans=x-d*a
else:
ans=x-d*a-d
print(abs(ans)) | x, k, d = map(int, input().split())
x = abs(x)
kk = x // d
amari = x % d
if kk >= k:
ans = (kk - k) * d + amari
elif (k - kk) % 2 == 1:
ans = abs(amari - d)
else:
ans = amari
print(ans) | 1 | 5,218,786,044,732 | null | 92 | 92 |
S = input()
K = int(input())
curr = S[0]
tmp = ""
T = []
for s in S:
if s == curr:
tmp += s
else:
T.append(tmp)
curr = s
tmp = s
T.append(tmp)
if len(T) == 1:
res = (len(S) * K) // 2
print(res)
exit(0)
res = 0
t_first = T[0]
t_last = T[-1]
if t_first[0] != t_last[0]:
res += (len(t_first) // 2) * K
res += (len(t_last) // 2) * K
else:
res += len(t_first) // 2
res += len(t_last) // 2
res += (len(t_first + t_last) // 2) * (K - 1)
for t in T[1:-1]:
res += (len(t) // 2) * K
print(res)
| def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
from math import gcd
mod = int(1e9+7)
n = ii()
cnd = {}
azero = 0
bzero = 0
allzero = 0
for _ in range(n):
a,b = iim()
if a*b == 0:
if a == 0 and b != 0:
azero += 1
elif a != 0 and b == 0:
bzero += 1
else:
allzero += 1
else:
g = gcd(a,b)
a //= g
b //= g
if a<0:
a *= -1
b *= -1
before = cnd.get((a,b),False)
if b > 0:
check = cnd.get((b,-a),False)
else:
check = cnd.get((-b,a),False)
if before:
cnd[(a,b)] = [before[0]+1,before[1]]
elif check:
if b > 0:
cnd[(b,-a)] = [check[0],check[1]+1]
else:
cnd[(-b,a)] = [check[0],check[1]+1]
else:
cnd[(a,b)] = [1,0]
cnd[0] = [azero,bzero]
noreg = 0
ans = 1
#print(cnd)
for item in cnd.values():
if item[0] == 0 or item[1] == 0:
noreg += max(item[0],item[1])
else:
ans *= (2**item[0]+2**item[1]-1)
ans %= mod
print((ans*((2**noreg)%mod)-1+allzero)%mod) | 0 | null | 97,975,323,300,370 | 296 | 146 |
import numpy as np
def main():
N, K = [int(x) for x in input().split()]
A = [float(x) for x in input().split()]
F = [float(x) for x in input().split()]
A = np.array(sorted(A))
F = np.array(sorted(F, reverse=True))
if K >= np.sum(A)*N:
print(0)
exit()
min_time = 0
max_time = A[-1] * F[0]
while max_time != min_time:
tgt_time = (min_time + max_time)//2
ideal_a = np.floor(tgt_time*np.ones(N)/F)
cost = A - ideal_a
require_k = np.sum(cost[cost > 0])
if require_k <= K:
max_time = tgt_time
else:
min_time = tgt_time+1
print(int(max_time))
if __name__ == "__main__":
main()
| import sys
def solve():
input = sys.stdin.readline
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
F = [int(f) for f in input().split()]
A.sort(reverse = True)
F.sort()
low, high = -1, 10 ** 13
while high - low > 1:
mid = (low + high) // 2
count = 0
for i in range(N):
counterF = mid // F[i]
if counterF < A[i]: count += A[i] - counterF
if count <= K: high = mid
else: low = mid
print(high)
return 0
if __name__ == "__main__":
solve() | 1 | 165,564,194,594,760 | null | 290 | 290 |
import sys
def input(): return sys.stdin.readline().rstrip()
A,B,C,K = map(int,input().split())
point = 0
if A <= K:
point += A
K -= A
else:
point += K
K = 0
if B <= K:
K -= B
else:
K = 0
point -= K
print(point) | a, b, c, k = map(int, input().split())
if k <= a:
print(k)
elif k <= a + b:
print(a)
elif k <= a + b + c:
print(a - (k-a-b))
else:
print(a - c) | 1 | 21,957,667,256,052 | null | 148 | 148 |
from sys import stdin
import sys
import math
from functools import reduce
import itertools
n,m = [int(x) for x in stdin.readline().rstrip().split()]
x = list('a'*n)
for i in range(m):
a, b = [x for x in stdin.readline().rstrip().split()]
a = int(a) - 1
if x[a] == 'a' or x[a] == b:
x[a] = b
else:
print(-1)
sys.exit()
if x[0] == '0' and n != 1:
print(-1)
sys.exit()
if x[0] == 'a' and n > 1: x[0] = '1'
if x[0] == 'a' and n == 1: x[0] = '0'
for i in range(1,n):
if x[i] == 'a': x[i] = '0'
print(''.join(x))
| N, M = map(int, input().split())
st = 1 * 10 ** (N-1)
en = st * 10
if N == 1:
st = 0
SC = []
for i in range(M):
sc = list(map(int,input().split()))
SC.append(sc)
for i in range(st,en):
t = str(i)
judge = True
for j in range(M):
if t[SC[j][0]-1] != str(SC[j][1]):
judge = False
if judge:
print(i)
exit()
print("-1")
| 1 | 60,780,905,667,170 | null | 208 | 208 |
s,e,bai = [int(x) for x in input().split()]
count=0
for i in range(s,e+1):
if i%bai == 0:
count +=1
print(count) | R, C, K = map(int, input().split())
g = [[-float('inf')]*C for _ in range(R)]
for i in range(K):
r, c, k = map(int, input().split())
r -= 1
c -= 1
g[r][c] = k
dp = [[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]
for r in range(R):
for c in range(C):
# k[0..3] = 0個拾っている状態から3個拾っている状態
# indexが大きいほど、小さいアイテムとなるので、小さいものから更新していく
for k in range(2, -1, -1):
dp[k+1][r][c] = max(
dp[k+1][r][c],
dp[k][r][c]+g[r][c]
)
for k in range(4):
dp[0][r+1][c] = max(dp[0][r+1][c], dp[k][r][c])
dp[k][r][c+1] = max(dp[k][r][c+1], dp[k][r][c])
ans = 0
for i in range(4):
ans = max(dp[i][R-1][C-1], ans)
print(ans)
| 0 | null | 6,497,757,560,932 | 104 | 94 |
tmp = (input().split(' '))
n = int(tmp[0])
k = int(tmp[1])
tmp = input().split(' ')
map_tmp = map(int,tmp)
grades = list(map_tmp)
j = k
while j < n:
curr_res = grades[j]
prev_res = grades[j-k]
if curr_res > prev_res:
print('Yes')
else:
print('No')
j+=1 | import sys
input = sys.stdin.readline
H,N = map(int,input().split())
spells = [list(map(int,input().split())) for i in range(N)]
INF = 10**10
dp = [INF]*(H+1)
dp[0] = 0
for use in spells:
damage = use[0]
mp = use[1]
for i in range(1,H+1):
dp[i] = min(dp[max(0,i-damage)] + mp, dp[i])
print(dp[-1])
| 0 | null | 44,213,155,828,362 | 102 | 229 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def input():
return sys.stdin.readline()
def resolve():
s = input().rstrip()
print(s[0:3])
if __name__ == "__main__":
resolve()
| import sys
def input(): return sys.stdin.readline().rstrip()
def main():
a,b,n=map(int, input().split())
if b<=n:
n=b-1
print(((a*n)//b) - (a*(n//b)))
if __name__ == '__main__':
main() | 0 | null | 21,577,457,375,628 | 130 | 161 |
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys
INF = float("inf")
def solve(N: int, M: int, p: "List[int]", S: "List[str]"):
ac = [0 for _ in range(N)]
for i in set(x[0] for x in zip(p, S) if x[1] == "AC"):
ac[i - 1] = 1
wa = 0
for x in zip(p, S):
ac[x[0] - 1] &= x[1] == "WA"
wa += ac[x[0] - 1]
return f'{len(set(x[0] for x in zip(p,S) if x[1] == "AC"))} {wa}'
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
M = int(next(tokens)) # type: int
p = [int()] * (M) # type: "List[int]"
S = [str()] * (M) # type: "List[str]"
for i in range(M):
p[i] = int(next(tokens))
S[i] = next(tokens)
print(f"{solve(N, M, p, S)}")
if __name__ == "__main__":
main()
| x, y = map(int, input().split())
ans = 0
for i in range(x+1):
if 2 * i + 4*(x-i) == y:
ans = 1
print('Yes' if ans == 1 else 'No') | 0 | null | 53,427,940,839,698 | 240 | 127 |
n,k=map(int,input().split())
a=0
b=n
while b>=k:
b=b//k
a+=1
print(a+1)
| n = int(input())
total = 0
for i in range(n):
a,b = map(int, input().split())
if a == b:
total += 1
if total == 3:
break
else:
total = 0
if total == 3:
print('Yes')
else:
print('No') | 0 | null | 33,580,027,781,824 | 212 | 72 |
import math
def modcomb(n, k, mod, fac, ifac):
x = fac[n]
y = ifac[n-k] * ifac[k] % mod
#print(x * y % mod)
return x * y % mod
def makeTables(n, mod):
fac = [1, 1]
ifac = [1, 1]
inv = [0, 1]
for i in range(2, n+1):
fac.append(i * fac[i-1] % mod)
inv.append(-inv[mod % i] * (mod // i) % mod)
ifac.append(inv[i] * ifac[i-1] % mod)
return fac, ifac
def answer(n, k, mod, fac, ifac):
if k >= n:
k = n - 1
ans = 0
for i in range(k+1):
c = modcomb(n, i, mod, fac, ifac) * modcomb(n-1, i, mod, fac, ifac) % mod
ans = (ans + c) % mod
return ans
n, k = map(int, input().split())
mod = 1000000007
fac, ifac = makeTables(n, mod)
print(answer(n, k, mod, fac, ifac))
| a,b,c,k = map(int,input().split())
if k < a:
ans = k
elif k < a + b:
ans = a
else:
ans = a - (k - a - b)
print(ans) | 0 | null | 44,161,508,780,772 | 215 | 148 |
m1,d1 = (int(x) for x in input().split())
m2,d2 = (int(x) for x in input().split())
if d2==1:
print(1)
else:
print(0) | a, b = map(int, input().split())
c, d = map(int, input().split())
if a != c:
ans = 1
else:
ans = 0
print(ans)
| 1 | 124,551,442,844,850 | null | 264 | 264 |
K,N = map(int,input().split())
A1 = list(map(int,input().split()))
D = [0] * (N)
A2 = [A1[i]+K for i in range(len(A1)-1)]
A1 = A1+A2
for i in range(N):
D[i] = A1[i+N-1] - A1[i]
#print(A1[i],A1[i+N-1])
print(min(D)) | 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, groupby, combinations_with_replacement
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
from decimal import Decimal, ROUND_CEILING
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
R, C, K = MAP()
dp = [[0]*4 for _ in range(C+1)]
field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)]
for _ in range(K):
r, c, v = MAP()
field[r][c] = v
for i in range(1, R+1):
dp_next = [[0]*4 for _ in range(C+1)]
for j in range(1, C+1):
up_max = max(dp[j])
p = field[i][j]
dp_next[j][0] = max(up_max, dp[j-1][0])
dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p)
dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p)
dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p)
dp = dp_next
print(max(dp[-1]))
| 0 | null | 24,370,208,287,040 | 186 | 94 |
from copy import copy
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
a_counter=Counter(a)
ans=0
for i in a_counter:
ans+=(a_counter[i])*(a_counter[i]-1)//2
for j in a:
true_ans=copy(ans)
true_ans=true_ans-((a_counter[j])*(a_counter[j]-1)//2-(a_counter[j]-1)*(a_counter[j]-2)//2)
print(true_ans) | from collections import *
from math import *
N = int(input())
A = list(map(int,input().split()))
C = Counter(A)
S = 0
for k,v in C.items():
S+=comb(v,2)
for a in A:
print(S-(comb(C[a],2)-comb(C[a]-1,2))) | 1 | 47,768,302,338,000 | null | 192 | 192 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
n, k, *p = map(int, read().split())
p.sort()
r = sum(p[:k])
print(r)
if __name__ == '__main__':
main()
| # coding: utf-8
n = int(input())
A = list(map(int, input().split()))
print(" ".join(map(str,A)))
for i in range(1,n):
v = A[i]
j = i -1
while j >= 0 and A[j] > v:
A[j+1] = A[j]
j -= 1
A[j+1] = v
print(" ".join(map(str,A)))
| 0 | null | 5,763,701,979,980 | 120 | 10 |
n,m,k=map(int,input().split())
mod=998244353
ans=m*pow(m-1,n-1,mod)
ans%=mod
if k==0:
print(ans)
exit()
lst=[1]+[1]
for i in range(2,n+10):
lst.append((lst[-1]*i)%mod)
def combinations(n,r):
xxx=lst[n]
xxx*=pow(lst[n-r],mod-2,mod)
xxx%=mod
xxx*=pow(lst[r],mod-2,mod)
xxx%=mod
return xxx
for i in range(1,k+1):
ans+=(m*pow(m-1,n-1-i,mod)*combinations(n-1,i))%mod
ans%=mod
print(ans)
| n, m = map(int,input().split())
sc = [list(map(int,input().split())) for _ in range(m)]
ans = -1
for i in range(1000):
s_i = str(i)
if len(s_i) == n and all(int(s_i[s-1])==c for s,c in sc):
ans = i
break
print(ans) | 0 | null | 41,975,732,118,060 | 151 | 208 |
S=input()
if S=='hi' or S=='hihi' or S=='hihihi' or S=='hihihihi'or S=='hihihihihi':
print('Yes')
else:
print('No')
| def main():
h, n = list(map(int, input().split()))
A, B = [], []
for _ in range(n):
a, b = list(map(int, input().split()))
A.append(a)
B.append(b)
mx = max(A)
INF = float('inf')
dp = [INF] * (h + mx + 1)
dp[0] = 0
for i in range(1, h + mx + 1):
for a, b in zip(A, B):
if i - a < 0:
dp[i] = min(dp[i], b)
else:
dp[i] = min(dp[i], dp[i - a] + b)
print(min(dp[h:h + mx + 1]))
if __name__ == '__main__':
main()
| 0 | null | 67,185,704,954,610 | 199 | 229 |
n, m = map(int,input().split())
lst = [[0, 0] for i in range(n)]
for i in range(m):
p, s = map(str,input().split())
p = int(p) - 1
if (lst[p][0] == 0):
if (s == "AC"):
lst[p][0] = 1
else:
lst[p][1] = lst[p][1] + 1
ans = 0
pena = 0
for i in range(n):
if (lst[i][0] == 1):
ans = ans + 1
pena = pena + lst[i][1]
print(ans, pena)
| N, M = map(int, input().split())
S = [[0 for j in range(2)] for i in range(M)]
for i in range(M):
X = input().split()
S[i][0] = int(X[0])
S[i][1] = str(X[1])
A = [0] * N
p = 0
s = 0
for i in range(M):
if S[i][1] == "WA":
if A[S[i][0]-1] != "x":
A[S[i][0]-1] += 1
if S[i][1] == "AC":
if A[S[i][0]-1] != "x":
p += A[S[i][0]-1]
A[S[i][0]-1] = "x"
s += 1
arr = [s, p]
print(*arr) | 1 | 93,471,557,702,758 | null | 240 | 240 |
from sys import stdin
N = int(stdin.readline().rstrip())
if N % 2 == 0:
print(N//2-1)
else:
print(N//2)
| N = int(input())
cnt = 0
for i in range(1,N//2 + 1):
if N - i != i:
cnt += 1
print(cnt) | 1 | 153,715,563,585,778 | null | 283 | 283 |
n = input().split()
n_int = list(map(int,n))
a = n_int[0]
b = n_int[1]
c = n_int[2]
if a < b < c:
print("Yes")
else:
print("No") | import sys
num = map(int, raw_input().split())
if num[0] < num[1]:
if num[1] < num[2]:
print "Yes"
else:
print "No"
else:
print "No" | 1 | 388,012,981,412 | null | 39 | 39 |
def insertion_sort(a, n, g, cnt):
for i in range(g,n,1):
v = a[i]
j = i - g #initial value
while j>=0 and a[j]>v:
a[j+g]=a[j]
j -= g
cnt += 1
a[j+g] = v
return a, cnt
def shell_sort(a,n,m,g):
cnt = 0
for i in range(m):
sorted_list, cnt = insertion_sort(a,n,g[i],cnt)
return sorted_list, cnt
def seqforshell(n):
seq = [1]
next_num = 3*seq[0] + 1
while next_num<n:
seq.insert(0, next_num)
next_num = 3*next_num + 1
return seq
if __name__ == "__main__":
a = []
n = int(input())
for i in range(n):
a.append(int(input()))
g = seqforshell(n)
m = len(g)
res, cnt = shell_sort(a,n,m,g)
print(m)
print(*g)
print(cnt)
for i in range(n):
print(res[i])
| #coding:utf-8
#1_2_D
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 -= g
cnt += 1
A[j+g] = v
def shellSort(A, n):
G = [1]
while True:
g = 3*G[0] + 1
if g > n:
break
G.insert(0, g)
m = len(G)
for i in range(m):
insertionSort(A, n, G[i])
return [m, G]
n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
m, G = shellSort(A, n)
print(m)
print(" ".join(map(str, G)))
print(cnt)
print("\n".join(map(str, A))) | 1 | 32,793,586,960 | null | 17 | 17 |
n = input()
l = map(int, raw_input().split())
max = l[0]
min = l[0]
s = 0
for i in range(n):
if max < l[i]:
max = l[i]
if min > l[i]:
min = l[i]
s = s + l[i]
print min, max, s | N, K = map(int, input().split())
p = list(map(int, input().split()))
cost = 0
for i in range(K):
cost += min(p)
p.remove(min(p))
print(cost) | 0 | null | 6,125,152,961,498 | 48 | 120 |
import sys
x=int(input())
n=1
while(100*n<=x):
if(x<=105*n):
print(1)
sys.exit()
n+=1
print(0)
| import math
h1, m1, h2, m2, k = map(int, input().split(" "))
t1 = h1 * 60 + m1
t2 = h2 * 60 + m2
print(t2 - t1 - k) | 0 | null | 72,363,241,531,910 | 266 | 139 |
n,k=map(int,input().split())
A=[int(input()) for i in range(n)]
def track_num(n):
cnt,track=0,1
for i in A:
if cnt+i>n:
track +=1
cnt=i
else:
cnt +=i
return track
def Binaryserch():
left,right=max(A),sum(A)+1
ans=0
while left<right:
mid=(left+right)//2
track=track_num(mid)
if track<=k:
ans=mid
right=mid
elif track>k:
left=mid+1
return ans
print(Binaryserch())
| import math
n, k = map(int, input().split())
w = [int(input()) for i in range(n)]
def check(P): # 最駄積載量Pのk台のトラックで何個目の荷物まで積めるか。積める荷物の数をreturn
i = 0
for j in range(k):
s = 0
while (s + w[i]) <= P:
s += w[i]
i += 1
if (i==n):
return n
return i
# 今、条件より1<=w<=10,000であり、1<=n<=100000
# つまり、トラックの最大積載量の取りうる最大値はこれらが一つのトラックに乗る時、つまり100000*10000
# 従ってこの範囲でPを探索すればよい
# 二分探索を使うことができる(これらのmidをPと仮定してcheckで積める個数を調べる
#→これがnよりも大きければPはもっと小さくて良いと言うことなのでrightをmidに、小さければleftをmidに!
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
ans = math.floor(solve())
print(ans)
| 1 | 88,874,730,560 | null | 24 | 24 |
a=int(input())
b=0
c=int(a**(1/2))
for i in range(-1000,1000):
for j in range(-1000,1000):
if((i**5)-(j**5)==a):
b=1
break
if(b==1):
break
print(i,j) | X = int(input())
for a in range(-10**3, 10**3):
for b in range(-10**3, 10**3):
if a**5 - b**5 == X:
print(a, b)
exit()
| 1 | 25,547,419,501,728 | null | 156 | 156 |
S = input()
T = input()
min_change = 10**9
for posi_1 in range(len(S)-len(T)+1):
tmp = 0
for posi_2 in range(len(T)):
if S[posi_1+posi_2] != T[posi_2]:
tmp += 1
min_change = min(tmp, min_change)
print(min_change) | s = list(input())
t = list(input())
ans = 1000
sub = 0
flag = False
for i in range(len(s)):
if s[i] != t[sub]:
sub = 0
continue
else:
if sub == len(t) - 1:
print(0)
flag = True
break
sub += 1
if not flag:
for i in range(len(s) - len(t) + 1):
sub = 0
an = 0
for j in range(len(t)):
if s[i + j] != t[sub]:
an += 1
sub += 1
ans = min(an, ans)
print(ans) | 1 | 3,638,852,355,040 | null | 82 | 82 |
n = int(input())
cnt = 0
d1, d2 = [0] * n, [0] * n
for i in range(n):
d1[i], d2[i] = map(int, input().split())
for i in range(n):
if d1[i] == d2[i]:
cnt += 1
else:
cnt = 0
if cnt == 3:
print("Yes")
exit()
print("No") | N=int(input())
ans=0
ke=0
for i in range(N):
D,E=map(int,input().split())
if D==E:
ans+=1
else:
ans=0
if ans>=3:
ke=1
if ke==1:
print("Yes")
else:
print("No") | 1 | 2,484,031,061,346 | null | 72 | 72 |
a,b,n=map(int,input().split())
if n<b:
c=(a*n)//b
print(c)
else:
c=(a*(b-1))//b
print(c) | N,M = map(int,input().split())
S = list(map(int,input()))
S.reverse()
#後ろから貪欲に
a = 0 #合計何マス進んだか
A = [] #何マスずつ進んでいるか
while a < N:
if N-a <= M:
A.append(N-a)
a = N
else:
for i in range(M,0,-1):
if S[a+i] == 0:
A.append(i)
a += i
break
else:
print(-1)
exit()
A.reverse()
print(*A) | 0 | null | 83,706,094,521,688 | 161 | 274 |
N, X, T = (int(x) for x in input().split())
if N % X == 0:
ans = N / X
else:
ans = N // X + 1
ans = T * ans
print("%d" % ans)
| N, X, T = (int(x) for x in input().split() )
a = int(N/X)
if N % X != 0:
t = (a + 1)* T
else:
t = a * T
print(t)
| 1 | 4,225,348,381,892 | null | 86 | 86 |
def main():
n, r = map(int, input().split(" "))
print(r+100*(10-min(10,n)))
if __name__ == "__main__":
main() | def main():
N,R = map(int, input().split())
if N>=10:
print(R)
else:
print(R+100*(10-N))
main()
| 1 | 63,649,760,998,240 | null | 211 | 211 |
S, W = map(int,input().split())
ans = "safe"
if(S<=W):
ans = "unsafe"
print(ans) | import sys
def resolve(in_):
s, w = map(int, next(in_).split())
return 'safe' if s > w else 'unsafe'
def main():
answer = resolve(sys.stdin.buffer)
print(answer)
if __name__ == '__main__':
main() | 1 | 29,164,769,567,318 | null | 163 | 163 |
n,a,b = map(int,input().split())
if a < b:
a,b = b,a
if (a - b) %2 == 0:
print((a-b)//2)
else:
print(min(a-1,n-b)+1+(b-a)//2) | import sys
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
n, a, b = MI()
d = b - a
ans = 0
if d%2:
temp = min(a-1, n-b)
ans = (d-1)//2 + temp + 1
else:
ans = d//2
print(ans)
if __name__ == '__main__':
main() | 1 | 109,009,224,574,270 | null | 253 | 253 |
N,K=map(int,input().split())
R,S,P=map(int,input().split())
L=input()
T=[]
for i in range(N):
T.append(L[i])
for i in range(K,N):
if T[i]==T[i-K]:
T[i]="0"
p=0
for i in T:
if i=="r":
p+=P
elif i=="s":
p+=R
elif i=="p":
p+=S
print(p) | # D - Friend Suggestions
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def root(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.root(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def same(self, x, y):
return self.root(x) == self.root(y)
def size(self, x):
return -self.parents[self.root(x)]
N,M,K = map(int,input().split())
friend = UnionFind(N+1)
block = [1]*(N+1)
for _ in range(M):
x,y = map(int,input().split())
block[x] += 1
block[y] += 1
friend.union(x,y)
for _ in range(K):
x,y = map(int,input().split())
if friend.same(x,y):
block[x] += 1
block[y] += 1
ans = [friend.size(i)-block[i] for i in range(1,N+1)]
print(*ans) | 0 | null | 84,258,397,425,312 | 251 | 209 |
a, b = map(int, input().split())
if a <= 9 and b <= 9:
print(a * b)
else:
print("-1") | import sys
input = sys.stdin.buffer.readline
R, C, K = map(int, input().split())
G = [[None for j in range(C)] for i in range(R)]
for _ in range(K):
r, c, v = map(int, input().split())
G[r - 1][c - 1] = v
dp = [[0, 0, 0, 0] for _ in range(C)]
for i in range(R):
for j in range(C):
if G[i][j] is not None:
dp[j][0], dp[j][1] = max(dp[j]), max(dp[j]) + G[i][j]
dp[j][2], dp[j][3] = 0, 0
else:
dp[j][0] = max(dp[j])
dp[j][1], dp[j][2], dp[j][3] = 0, 0, 0
for j in range(1, C):
if G[i][j] is not None:
dp[j][0] = max(dp[j - 1][0], dp[j][0])
dp[j][1] = max(dp[j - 1][1], dp[j - 1][0] + G[i][j], dp[j][1])
if dp[j - 1][1] != 0:
dp[j][2] = max(dp[j - 1][2], dp[j - 1][1] + G[i][j], dp[j][2])
if dp[j - 1][2] != 0:
dp[j][3] = max(dp[j - 1][3], dp[j - 1][2] + G[i][j], dp[j][3])
else:
dp[j][0] = max(dp[j - 1][0], dp[j][0])
dp[j][1] = max(dp[j - 1][1], dp[j][1])
dp[j][2] = max(dp[j - 1][2], dp[j][2])
dp[j][3] = max(dp[j - 1][3], dp[j][3])
print(max(dp[-1])) | 0 | null | 81,655,858,177,568 | 286 | 94 |
n=int(input())
s=input()
flag=0
ans=0
for si in s:
if si=='A':
flag=1
elif si=='B' and flag==1:
flag=2
elif si=='C' and flag==2:
ans+=1
flag=0
else:
flag=0
print(ans)
| n = input()
print(len(input().split("ABC")) - 1) | 1 | 99,417,307,336,820 | null | 245 | 245 |
n = int(input())
x = list(map(int, input().split()))
mini = min(x)
maxi = max(x)
summ = sum(x)
print(mini, maxi, summ) | S = input()
N = len(S)
A = [int(S[i]) for i in range(N)]
A = A[::-1]
MOD = 2019
p10 = [1] * N
for i in range(1, N):
p10[i] = (p10[i - 1] * 10) % MOD
for i in range(N):
A[i] = (A[i] * p10[i]) % MOD
cumsum = [A[0]] * N
for i in range(1, N):
cumsum[i] = (cumsum[i - 1] + A[i]) % MOD
cnt = [0] * MOD
cnt[0] = 1
for i in range(N):
cnt[cumsum[i]] += 1
ans = 0
for i in range(MOD):
ans += cnt[i] * (cnt[i] - 1) // 2
print(ans)
| 0 | null | 15,789,498,553,570 | 48 | 166 |
N, M = map(int, input().split())
H = list(map(int, input().split()))
ans = M
counted = []
appeared = set()
for i in range(M):
a, b = map(int, input().split())
if H[a-1] < H[b-1]:
appeared.add(a)
elif H[a-1] > H[b-1]:
appeared.add(b)
else:
appeared.add(a)
appeared.add(b)
ans = len(H) - len(appeared)
print(ans)
| n,m = map(int,input().split())
graph = [[] for _ in range(n)]
h = list(map(int,input().split()))
for _ in range(m):
a,b = map(int,input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
cnt = 0
for i in range(n):
if graph[i] == []:
cnt += 1
continue
if max([h[j] for j in graph[i]]) < h[i] : cnt += 1
print(cnt) | 1 | 24,985,674,239,538 | null | 155 | 155 |
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
B = [0 for i in range(10**5+1)]
S = [0]
s = 0
for i in range(N):
B[A[i]] += 1
s += A[i]
S.append(s)
for i in range(2,10**5+2):
B[-i] += B[-i+1]
d = 0
u = 2*10**5
for i in range(30):
x = (u+d)//2
if i == 28:
x += 1
m = 0
for i in range(N):
a = A[i]
y = max(x - a,0)
if y <= 10**5:
m += B[y]
if m >= M:
d = x
else:
u = x
ans = 0
for i in range(N):
a = A[i]
x = max(d - a,0)
if x <= 10**5:
y = B[x]
else:
y = 0
ans += (S[N] - S[N-y]) + a*y
ans -= (m-M)*d
print(ans) | h,w,k = map(int,input().split())
ls = [str(input()) for _ in range(h)]
a = [[] for _ in range(2**h)]
b = [[] for _ in range(2**w)]
for i in range(2**h):
for j in range(h):
if (i>>j)&1:
a[i].append(j)
for s in range(2**w):
for l in range(w):
if (s>>l)&1:
b[s].append(l)
p = 0
for e in range(2**h):
for f in range(2**w):
cnt = 0
for g in range(len(ls)):
if g not in a[e]:
di = list(ls[g])
for t in range(len(di)):
if t not in b[f]:
if di[t] == "#":
cnt += 1
if cnt == k:
p += 1
print(p) | 0 | null | 58,855,211,067,620 | 252 | 110 |
import sys
def ISS(): return sys.stdin.readline().rstrip().split()
s,t =ISS()
print(t+s)
| S = list(map(str, input().split()))
print(S[1] + S[0]) | 1 | 102,821,022,217,548 | null | 248 | 248 |
N = int(input())
X = list(map(int,input().split()))
cmin = 10**6+10
for i in range(101):
cnt = 0
for j in range(N):
cnt += (i-X[j])**2
cmin = min(cmin,cnt)
print(cmin) | _, lis = input(), list(map(int, input().split()))
print("{} {} {}".format(min(lis), max(lis), sum(lis)))
| 0 | null | 33,215,160,965,572 | 213 | 48 |
card = []
check = [[0, 0, 0] for i in range(3)]
for i in range(3):
card.append(list(map(int, input().split())))
n = int(input())
for i in range(n):
b = int(input())
for j in range(3):
for k in range(3):
if b == card[j][k]:
check[j][k] = 1
flag = 0
for i in range(3):
if check[i][0] == check[i][1] == check[i][2] == 1:
flag = 1
break
elif check[0][i] == check[1][i] == check[2][i] == 1:
flag = 1
break
elif check[0][0] == check[1][1] == check[2][2] == 1:
flag = 1
break
elif check[0][2] == check[1][1] == check[2][0] == 1:
flag = 1
break
if flag:
print('Yes')
else:
print('No') | def LIHW(h):
return [list(map(int, input().split())) for _ in range(h)]
def LIH(h):
return [int(input()) for _ in range(h)]
card = LIHW(3)
N = int(input())
num = LIH(N)
bingo = [[0, 0, 0] for _ in range(3)]
for i in num:
for a in range(3):
for b in range(3):
if i == card[a][b]:
bingo[a][b] = 1
break
ans = "No"
for i in range(3):
if bingo[i][0] == 1 and bingo[i][1] == 1 and bingo[i][2] == 1:
ans = "Yes"
for i in range(3):
if bingo[0][i] == 1 and bingo[1][i] == 1 and bingo[2][i] == 1:
ans = "Yes"
if bingo[0][0] == 1 and bingo[1][1] == 1 and bingo[2][2] == 1:
ans = "Yes"
if bingo[0][2] == 1 and bingo[1][1] == 1 and bingo[2][0] == 1:
ans = "Yes"
print(ans)
| 1 | 59,934,472,481,858 | null | 207 | 207 |
N,M = map(int, input().split())
C = list(map(int, input().split()))
C.sort()
dp = [[float("inf") for _ in range(N+1)] for _ in range(M+1)]
dp[0][0] = 0
#i番目までのコインを使えるときに、j円を作る場合の、コインの最小枚数を求める
for i in range(M):
for j in range(N+1):
# i番目を使えない場合
if C[i] > j:
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[-1][-1])
| def f(num):
mn = 20000
for y in c:
if y > num/2:
break
mn = min(mn, yen[y]+yen[num-y])
return mn
n, m = map(int, input().split())
c = list(map(int, input().split()))
c = sorted(c)
yen = [0 for i in range(n+1)]
yen[1] = 1
for num in range(2, n+1):
if num in c:
yen[num] = 1
else:
yen[num] = min(yen[num-1]+1,f(num))
print(yen[n])
| 1 | 141,727,104,148 | null | 28 | 28 |
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()
def main():
h,w,k=LI()
field=[list(S()) for _ in range(h)]
ans=[[0]*w for _ in range(h)]
l=[]
f=False
y1=0
x1=0
for i in range(h):
for j in range(w):
if field[i][j]=='#':
if f:
l.append([y1,x1,i-1,w-1])
y1=i
x1=0
f=True
break
l.append([y1,x1,h-1,w-1])
# print(l)
l2=[]
while len(l)>0:
y1,x1,y2,x2=l.pop()
st=0
f=False
for j in range(x1,x2+1):
for i in range(y1,y2+1):
if field[i][j]=='#':
if f:
l2.append([y1,st,y2,j-1])
st=j
f=True
break
l2.append([y1,st,y2,x2])
cnt=1
while len(l2)>0:
y1,x1,y2,x2=l2.pop()
for i in range(y1,y2+1):
for j in range(x1,x2+1):
ans[i][j]=cnt
cnt+=1
for x in ans:
print(' '.join([str(y) for y in x]))
main()
# print(main())
| H,W,s=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_h=[0]*H
for i in range(H):
if "#" in set(l[i]):
l_h[i]=1
cnt=0
bor=sum(l_h)-1
for i in range(H):
if cnt==bor:
break
if l_h[i]==1:
l_h[i]=-1
cnt+=1
cnt=1
import numpy as np
tmp=[]
st=0
ans=np.zeros((H,W))
for i in range(H):
tmp.append(l[i])
if l_h[i]==-1:
n_tmp=np.array(tmp)
s_count=np.count_nonzero(n_tmp=="#")-1
for j in range(W):
ans[st:i+1,j]=cnt
if "#" in n_tmp[:,j] and s_count>0:
cnt+=1
s_count-=1
st=i+1
cnt+=1
tmp=[]
n_tmp=np.array(tmp)
s_count=np.count_nonzero(n_tmp=="#")-1
for j in range(W):
ans[st:i+1,j]=cnt
if "#" in n_tmp[:,j] and s_count>0:
cnt+=1
s_count-=1
for i in ans:
print(*list(map(int,i))) | 1 | 143,557,821,395,940 | null | 277 | 277 |
n = int(input())
a = list(map(int, input().split()))
ans = sum(a)
ans2 = 0
for i in range(n):
if abs((ans - a[i]) - (ans2 + a[i])) < abs(ans - ans2):
ans2 += a[i]
ans -= a[i]
else:
break
print(abs(ans - ans2))
| n = int(input())
a = [int(i) for i in input().split()]
b = []
sum_a = sum(a)
cum = 0
for i in a:
cum += i
b.append(abs(sum_a - cum * 2))
print(min(b)) | 1 | 142,173,318,557,050 | null | 276 | 276 |
'''
Created on 2020/08/20
@author: harurun
'''
def main():
import sys
pin=sys.stdin.readline
pout=sys.stdout.write
perr=sys.stderr.write
H=int(pin())
W=int(pin())
N=int(pin())
t=max(H,W)
if N%t==0:
print(N//t)
else:
print(N//t+1)
return
main() | a = int(input())
b = int(input())
n = int(input())
t = max(a, b)
print((n + t - 1) // t) | 1 | 89,175,630,955,680 | null | 236 | 236 |
from scipy.sparse import*
f=csgraph.johnson
n,m,l,*t=map(int,open(0).read().split())
m*=3
[*map(print,f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n)%n-1)] |
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np
import sys
def input():
return sys.stdin.readline().strip()
N, M, L = list(map(int, input().split()))
graph = np.full((N, N), np.inf)
for i in range(M):
A, B, C = list(map(int, input().split()))
graph[A - 1, B - 1] = C
graph[B - 1, A - 1] = C
graph = csr_matrix(graph)
dist = floyd_warshall(graph, directed=False)
graph2 = np.full((N, N), np.inf)
for i in range(N):
for j in range(N):
if dist[i, j] <= L:
graph2[i, j] = 1
graph2 = csr_matrix(graph2)
num = shortest_path(graph2, directed=False)
Q = int(input())
for q in range(Q):
s, t = list(map(int, input().split()))
if num[s - 1][t - 1] < 10000000:
print(int(num[s - 1][t - 1] - 1))
else:
print(-1)
| 1 | 173,771,248,189,838 | null | 295 | 295 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
n, m = map(int, input().split())
graph = [0] + [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
from collections import deque
def bfs(graph, queue, dist, prev):
while queue:
current = queue.popleft()
for _next in graph[current]:
if dist[_next] != -1:
continue
else:
dist[_next] = dist[current] + 1
prev[_next] = current
queue.append(_next)
queue = deque([1])
dist = [0] + [-1] * n
prev = [0] + [-1] * n
dist[1] = 0
bfs(graph, queue, dist, prev)
if -1 in dist:
print('No')
else:
print('Yes')
for v in prev[2:]:
print(v)
| def e_handshake():
import numpy as np
N, M = [int(i) for i in input().split()]
A = np.array(input().split(), np.int64)
A.sort()
def shake_cnt(x):
# 幸福度が x 以上となるような握手を全て行うときの握手の回数
# 全体から行わない握手回数を引く
return N**2 - np.searchsorted(A, x - A).sum()
# 幸福度の上昇が right 以上となるような握手はすべて行い、
# left となるような握手はいくつか行って握手回数が M 回になるようにする
left = 0
right = 10 ** 6
while right - left > 1:
mid = (left + right) // 2
if shake_cnt(mid) >= M:
left = mid
else:
right = mid
# 幸福度が right 以上上がるような握手をすべて行ったとして、回数と総和を計算
X = np.searchsorted(A, right - A) # 行わない人数
Acum = np.zeros(N + 1, np.int64) # 累積和で管理する
Acum[1:] = np.cumsum(A)
happiness = (Acum[-1] - Acum[X]).sum() + (A * (N - X)).sum()
# 幸福度が right 未満の上昇となる握手を、握手回数が M になるまで追加で行う
shake = N * N - X.sum()
happiness += (M - shake) * left
return happiness
print(e_handshake()) | 0 | null | 64,477,860,114,658 | 145 | 252 |
n = int(input())
f = 100000
if n == 0:
print(int(f))
else:
for i in range(n):
f *= 1.05
#print(f)
#F = f - f%1000 + 1000
if f%1000 != 0:
f = (f//1000)*1000 + 1000
#print(i,f)
print(int(f))
| d,t,s = map(int, input().split())
if d/s > t: print('No')
else: print('Yes') | 0 | null | 1,805,738,977,042 | 6 | 81 |
import math
a, b, c = list(map(int, input().split()))
sa, sb, sc = math.sqrt(a), math.sqrt(b), math.sqrt(c)
d = c-a-b
if (c-a-b > 0) and ((c-a-b)**2 > 4*a*b):
print("Yes")
else:
print("No")
| n = int(input())
s = input()
t = s[:len(s)//2]
if 2*t == s:
print("Yes")
else:
print("No") | 0 | null | 98,974,192,594,562 | 197 | 279 |
n , k , s = map(int,input().split())
l = []
for i in range(k):
l.append(s)
if s == 1:
t = 3
elif s == 10**9:
t = 1
else:
t = s + 1
for i in range(n-k):
l.append(t)
print(' '.join(map(str, l))) | N, K, S = map(int, input().split())
a = [S for i in range(K)]
if S == 10 ** 9:
S -= 2
for i in range(N - K):
a.append(S + 1)
print(*a)
| 1 | 91,485,104,421,920 | null | 238 | 238 |
n,d= map(int, input().split())
xy = [list(map(int,input().split())) for _ in range(n)]
c= 0
for x,y in xy:
if x**2+y**2 <= d**2:
c += 1
print(c) | import math
N,D = map(int,input().split())
X = [None]*N
Y = [None]*N
for i in range(N):
X[i],Y[i] = map(int,input().split())
ans=0
for i in range(N):
if(math.sqrt(X[i]**2+Y[i]**2) <=D):
ans+=1
print(ans)
| 1 | 5,885,311,883,780 | null | 96 | 96 |
n = input()
l = list(map(int, str(n)))
dp = [[0] * 2 for _ in range(len(l)+1)]
dp[0][1] = 1
for i, v in enumerate(l):
dp[i+1][0] = min(dp[i][0] + v, dp[i][1] + (10 - v))
dp[i+1][1] = min(dp[i][0] + v + 1, dp[i][1] + (10 - v) - 1)
print(dp[-1][0]) | import sys
input = sys.stdin.readline
def read():
S = input().strip()
return S,
def solve(S):
N = len(S)
ans = 0
for i in range(N//2):
if S[i] != S[N-i-1]:
ans += 1
return ans
if __name__ == '__main__':
inputs = read()
print(solve(*inputs))
| 0 | null | 95,539,799,411,808 | 219 | 261 |
N = int(input())
ans = [0]*(1+N)
for x in range(1, 10**2+1):
for y in range(1, 10**2+1):
for z in range(1, 10**2+1):
v = x*x+y*y+z*z+x*y+y*z+z*x
if v<=N:
ans[v] += 1
for i in range(1, N+1):
print(ans[i]) | from collections import defaultdict
N = int(input())
d = defaultdict(int)
for x in range(1,100):
for y in range(x,100):
for z in range(y,100):
n = x**2+y**2+z**2+x*y+y*z+z*x
if x == y == z:
d[n] += 1
elif x == y or y == z or z == x:
d[n] += 3
else:
d[n] += 6
if n > N:
break
for i in range(1,N+1):
print(d[i])
| 1 | 8,010,098,537,112 | null | 106 | 106 |
import sys
sys.setrecursionlimit(10**7)
import fileinput
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
for line in sys.stdin:
x, y = map(int, line.split())
print(gcd(x,y),lcm(x,y))
| 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 | 567,242,260 | null | 5 | 5 |
# 28
# ALDS_11_C - 幅優先探索
from collections import deque
n = int(input())
u_l = []
k_l = []
v_l = []
for i in range(n):
_v_l = []
for j, _ in enumerate(input().split()):
if j == 0:
u_l.append(int(_))
elif j == 1:
k_l.append(int(_))
else:
_v_l.append(int(_))
v_l.append(_v_l)
d_l = [-1]*n
d_l[0] = 0
s_dq = deque([1])
while len(s_dq) > 0:
_s = s_dq.popleft()
_s -= 1
for i in range(k_l[_s]):
_v = v_l[_s][i]
if d_l[_v-1] == -1:
s_dq.append(_v)
d_l[_v-1] = d_l[_s] + 1
for _id, _d in zip(range(1,n+1), d_l):
print(_id, _d)
| from collections import deque
INF = 1e9
def BFS(n, adj):
d = [INF] * N
d[0] = 0
next_v = deque([0]) # ?¬??????????????????????
while next_v:
u = next_v.popleft()
for v in adj[u]:
if d[v] == INF:
d[v] = d[u] + 1
next_v.append(v)
return d
N = int(input())
Adj = [None] * N
for i in range(N):
u, k, *vertex = [int(i)-1 for i in input().split()]
Adj[u] = vertex
distance = BFS(N, Adj)
for i, di in enumerate(distance, start = 1):
if di == INF:
print(i, -1)
else:
print(i, di) | 1 | 3,781,142,810 | null | 9 | 9 |
n,m = list(map(int, input("").split()))
h = list(map(int, input("").split()))
p = [1] * n
for i in range(m):
a,b = map(int, input().split())
if h[a-1] <= h[b-1]:
p[a-1]=0
if h[a-1] >= h[b-1]:
p[b-1]=0
print(sum(p)) | n, m = map(int, input().split())
arr = [0] + list(map(int, input().split()))
g = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
ans = 0
for i in range(1, n + 1):
for j in g[i]:
if arr[j] >= arr[i]:
break
else:
ans += 1
print(ans)
| 1 | 25,129,871,525,152 | null | 155 | 155 |
import math
import sys
readline = sys.stdin.readline
def main():
n = int(readline().rstrip())
if n % 2 == 1:
print(0)
else:
n = n // 2
num = 0
for i in range(1, 100):
num += n // (5 ** i)
print(num)
if __name__ == '__main__':
main()
| cnt = 0
coefficients = [1]
n = 1
_list = []
length = int(input())
for i in range(length):
try:
_list.append(int(input()))
except EOFError:
continue
for i in range(1,length):
if i < n:
continue
coefficients.append(i)
n = i*2.25 + 1
for c in coefficients[::-1]:
for i in range(length):
val = _list[i]
j = i - c
while j >= 0 and _list[j] > val:
_list[j + c] = _list[j]
j -= c
cnt += 1
_list[j+c] = val
print(len(coefficients))
print(*coefficients[::-1])
print(cnt)
print(*_list, sep='\n') | 0 | null | 58,354,390,835,520 | 258 | 17 |
h, w, k = map(int, input().split())
c = []
ans = 0
for i in range(h):
c_i = list(input())
c.append(c_i)
for i in range(1 << h):
for j in range(1 << w):
cnt = 0
for ii in range(h):
for jj in range(w):
if (i >> ii & 1):
continue
if (j >> jj & 1):
continue
if c[ii][jj] == "#":
cnt += 1
if cnt == k:
ans += 1
print(ans) | h,w,p=map(int,input().split())
A=[]
ans=0
for i in range(h):
A.append(list(input()))
for i in range(2**h):
hs=[False]*h
for j in range(h):
if ((i>>j)&1):
hs[j]=True
for k in range(2**w):
ws=[False]*w
for l in range(w):
if ((k>>l)&1):
ws[l]=True
cnt=0
for a in range(h):
if hs[a]==True:
for b in range(w):
if ws[b]==True:
if A[a][b]=='#':
cnt+=1
if cnt==p:
ans+=1
print(ans) | 1 | 8,958,406,805,192 | null | 110 | 110 |
import sys
s = input()
def match(a):
if a[0:2] == 'hi':
return a[2:]
else:
print('No')
sys.exit()
if len(s)%2 == 1:
print('No')
sys.exit()
else:
while len(s) > 1:
s = match(s)
print('Yes') | from collections import defaultdict
from collections import deque
n,m,k = map(int,input().split())
f_set = {tuple(map(int,input().split())) for _ in range(m)}
b_set = {tuple(map(int,input().split())) for _ in range(k)}
friends_counts = {key:[] for key in range(1,n+1)}
blocks_counts = {key:[] for key in range(1,n+1)}
for f in f_set:
friends_counts[f[0]].append(f[1])
friends_counts[f[1]].append(f[0])
for b in b_set:
blocks_counts[b[0]].append(b[1])
blocks_counts[b[1]].append(b[0])
friends_groups_list = [set() for _ in range(n+1)]
#dfs
que = deque()
groups_count = 0
checked_dict = {key:0 for key in range(1,n+1)}
#que.appendleft(1)
for i in range(1,n+1):
if checked_dict[i] == 1:
continue
que.appendleft(i)
checked_dict[i] = 1
while que:
x = que.popleft()
friends_groups_list[groups_count].add(x)
for i in range(len(friends_counts[x])):
if checked_dict[friends_counts[x][i]] == 0:
que.appendleft(friends_counts[x][i])
checked_dict[friends_counts[x][i]] = 1
groups_count += 1
result_list=[0]*n
#print(blocks_counts)
#print(friends_groups_list)
for i in range(len(friends_groups_list)):
mini_set = friends_groups_list[i]
for ms in mini_set:
result_list[ms-1] = len(mini_set) - 1 - len(friends_counts[ms])
block_counter_in_minilist = 0
for k in blocks_counts[ms]:
if k in mini_set:
block_counter_in_minilist += 1
result_list[ms-1] -= block_counter_in_minilist
print(" ".join(map(str,result_list)))
#cの配列の解釈が違ったらしい。。。
#f_set = {tuple(map(int,input().split())) for _ in range(m)}
#
#b_set = {tuple(map(int,input().split())) for _ in range(k)}
#
#c_list = [0] * (n-1)
#
#result_dict = {key:0 for key in range(1,n+1)}
#
#for f in f_set:
# if abs(f[0]-f[1]) == 1:
# c_list[min(f[0],f[1]) - 1] = 1
# #ここでelseで飛び石での友達のsetを作ってもよいが、そもそもsetなのでinでの探索にそんなに時間かからないし、いったんこのままやってみる。
#
#for start in range(0,n-2):
# if c_list[start] == 0:
# #c[start]が1になるまで飛ばす
# continue
#
# for end in range(start+1,n-1):
# if c_list[end] == 0:
# #友人同士ではないペアまできたらstartをインクリメント
# break
#
# #もし「友人候補」の候補者が、「友人関係でない」かつ「ブロック関係でない」ことをチェックしている。
# if not (start+1,end+2) in f_set and not (end+2,start+1) in f_set and not (start+1,end+2) in b_set and not (end+2,start+1) in b_set:
# result_dict[start+1] += 1
# result_dict[end+2] += 1
#
#for i in range(1,n+1):
# print(result_dict[i])
#
#print(c_list)
| 0 | null | 57,474,551,006,858 | 199 | 209 |
n=int(input())
a=("")
for _ in range(n):
a=a+"ACL"
print(a) | def main():
n, mod = map(int, input().split())
s = input().rstrip()
a = [int(c)%mod for c in s]
ans = 0
if mod == 2 or mod == 5:
for i in range(n):
if a[i] == 0:
ans += i + 1
else:
mul = 10 % mod
for i in reversed(range(n-1)):
a[i] = (a[i]*mul + a[i+1]) % mod
mul = mul*10 % mod
b = [0] * mod
b[0] = 1
for i in reversed(range(n)):
ans += b[a[i]]
b[a[i]] += 1
print(ans)
if __name__ == "__main__":
main() | 0 | null | 30,333,269,694,170 | 69 | 205 |
def main():
n, k = map(int, input().split())
p_lst = list(map(int, input().split()))
lst = []
for i in range(n):
lst.append((p_lst[i] + 1) / 2)
tmp_sum = sum(lst[:k])
maximum = tmp_sum
for i in range(n - k):
tmp_sum -= lst[i]
tmp_sum += lst[i + k]
maximum = max(maximum, tmp_sum)
print(maximum)
if __name__ == '__main__':
main() | import sys
from collections import deque
n = int(input())
q = deque()
for i in range(n):
c = sys.stdin.readline()[:-1]
if c[0] == 'i':
q.appendleft(c[7:])
elif c[6] == ' ':
try:
q.remove(c[7:])
except:
pass
elif c[6] == 'F':
q.popleft()
else:
q.pop()
print(*q) | 0 | null | 37,461,951,084,350 | 223 | 20 |
N=int(input())
a=0
ans=float('inf')
x=list(map(int,input().split()))
for i in range(100):
for j in range(N):
a=a+(x[j]-i-1)**2
#print(a)
ans=min(ans,a)
a=0
print(ans)
| # -*- coding: utf-8 -*-
N = int(input())
ans ='No'
for x in range(1, 10, 1):
if (N % x) == 0:
for y in range(x, 10, 1):
if x * y == N:
ans = 'Yes'
break
if ans == 'Yes':
break
print(ans) | 0 | null | 112,606,960,781,380 | 213 | 287 |
S = int(raw_input())
print '%d:%d:%d' %(S/3600,(S/60)%60,S%60) | n = input()
print "%d:%d:%d" % (n/3600,(n/60)%60,n%60) | 1 | 327,423,402,220 | null | 37 | 37 |
def main(n,m,s):
#i初期化
i = n
tmp = []
while i > 0 :
for j in range(m,-1,-1):
if j == 0 :
i = -1
break
if i-j >= 0 :
if s[i-j] == '0':
i -= j
tmp.append(j)
break
if i == 0:
for l in reversed(tmp):
print(l,end=' ')
else:
print(-1)
n,m = map(int,input().split())
s = input()
main(n,m,s)
| import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N, M = mapint()
S = list(input())
def solve():
now = N
choice = []
while 1:
if now==0:
return choice[::-1]
for m in range(M, 0, -1):
nx = now-m
if nx<0: continue
if S[nx]=='1': continue
now = nx
choice.append(m)
break
else:
return [-1]
print(*solve()) | 1 | 138,574,573,794,580 | null | 274 | 274 |
h, w = map(int, input().split())
S = []
for _ in range(h):
s = str(input())
S.append(s)
DP = [[0 for _ in range(w)] for _ in range(h)]
if S[0][0] == '.':
DP[0][0] = 0
else:
DP[0][0] = 1
ren = False
if DP[0][0] == 1:
ren = True
for i in range(1,h):
if S[i][0] == '.':
DP[i][0] = DP[i-1][0]
ren = False
elif ren == False and S[i][0] == '#':
DP[i][0] = DP[i-1][0] + 1
ren = True
elif ren == True and S[i][0] == '#':
DP[i][0] = DP[i-1][0]
ren = True
ren = False
if DP[0][0] == 1:
ren = True
for j in range(1,w):
if S[0][j] == '.':
DP[0][j] = DP[0][j-1]
ren = False
elif ren == False and S[0][j] == '#':
DP[0][j] = DP[0][j-1] + 1
ren = True
elif ren == True and S[0][j] == '#':
DP[0][j] = DP[0][j-1]
ren = True
for i in range(1,h):
for j in range(1,w):
if S[i][j] == '.':
DP[i][j] = min(DP[i-1][j], DP[i][j-1])
elif S[i][j] == '#':
res_i = 0
res_j = 0
if S[i-1][j] == '.':
res_i = 1
if S[i][j-1] == '.':
res_j = 1
DP[i][j] = min(DP[i-1][j] + res_i, DP[i][j-1] + res_j)
print(DP[h-1][w-1]) | H, W = map(int, input().split())
S = [[i for i in input()] for j in range(H)]
dp = [[0 for i in range(W)] for j in range(H)]
if S[0][0] == "#":
dp[0][0] = 1
for i in range(1, H):
if S[i-1][0] == "." and S[i][0] == "#":
dp[i][0] = dp[i-1][0] + 1
else:
dp[i][0] = dp[i-1][0]
for j in range(1, W):
if S[0][j-1] == "." and S[0][j] == "#":
dp[0][j] = dp[0][j-1] + 1
else:
dp[0][j] = dp[0][j-1]
for h in range(1, H):
for w in range(1, W):
if S[h][w] == "#":
if S[h-1][w] == ".":
c1 = dp[h-1][w] + 1
else:
c1 = dp[h-1][w]
if S[h][w-1] == ".":
c2 = dp[h][w-1] + 1
else:
c2 = dp[h][w-1]
dp[h][w] = min(c1, c2)
else:
dp[h][w] = min(dp[h-1][w], dp[h][w-1])
print(dp[H-1][W-1]) | 1 | 49,538,822,507,900 | null | 194 | 194 |
n = int(input())
if n == 2 * int(n / 2):
p = 1 / 2
else:
p = (n + 1) / (2 * n)
print(p) | def main():
x,k,d = map(int,input().split(" "))
if abs(x) % d == 0:
exceed_num = (abs(x) // d)
else:
exceed_num = (abs(x) // d) + 1
if k <= exceed_num :
print(abs( abs(x) - (d*k) ))
else:
if (k - exceed_num) % 2 == 0:
print(abs( abs(x) - (d*exceed_num) ))
else:
print(abs( abs(x) - (d*(exceed_num-1) )))
if __name__ == '__main__':
main() | 0 | null | 90,969,415,041,404 | 297 | 92 |
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n):
for j in range(i + 1, n):
if j * 2 - i >= n:
continue
if s[i] == s[j] or s[j] == s[2 * j - i] or s[i] == s[2 * j - i]:
continue
ans -= 1
print(ans)
| n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
res = r*g*b
for i in range(n):
for j in range(n):
jj = i+j
k = jj+j
if k >= n: break
if s[i]!=s[jj] and s[jj]!=s[k] and s[k]!=s[i]:
res-=1
print(res)
| 1 | 36,266,840,443,648 | null | 175 | 175 |
def num_divisors_table(n):
table = [0] * (n + 1)
for i in range(1, n + 1):
for j in range(i, n + 1, i):
table[j] += j
return table
n=int(input())
print(sum(num_divisors_table(n))) | def f(x):
return x * (x - 1) // 2
n = int(input())
a = list(map(int, input().split()))
counter = [0] * n
for ai in a:
ai -= 1
counter[ai] += 1
m = 0
for c in counter:
m += f(c)
for i, ai in enumerate(a):
ai -= 1
c = counter[ai]
if c == 0:
print(m)
else:
print(m - f(c) + f(c - 1)) | 0 | null | 29,327,013,073,272 | 118 | 192 |
s = input()
N = len(s)+1
res = []
ans = 0
i = 0
while i < N-1:
seq = 1
while i < N - 2 and s[i] == s[i + 1]:
i += 1
seq += 1
res.append(seq)
i += 1
if s[0] == '>':
ans += res[0] * (res[0] + 1) // 2
res = res[1:]
if s[-1] == '<':
ans += res[-1] * (res[-1]+1)//2
res = res[:-1]
for i in range(len(res) // 2):
tmp = max(res[2 * i], res[2 * i + 1])
tmp2 = min(res[2 * i], res[2 * i + 1])
ans += tmp * (tmp + 1) // 2
ans += tmp2 * (tmp2-1) //2
print(ans) | import sys
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
sys.setrecursionlimit(10**6)
INF = 10**20
def mint():
return map(int,input().split())
def lint():
return map(int,input().split())
def judge(x, l=['Yes', 'No']):
print(l[0] if x else l[1])
S = input()
N = len(S)+1
L = [0]*N
R = [0]*N
for i in range(N-1):
L[i+1] = L[i]+1 if S[i]=='<' else 0
for i in range(N-2,-1,-1):
R[i] = R[i+1]+1 if S[i]=='>' else 0
ans = [max(l,r) for l,r in zip(L,R)]
print(sum(ans)) | 1 | 155,780,096,046,510 | null | 285 | 285 |
N=int(input())
List = list(map(int, input().split()))
Row = int(input())
QList = []
for i in range (Row):
QList.append(list(map(int, input().split())))
dictA ={}
res = 0
for i in range (N):
dictA.setdefault(List[i],0)
dictA[List[i]] += 1
res += List[i]
num = 0
for i in range(Row):
if QList[i][0] not in dictA:
pass
else:
num = dictA[QList[i][0]]
res = res - dictA[QList[i][0]] * QList[i][0]
dictA[QList[i][0]] = 0
dictA.setdefault(QList[i][1],0)
dictA[QList[i][1]] += num
res += QList[i][1] * num
print(res) | from collections import Counter
N = int(input())
A = list(map(int,input().split()))
C_A = Counter(A)
S = 0
for ca in C_A:
S += ca * C_A[ca]
Q = int(input())
for q in range(Q):
b, c = map(int,input().split())
S += (c - b) * C_A[b]
C_A[c] += C_A[b]
C_A[b] = 0
print(S)
| 1 | 12,106,908,797,850 | null | 122 | 122 |
#!/usr/bin/env python
def main():
N = int(input())
D = list(map(int, input().split()))
ans = 0
for i in range(N-1):
d1 = D[i]
for d2 in D[i+1:]:
ans += d1 * d2
print(ans)
if __name__ == '__main__':
main()
| N = int(input())
S = str(input())
num = []
for _ in range(10):
num.append([])
for i in range(N):
num[int(S[i])].append(i)
ans = 0
for X1 in range(0, 10):
for X2 in range(0, 10):
for X3 in range(0, 10):
X = str(X1) + str(X2) + str(X3)
if num[X1] != [] and num[X2] != [] and num[X3] != []:
if num[X1][0] < num[X2][-1]:
for i in num[X2]:
if num[X1][0] < i < num[X3][-1]:
ans += 1
break
print(ans)
| 0 | null | 148,694,554,704,980 | 292 | 267 |
def main():
def check(p):
# 積めた荷物の数
i = 0
# トラックの数だけ試行
for _ in range(k):
# 現在のトラックの重量
s = 0
while s + w[i] <= p:
# 積める場合は積んで次の荷物へ
# 積めない場合は次のトラックへ
s += w[i]
i += 1
if i == n:
return n
return i
def solve():
left, right = 0, 100000 * 10000 + 1
while left < right:
mid = (left + right) // 2
if n <= check(mid):
right = mid
else:
left = mid + 1
return left
n, k = [int(i) for i in input().split()]
w = [int(input()) for _ in range(n)]
ans = solve()
print(ans)
if __name__ == '__main__':
main()
| def check(P):
i=0
for _ in[0]*k:
s=0
while s+w[i]<=P:
s+=w[i];i+=1
if i==n: return n
return i
n,k=map(int,input().split())
w=[int(input())for _ in[0]*n]
l=0;r=n*100000
while r-l>1:
m=(l+r)//2
if check(m)>=n:r=m
else:l=m
print(r)
| 1 | 88,845,497,172 | null | 24 | 24 |
from collections import deque
def main():
d = deque()
for _ in range(int(input())):
command = input()
if command[0] == 'i':
d.appendleft(command[7:])
elif command[6] == ' ':
try:
d.remove(command[7:])
except ValueError:
pass
elif len(command) == 11:
d.popleft()
else:
d.pop()
print(*d)
if __name__ == '__main__':
main()
| n = int(input())
a = list(map(int,input().split()))
m0 = 0
m1 = 0
m2 = 0
if n%2 == 0:
for i,ai in enumerate(a):
if i%2 == 0:
m0 += ai
else:
m1 = max(m0,m1+ai)
print(max(m0,m1))
else:
for i,ai in enumerate(a):
if i%2 == 0:
if i > 0:
m2 = max(m2+ai,m1,m0)
if i < n-1:
m0 += ai
else:
m1 = max(m0,m1+ai)
print(max(m0,m1,m2))
| 0 | null | 18,778,820,038,482 | 20 | 177 |
from collections import defaultdict, Counter
from heapq import heapify, heappop, heappush
from sys import stdin
def main():
n = int(input())
a = [int(x) for x in input().split()]
if n % 2 == 0:
if n == 2:
print(max(a))
return
v = [[None for j in range(2)] for i in range(n)]
v[0][0] = a[0]
v[1][1] = a[1]
for i in range(2, n):
for j in range(2):
c = []
if v[i-2][j] is not None:
c.append(v[i-2][j])
if j >= 1 and i >= 3:
if v[i-3][j-1] is not None:
c.append(v[i-3][j-1])
if len(c) == 0:
continue
v[i][j] = max(c) + a[i]
c = [v[n-1][j] for j in range(2) if v[n-1][j] is not None]
print(max(c))
else:
if n == 3:
print(max(a))
return
v = [[None for j in range(3)] for i in range(n)]
v[0][0] = a[0]
v[1][1] = a[1]
v[2][0] = a[0]+a[2]
v[2][2] = a[2]
for i in range(3, n):
for j in range(3):
c = []
if v[i-2][j] is not None:
c.append(v[i-2][j])
if j >= 1:
if v[i-3][j-1] is not None:
c.append(v[i-3][j-1])
if j >= 2 and i >= 4:
if v[i-4][j-2] is not None:
c.append(v[i-4][j-2])
if len(c) == 0:
continue
v[i][j] = max(c) + a[i]
c = [v[n-1][j] for j in range(1, 3) if v[n-1][j] is not None]
print(max(c))
def input(): return stdin.readline().rstrip()
main()
| #基本1つ飛ばしだが、パスも使える。
#奇数ならパス2回、偶数ならパス1回。
N = int(input())
A = [int(hoge) for hoge in input().split()]
DP = [[0]*3 for n in range(N)] #DP[n][y] = n桁目までみて、パスをy回使ったときの最大値
DP[0][0] = A[0]
for n in range(1,N):#n桁目までみる。
#「選ぶ」か「パスを使う」かの2択。
DP[n][0] = DP[n-2][0] + A[n]
DP[n][1] = max((DP[n-2][1]+A[n],DP[n-1][0]))
DP[n][2] = max((DP[n-2][2]+A[n],DP[n-1][1]))
if N%2:
print(DP[n][2])
else:
print(DP[n][1]) | 1 | 37,156,124,002,180 | null | 177 | 177 |
h0, w0, k0 = [ int(x) for x in input().split() ]
found, buf = 0, {}
for i in range(h0): buf[1 << i] = { 1 << j for j,x in enumerate(input()) if x == '#' }
for i in range(1, 1 << h0):
idx = {}
for k,vs in buf.items():
if k & i == 0: continue
for v in vs: idx[v] = idx.get(v, 0) + 1
for j in range(1, 1 << w0):
n = sum([ v for k,v in idx.items() if k & j ])
if n == k0: found += 1
print(found) | S = input()
ans = {'ABC': 'ARC', 'ARC': 'ABC'}
print(ans[S]) | 0 | null | 16,404,890,625,120 | 110 | 153 |
n = int(input())
def solve1(n: int) -> str:
k = 0
for i in range(len(n)):
k += int(n[i])
if k % 9 == 0:
return "Yes"
else:
return "No"
def solve2(n: int) -> str:
if n % 9 == 0:
return "Yes"
else:
return "No"
print(solve2(n))
| print('No') if int(input())%9 else print('Yes') | 1 | 4,371,778,424,160 | null | 87 | 87 |
import sys
def input(): return sys.stdin.readline().strip()
def resolve():
n,r=map(int, input().split())
if n>=10:
print(r)
else:
print(r+100*(10-n))
resolve() | n,k=list(map(int,input().split()))
if(n>=10):
print(int(k))
else:
rating =k + 1000 - 100*n
print(rating) | 1 | 63,361,559,161,732 | null | 211 | 211 |
n,a,b=map(int,input().split())
if abs(a-b)%2==0:
print(abs(a-b)//2)
else:
print(min(n-max(a,b)+1,min(a,b))+(abs(a-b)-1)//2) | import sys
def input(): return sys.stdin.readline().strip()
def resolve():
n,a,b=map(int, input().split())
if (abs(a-b))%2==0:
print((abs(a-b))//2)
else:
ue=max(a,b)-1
sita=n-min(a,b)
hokasita=((n-max(a,b))*2+1+abs(b-a))//2
hokaue=(((min(a,b))-1)*2+1+abs(b-a))//2
print(min(ue,sita,hokasita,hokaue))
resolve()
| 1 | 109,441,193,276,220 | null | 253 | 253 |
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())
ans=0
for j in range(1,n+1):
y = n//j
ans += y*(y+1)*j//2
print(ans) | 1 | 11,042,872,135,098 | null | 118 | 118 |
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def pre(s):
n = len(s)
pi = [0] * n
for i in range(1, n):
j = pi[i - 1]
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi
def prod(a):
ans = 1
for each in a:
ans = (ans * each)
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def solve(h, w, k, board):
ans = 0
for row in range(1 << h):
for col in range(1 << w):
count = 0
for i in range(h):
for j in range(w):
if (row >> i) & 1 == 0:
continue
if (col >> j) & 1 == 0:
continue
if board[i][j] == '#':
count += 1
if count == k:
ans += 1
return ans
h, w, k = map(int, input().split())
board = []
for i in range(h):
board += [list(input())]
print(solve(h, w, k, board))
| H,W,K=map(int,input().split())
S=[list(input()) for _ in range(H)]
ans=0
for H_mask in range(2**H):
for W_mask in range(2**W):
cnt=0
for i in range(H):
for j in range(W):
if (H_mask>>i)&1==0 and (W_mask>>j)&1==0\
and S[i][j]=='#':
cnt+=1
if cnt==K:
ans+=1
print(ans)
| 1 | 8,869,643,736,498 | null | 110 | 110 |
while True:
s = input()
if s == "-":
exit()
m = int(input())
for i in range(m):
h = int(input())
s = s[h:] + s[:h]
print(s)
| while True:
chk_list = input()
if chk_list == "-":
break
num = int(input())
for i in range(num):
n = int(input())
chk_list = chk_list[n:] + chk_list[0:n]
print(chk_list)
| 1 | 1,934,155,768,732 | null | 66 | 66 |
K = int(input())
S = list(input())
if len(S) <= K:
print(''.join(S))
else:
del S[K:len(S)]
S = ''.join(S) + '...'
print(S) | import sys
sys.setrecursionlimit(10**9)
def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))
INF=10**20
def main():
N,K=mi()
n = N//K
ans = INF
for m in [n-1,n,n+1]:
ans = min(ans,abs(N-K*m))
print(ans)
if __name__ == "__main__":
main() | 0 | null | 29,467,173,983,142 | 143 | 180 |
a, b, x = map(int, input().split())
if x > (a**2)*b/2:
t = 2*((a**2)*b-x)/(a**3)
else:
t = a*(b**2)/(2*x)
import math
ans = math.degrees(math.atan(t))
print(ans)
| import math
a, b, x = map(int, input().split())
s = x / a
if s >= a * b / 2:
c = (a * b - s) * 2 / a
rad = math.atan2(c, a)
else:
c = s * 2 / b
rad = math.atan2(b, c)
ans = math.degrees(rad)
print(ans)
| 1 | 163,097,983,379,940 | null | 289 | 289 |
s=input()
mozisuu=len(s)
def hanteiki(s):
hantei=True
mozisuu=len(s)
for i in range(len(s)):
if s[i]!=s[mozisuu-i-1]:
hantei=False
return hantei
if hanteiki(s):
if hanteiki(s[0:(mozisuu-1)//2]):
if hanteiki(s[(mozisuu+3)//2-1:]):
print("Yes")
exit()
print("No") | import sys
def merge(A, left, mid, right):
n1 = mid - left
n2 = right - mid
L = []
R = []
for i in range(n1):
L.append(A[left+i])
for i in range(n2):
R.append(A[mid+i])
L.append("INFTY")
R.append("INFTY")
i = 0
j = 0
for k in range(left, right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
global c
c += 1
def mergeSort(A, left, right):
if left+1 < right:
mid = (left + right) / 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
if __name__ == "__main__":
lines = sys.stdin.readlines()
N = int(lines[0])
nums = [int(n) for n in lines[1].split(" ")]
c = 0
mergeSort(nums, 0, N)
print " ".join(map(str, nums))
print c | 0 | null | 23,326,788,146,880 | 190 | 26 |
#
import sys
input=sys.stdin.readline
def main():
A,B,C=map(int,input().split())
if (A==B or B==C or C==A) and not A==B==C:
print("Yes")
else:
print("No")
if __name__=="__main__":
main()
| nums = tuple(map(int, input().split()))
s = set(nums)
if len(s) == 2:
print('Yes')
else:
print("No")
| 1 | 68,286,290,091,292 | null | 216 | 216 |
x = int(input())
def sanjo(x):
return x**3
print(sanjo(x)) | x = raw_input()
x = int(x)
print x * x * x | 1 | 281,717,955,440 | null | 35 | 35 |
mod = 1000000007
n = int(input())
ans = pow(10, n, mod) - 2*pow(9, n, mod) + pow(8, n, mod)
print(ans % mod)
| n = int(input())
mod = 7+10**9
a = 10**n % mod
b = 9**n % mod
c = 8**n % mod
print((a - 2*b + c)%mod) | 1 | 3,164,795,806,468 | null | 78 | 78 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.