code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def main():
N = I()
res = 0
for i in range(N):
a,b = MI()
if a==b:
res+=1
if res==3:
print("Yes")
exit()
else:
res=0
print("No")
if __name__ == "__main__":
main()
|
#K = input()
NR = list(map(int, input().split()))
#s = input().split()
#N = int(input())
if (NR[0] >= 10):
print(NR[1])
else:
print(NR[1] + 100 * (10 - NR[0]))
| 0 | null | 32,952,594,869,372 | 72 | 211 |
n = int(input())
dic_A = set()
dic_C = set()
dic_G = set()
dic_T = set()
for i in range(n) :
p, string = input().split()
if p == 'insert' :
if string[0] == 'A' :
dic_A.add(string)
elif string[0] == 'C' :
dic_C.add(string)
elif string[0] == 'G' :
dic_G.add(string)
else :
dic_T.add(string)
else :
if string[0] == 'A' :
if string in dic_A :
print('yes')
else :
print('no')
elif string[0] == 'C' :
if string in dic_C :
print('yes')
else :
print('no')
elif string[0] == 'G' :
if string in dic_G :
print('yes')
else :
print('no')
else :
if string in dic_T :
print('yes')
else :
print('no')
|
MI = lambda :(map(int,input().split()))
x = int(input())
if x>1799:
print(1)
elif x>1599:
print(2)
elif x>1399:
print(3)
elif x>1199:
print(4)
elif x>999:
print(5)
elif x>799:
print(6)
elif x>599:
print(7)
else:
print(8)
| 0 | null | 3,391,075,118,102 | 23 | 100 |
from sys import stdin
input = lambda : stdin.readline().strip()
ri = lambda : int(input())
ril = lambda : list(map(int, input().split()))
from collections import defaultdict
N = ri()
A = ril()
Q = ri()
s = sum(A)
d = defaultdict(int)
for a in A:
d[a] += 1
ans = []
for i in range(Q):
B, C = ril()
s += (C - B) * d[B]
d[C] += d[B]
d[B] = 0
ans.append(str(s))
print('\n'.join(ans))
|
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a) # はじめの合計、名前がsumだとsum関数とかぶってよくないので
# q回操作します。ループ変数は使わないので_とします(Pythonの慣習)
for _ in range(q):
before, after = map(int, input().split())
# 合計を変更します
sum_res -= before * counter[before]
sum_res += after * counter[before]
# 個数を変更します
counter[after] += counter[before]
counter[before] = 0
print(sum_res)
| 1 | 12,177,776,983,480 | null | 122 | 122 |
N = int(input())
S = input()
ans = ''
Z_s = 'Z'
for i in range(len(S)):
ord_s = ord(S[i]) + N
if ord_s > ord(Z_s):
ord_s -= ord('Z') - ord('A') + 1
ans += chr(ord_s)
print(ans)
|
import sys
input = sys.stdin.readline
from string import ascii_uppercase
letters = ascii_uppercase + ascii_uppercase
shift = int(input())
s = input()[:-1]
for char in s: print(letters[letters.index(char) + shift], end = '')
print()
| 1 | 134,704,660,279,262 | null | 271 | 271 |
a,b,c=map(int,input().split(' '))
a,b,c=c,a,b
print(str(a)+" "+str(b)+" "+str(c))
|
#!/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()
| 0 | null | 26,430,127,600,800 | 178 | 130 |
str = input()
lstr = list(str)
num = int(input())
for i in range(num):
order = input().split()
if (order[0] == "print"):
onum = list(map(int,order[1:3]))
for i in range(onum[0],onum[1]+1):
print(lstr[i],end = "")
print("")
elif (order[0] == "reverse"):
onum = list(map(int,order[1:3]))
if (onum[0] == 0):
tmp1 = lstr[0:onum[1]+1]
tmp1.reverse()
tmp2 = lstr[onum[1]+1:]
lstr = tmp1 + tmp2
else:
tmp1 = lstr[:onum[0]]
tmp2 = lstr[onum[0]:onum[1]+1]
tmp2.reverse()
tmp3 = lstr[onum[1]+1:]
lstr = tmp1 + tmp2 +tmp3
elif (order[0] == "replace"):
onum = list(map(int,order[1:3]))
restr = list(order[3])
for i in range(onum[0],onum[1]+1):
lstr[i] = restr[i-onum[0]]
|
s = input()
n = int(input())
for i in range(n):
command = input().split()
if command[0] == 'replace':
a,b = map(int,command[1:3])
s = s[:a]+ command[3] + s[b+1:]
elif command[0] == 'reverse':
a,b = map(int,command[1:3])
s = s[0:a] + s[a:b+1][::-1] + s[b+1:]
elif command[0] == 'print':
a,b = map(int,command[1:3])
print(s[a:b+1])
| 1 | 2,093,988,748,160 | null | 68 | 68 |
n=int(input())
l=[input().split() for i in range(n)]
x=input()
time=0
for i in range(n):
time += int(l[i][1])
if l[i][0]==x:
time=0
print(time)
|
def gcd(x, y):
if x < y:
x, y = y, x
while y != 0:
x, y = y, x % y
return x
if __name__ == "__main__":
x, y = list(map(int, input().split()))
print(gcd(x, y))
| 0 | null | 48,519,156,986,520 | 243 | 11 |
# 高橋君の夏休みが N 日間
# 夏休みの宿題が M個出されており、i 番目の宿題をやるには A i日間かかる
# 条件 複数の宿題を同じ日にはできない。
# また宿題をやる日は遊べない。
# 夏休み中に全ての宿題を終わらせるとき、最大何日遊ぶことができるか。
# 終わらない時は-1
N, M = map(int, input().split())
A = list(map(int, input().split()))
# 宿題にかかる合計日数
play_day = N - sum(A)
if play_day < 0:
print("-1")
else:
print(play_day)
|
# coding: utf-8
num = int(input())
count = 0
str = input().split()
table = [int(i) for i in str]
#全探索
for j in range(num): #ループ1
for k in range(j+1, num): #ループ2
for l in range(k+1, num): #ループ3
if table[j] != table[k] and table[k] != table[l] and table[j] != table[l]:
len = table[j] + table[k] + table[l] #周の長さ
ma = max(table[j], max(table[k], table[l])) #一番長い辺
rest = len - ma #残りの2辺の合計
if ma < rest: #最大の辺が残り2辺より小さければ三角形は成立する
count += 1
print(count)
| 0 | null | 18,461,230,252,240 | 168 | 91 |
n,m=[int(j) for j in input().split()]
c=[int(j) for j in input().split()]
dp=[10**18]*(n+1)
dp[0]=0
for i in c:
for j in range(i,n+1):
dp[j]=min(dp[j],dp[j-i]+1)
print(dp[-1])
|
S = input()
S_inv = S[-1::-1]
counter = 0
for i in range(len(S)//2):
if S[i]!=S_inv[i]:
counter +=1
print(counter)
| 0 | null | 59,810,078,706,290 | 28 | 261 |
def solve(string):
_, *a = map(int, string.split())
return str((sum(a)**2 - sum(map(lambda x: x**2, a))) // 2 % (10**9 + 7))
if __name__ == '__main__':
import sys
print(solve(sys.stdin.read().strip()))
|
A,B,C,K = map(int,input().split())
ans = 0
if K > A:
ans += A
K -= A
if K > B:
ans += 0
K -= B
if K > 0:
ans -= K
else:
ans += 0
else:
ans = K
print(ans)
| 0 | null | 12,878,278,845,362 | 83 | 148 |
import math
def primes(N):
p = [False, False] + [True] * (N - 1)
for i in range(2, int(N ** 0.5) + 1):
if p[i]:
for j in range(i * 2, N + 1, i):
p[j] = False
return [i for i in range(N + 1) if p[i]]
N = int(input())
A = list(map(int, input().split()))
maxA = max(A) + 1
g, Ac = A[0], [0] * maxA
P = primes(maxA)
for a in A:
Ac[a] += 1
ans = "pairwise"
for p in P:
if sum(Ac[p:maxA:p]) > 1:
ans = "setwise"
for a in A:
g = math.gcd(g, a)
print(ans if g == 1 else "not", "coprime")
|
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
n,k = list(map(int, input().split()))
# C,Pを求める前処理
m = 4*10**5
mod = 10**9 + 7
fact = [0]*(m+5)
fact_inv = [0]*(m+5)
inv = [0]*(m+5)
fact[0] = fact[1] = 1
fact_inv[0] = fact_inv[1] = 1
inv[1] = 1
for i in range(2,m+5):
fact[i] = fact[i-1] * i % mod
inv[i] = mod - inv[mod % i] * (mod // i) % mod
fact_inv[i] = fact_inv[i-1] * inv[i] % mod
# nCkをmod(素数)で割った余りを求める.ただしn<10**7
# 前処理はm=n+5まで
def cmb(n,k,mod):
return fact[n] * (fact_inv[k] * fact_inv[n-k] % mod) % mod
if k >= n-1:
print(cmb(2*n-1, n-1, mod)%mod)
else:
ans = 0
for i in range(0,k+1):
ans += cmb(n,i,mod)*cmb(n-1,n-i-1,mod)%mod
ans %= mod
print(ans)
| 0 | null | 35,547,826,055,200 | 85 | 215 |
import itertools
N, M, Q = map(int, input().split())
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
a[i], b[i], c[i], d[i] = map(int, input().split())
cc = [i for i in range(1, M+1)]
ans = 0
for A in itertools.combinations_with_replacement(cc, N):
A = list(A)
t = 0
for i in range(Q):
if A[b[i]-1] - A[a[i]-1] == c[i]:
t += d[i]
ans = max(ans, t)
print(ans)
|
inputNum = []
for i in range(0, 10):
inputNum.append(int(raw_input()))
inputNum.sort(reverse=True)
for i in range(0, 3):
print inputNum[i]
| 0 | null | 13,881,114,794,592 | 160 | 2 |
#!/usr/bin/env python3
import sys
import collections as cl
def II(): 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 = input()
print("No" if N[0] == N[1] == N[2] else "Yes")
main()
|
# 158 A
S = input()
print('No') if S == 'AAA' or S == 'BBB' else print('Yes')
| 1 | 54,471,175,350,588 | null | 201 | 201 |
S = input()
arr = [0]*(len(S) + 1)
cn = 0
for i in range(len(S)):
if S[i] == "<":
cn += 1
else:
cn = 0
arr[i+1] = cn
cn = 0
for i in range(len(S)-1, -1, -1):
if S[i] == ">":
cn += 1
else:
cn = 0
arr[i] = max(arr[i], cn)
# print(arr)
print(sum(arr))
|
n,m=map(int,input().split())
print("Yes" if n<=m else "No")
| 0 | null | 119,927,058,885,622 | 285 | 231 |
n = int(raw_input())
a = map(int, raw_input().split())
a_ = sorted(a)
sum = 0
for e in a_:
sum += e
print "%d %d %d" %(a_[0], a_[-1], sum)
|
n, m = map(int, input().split())
a = list(map(int, input().split()))
s = int(sum(a))
if n > s:
print(n-s)
elif n == s:
print(0)
else:
print(-1)
| 0 | null | 16,343,078,059,700 | 48 | 168 |
from collections import deque
def init_tree(x, par):
for i in range(x + 1):
par[i] = i
def find(x, par):
q = deque()
q.append(x)
while len(q) > 0:
v = q.pop()
if v == par[v]:
return v
q.append(par[v])
def union(x, y, par, rank):
px, py = find(x, par), find(y, par)
if px == py:
return
if rank[px] < rank[py]:
par[px] = py
return
elif rank[px] == rank[py]:
rank[px] += 1
par[py] = px
n, m, k = map(int, input().split())
par = [0] * (n + 1)
rank = [0] * (n + 1)
init_tree(n, par)
eg = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
union(a, b, par, rank)
eg[a].append(b)
eg[b].append(a)
for _ in range(k):
a, b = map(int, input().split())
eg[a].append(b)
eg[b].append(a)
xs = [0] * (n + 1)
ys = [0] * (n + 1)
for i in range(1, n + 1):
p = find(i, par)
xs[p] += 1
for v in eg[i]:
if p == find(v, par):
ys[i] += 1
ans = [-1] * (n + 1)
for i in range(1, n + 1):
ans[i] += xs[find(i, par)] - ys[i]
ans = [-1] * (n + 1)
for i in range(1, n + 1):
ans[i] += xs[find(i, par)] - ys[i]
print(*ans[1:])
|
N,M,K=map(int,input().split())
par=[0]*(N+1)
num=[0]*(N+1)
group = [1]*(N+1)
for i in range(1,N+1): par[i]=i
def root(x):
if par[x]==x: return x
return root(par[x])
def union(x,y):
rx = root(x)
ry = root(y)
if rx==ry: return
par[max(rx,ry)] = min(rx,ry)
group[min(rx,ry)] += group[max(rx,ry)]
def same(x,y):
return root(x)==root(y)
for _ in range(M):
a,b=map(int,input().split())
union(a,b)
num[a]+=1
num[b]+=1
for _ in range(K):
c,d=map(int,input().split())
if same(c,d):
num[c]+=1
num[d]+=1
for i in range(1,N+1):
print(group[root(i)]-num[i]-1,end=" ")
| 1 | 61,835,507,356,740 | null | 209 | 209 |
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse = True)
num = 0
for i in A:
num += i
if A[M-1] >= num/(4*M):
print('Yes')
else:
print('No')
|
n,m = map(int,input().split())
A = list(map(int,input().split()))
s = sum(A)
d = s * (1/(4*m))
t = sum(a >= d for a in A)
print('Yes' if t >= m else 'No')
| 1 | 38,737,333,660,102 | null | 179 | 179 |
def num():
from sys import stdin
h, n = map(int, input().split())
magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
INF = float('inf')
ans = [INF]*(h+1)
ans[-1] = 0
for i in range(h, 0, -1):
if ans[i] != INF:
for j, k in magic:
if i-j < 0:
num = ans[i]+k
if ans[0] > num:
ans[0] = num
else:
num = ans[i]+k
if ans[i-j] > num:
ans[i-j] = num
return ans[0]
print(num())
|
#In[]:
x = int(input())
money = 100
year = 0
while True:
money += money//100
year += 1
if money >= x:
print(year)
break
| 0 | null | 53,877,515,671,590 | 229 | 159 |
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
n = inn()
a = inl()
if n<=3:
print(max(a))
exit()
lacc = [0]*n
racc = [0]*n
lacc[0] = a[0]
lacc[1] = a[1]
racc[n-1] = a[n-1]
racc[n-2] = a[n-2]
for i in range(2,n):
if i%2==0:
lacc[i] = lacc[i-2]+a[i]
else:
lacc[i] = max(lacc[i-3],lacc[i-2])+a[i]
if n%2==0:
print(max(lacc[n-2],lacc[n-1]))
exit()
for i in range(n-3,-1,-1):
if (n-1-i)%2==0:
racc[i] = racc[i+2]+a[i]
else:
racc[i] = max(racc[i+3],racc[i+2])+a[i]
mx = max(racc[2],lacc[n-3])
for i in range(1,n-2):
if i%2==0:
mx = max(mx, max(lacc[i-2],lacc[i-1])+racc[i+2])
else:
mx = max(mx, max(racc[i+2],racc[i+3])+lacc[i-1])
print(mx)
|
import math
def isPrime( x ):
if 2 == x or 3 == x:
return True
if 0 == x&1:
return False
i = 3
limit = math.sqrt( x )
while i <= limit:
if 0 == x%i:
return False
i += 2
return True
n = int( raw_input( ) )
nums = []
i = 0
while i < n:
nums.append( int( raw_input( ) ) )
i += 1
cnt = i = 0
for val in nums:
if isPrime( val ):
cnt += 1
print( cnt )
| 0 | null | 18,715,845,306,840 | 177 | 12 |
"""
Given N, D, A, N means no. of monsters, D means distances of bombs, A attack of the bombs
Find the minimum of bombs required to win.
Each monster out of N pairs - pair[0] = position, pair[1] = health
"""
from collections import deque
mod = 1e9+7
def add(a, b):
c = a + b
if c >= mod:
c -= mod
return c
def main():
N, D, A = [int(x) for x in raw_input().split()]
D *= 2
monsters = []
for _ in range(N):
pos, health = [int(x) for x in raw_input().split()]
monsters.append([pos, health])
monsters.sort(key = lambda x : x[0])
remain_attacks_queue = deque([])
remain_attack = 0
ans = 0
for monster in monsters:
# monster[0] - pos, monster[1] - health
# queue[0] - position, queue[1] - attack points
while len(remain_attacks_queue) and monster[0] - D > remain_attacks_queue[0][0]:
remain_attack -= remain_attacks_queue.popleft()[1]
if remain_attack < monster[1]:
remained_health = monster[1] - remain_attack
times = remained_health / A if remained_health % A == 0 else remained_health // A + 1
#print(times)
attack = times * A
remain_attacks_queue.append([monster[0], attack])
ans += times
remain_attack += attack
print(ans)
main()
|
H, W, m = map(int, input().split())
H_bomb = [0]*H
W_bomb = [0]*W
S = set()
for _ in range(m):
h, w = map(int, input().split())
h, w = h-1, w-1
S.add(tuple([h, w]))
H_bomb[h] += 1
W_bomb[w] += 1
H_max = max(H_bomb)
W_max = max(W_bomb)
H_memo = set()
W_memo = set()
for i in range(H):
if H_bomb[i] == H_max:
H_memo.add(i)
for j in range(W):
if W_bomb[j] == W_max:
W_memo.add(j)
ans = H_max + W_max
# for i in H_memo:
# for j in W_memo:
# if tuple([i, j]) not in S:
# print(ans)
# exit()
cnt = 0
for s in S:
if s[0] in H_memo and s[1] in W_memo:
cnt += 1
if cnt == len(H_memo)*len(W_memo):
print(ans-1)
else:
print(ans)
| 0 | null | 43,358,506,596,988 | 230 | 89 |
n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
n_i = int(n[i])
dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1]))
|
import numpy as np
count = 0
n,d = map(int,input().split())
for _ in range(n):
p,q = map(int, input().split())
if np.sqrt(p**2+q**2) <= d:
count += 1
print(count)
| 0 | null | 38,380,419,126,378 | 219 | 96 |
N=int(input())
if N%2==1:
print(int((N-1)/2+1))
else:
print(int(N/2))
|
N = int(input())
print(int((N + 1) / 2))
| 1 | 58,901,791,596,622 | null | 206 | 206 |
n,m=map(int,input().split())
*s,=map(int,input()[::-1])
i=0
a=[]
while i<n:
j=min(n,i+m)
while s[j]:j-=1
if j==i:
print(-1)
exit()
a+=j-i,
i=j
print(*a[::-1])
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
s = input().strip()
ans = []
now = n
while now != 0:
tmp = False
for i in range(m, 0, -1):
if now-i >= 0 and s[now-i] == '0':
tmp = True
now -= i
ans.append(i)
break
if not tmp:
print(-1)
sys.exit()
print(*ans[::-1])
| 1 | 139,249,281,125,052 | null | 274 | 274 |
import itertools
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(len(l)-2):
for j in range(i+1,len(l)):
k = bisect.bisect_left(l, l[i]+l[j])
if k > j:
ans += k - j -1
print(ans)
|
OFFSET = 97
def main():
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
total = 0
a_index = 0
b_index = 0
while a_index != len(A):
if total + A[a_index] <= k:
total += A[a_index]
a_index += 1
else:
break
while b_index != len(B):
if total + B[b_index] <= k:
total += B[b_index]
b_index += 1
else:
break
ans = a_index + b_index
a_index -= 1
while a_index != -1:
total -= A[a_index]
a_index -= 1
while b_index != len(B):
if total + B[b_index] <= k:
total += B[b_index]
b_index += 1
else:
break
if ans < a_index + 1 + b_index :
ans = a_index + 1 + b_index
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 91,729,195,190,614 | 294 | 117 |
s = input()
hi = ['hi', 'hihi', 'hihihi', 'hihihihi', 'hihihihihi']
print('Yes') if s in hi else print('No')
|
s = input()
if len(s)%2!=0:
print("No")
else:
if s.count("hi") == len(s)//2:
print("Yes")
else:
print("No")
| 1 | 53,076,861,484,808 | null | 199 | 199 |
L = int(input())
L /= 3
print(L**3)
|
L = int(input())
print(L*L*L/27)
| 1 | 47,176,097,211,110 | null | 191 | 191 |
#ABC166-B
count=0
a=[]
n,k=map(int,input().split())
for i in range(k):
d = int(input())
a+=list((map(int, input().split())))
ans=(n-len(set(a)))
print(ans)
|
#ALDS_11_B - 深さ優先探索
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)#再帰回数の上限
def DFS(u, cnt):
visited.add(u)
first_contact = cnt
cnt += 1
for nb in edge[u]:
if nb not in visited:
cnt = DFS(nb, cnt)
stamp[u] = first_contact, cnt
return cnt+1
N = int(input())
edge = [[] for i in range(N)]
for _ in range(N):
tmp = list(map(int,input().split()))
if tmp[1] != 0:
edge[tmp[0]-1] = list(map(lambda x: int(x-1),tmp[2:]))
stamp = [None]*N
visited = set()
c = 1
for i in range(N):
if i not in visited:
c = DFS(i, c)
for i,s in enumerate(stamp):
print(i+1,*s)
| 0 | null | 12,413,002,864,640 | 154 | 8 |
N = int(input())
S, T = input().split()
L = [S[i]+T[i] for i in range(N)]
print(*L, sep="")
|
n=int(input())
a,b=map(str,input().split())
s=''
for i in range(n):
s+=a[i]
s+=b[i]
print(s)
| 1 | 111,805,921,134,360 | null | 255 | 255 |
s = input()
if s[0] == s[1] and s[1] == s[2]:
print('No')
else:
print('Yes')
|
from collections import deque;
count, time_slice = map(int, input().split(" "));
data = [];
for i in range(count):
data += input().split(" ");
def round_robin(data, count, time_slice):
t = 0;
data = [[data[i], int(data[i + 1])] for i in range(0, len(data), 2)];
que = deque(data);
while len(que) > 0:
current = que.popleft();
if current[1] > time_slice:
current[1] -= time_slice;
t += time_slice;
que.append(current);
else:
t += current[1];
print(current[0], t);
round_robin(data, count, time_slice);
| 0 | null | 27,435,052,120,762 | 201 | 19 |
N,K = map(int,input().split())
l = []
mod = 998244353
for i in range(K):
L,R = map(int,input().split())
l.append([L,R])
l.sort()
a = [0 for i in range(N+1)]
a[1] = 1
b = [0 for i in range(N+1)]
b[1] = 1
for i in range(2,N+1):
for j in range(K):
if l[j][0] < i:
a[i] += (b[i-l[j][0]]-b[max(0,i-l[j][1]-1)])%mod
else:
break
b[i] = (b[i-1]+a[i])%mod
mod = 998244353
print(a[N]%mod)
|
N,K=map(int, input().split())
P=998244353
S=[tuple(map(int,input().split())) for _ in range(K)]
f=[0]*(10**6)
f[0]=1
for i in range(N):
for l, r in S:
f[i+l] += f[i]
f[i+l]%=P
f[i+r+1]-=f[i]
f[i+r+1]%=P
if i:
f[i+1]+=f[i]
f[i]%=P
print(f[N-1])
| 1 | 2,677,299,018,050 | null | 74 | 74 |
# Aizu Problem ALDS_1_2_C: Stable Sort
#
import sys, math, os
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input2.txt", "rt")
def printA(A):
print(' '.join([str(a) for a in A]))
def bubble_sort(A):
for i in range(len(A) - 1):
for j in range(len(A) - 1, i, -1):
if A[j][1] < A[j - 1][1]:
A[j], A[j - 1] = A[j - 1], A[j]
return A
def selection_sort(A):
for i in range(len(A)):
mini = i
for j in range(i, len(A)):
if A[j][1] < A[mini][1]:
mini = j
if mini != i:
A[i], A[mini] = A[mini], A[i]
return A
def is_stable(A, B):
for val in range(1, 10):
v = str(val)
if [a[0] for a in A if a[1] == v] != [b[0] for b in B if b[1] == v]:
return False
return True
N = int(input())
A = list(input().split())
A_bubble = bubble_sort(A[:])
printA(A_bubble)
print("Stable" if is_stable(A, A_bubble) else "Not stable")
A_select = selection_sort(A[:])
printA(A_select)
print("Stable" if is_stable(A, A_select) else "Not stable")
|
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()
| 0 | null | 45,512,287,487,602 | 16 | 238 |
n = int(input())
s = []
for i in range(n):
s.append(input())
s.sort()
# print(s)
m = 1
cnt = [1]
ans = []
for i in range(1,n):
if s[i] == s[i-1]:
cnt[-1] += 1
else:
if cnt[-1] > m:
ans = [s[i-1]]
elif cnt[-1] == m:
ans.append(s[i-1])
m = max(m, cnt[-1])
cnt.append(1)
if i == n-1 and cnt[-1] > m:
ans = [s[i]]
elif i == n-1 and cnt[-1] == m:
ans.append(s[i])
print('\n'.join(ans))
|
N = int(input())
S = sorted([input() for x in range(N)])
word = [S[0]]+[""]*(N-1) #単語のリスト
kaisuu = [0]*N #単語の出た回数のリスト
k = 0 #今wordリストのどこを使っているか
for p in range(N): #wordとkaisuuのリスト作成
if word[k] != S[p]:
k += 1
word[k] = S[p]
kaisuu[k] += 1
else:
kaisuu[k] += 1
m = max(kaisuu) #m:出た回数が最大のもの
ansnum = [] #出た回数が最大のもののwordリストでの位置
for q in range(N):
if kaisuu[q] == m:
ansnum += [q]
for r in range(len(ansnum)):
print(word[ansnum[r]])
| 1 | 70,278,591,604,020 | null | 218 | 218 |
from sys import stdin
m1, d1 = [int(_) for _ in stdin.readline().rstrip().split()]
m2, d2 = [int(_) for _ in stdin.readline().rstrip().split()]
if m1 == m2:
print(0)
else:
print(1)
|
M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
print(1 if M1 != M2 else 0)
| 1 | 124,806,152,398,170 | null | 264 | 264 |
N, = map(int, input().split())
X = [(i+1, x) for i, x in enumerate(map(int, input().split()))]
dp = [[0]*(N+1) for _ in range(N+1)]
X = sorted(X, key=lambda x:x[1])[::-1]
for i in range(1, N+1):
ai, a = X[i-1]
dp[i][0] = dp[i-1][0]+a*abs(ai-(N-i+1))
for j in range(1, i):
dp[i][j] = max(dp[i-1][j-1]+a*abs(ai-j), dp[i-1][j]+a*abs(ai-(N-(i-j)+1)))
dp[i][i] = dp[i-1][i-1]+a*abs(ai-i)
print(max(dp[-1]))
|
n = int(input())
a = list(map(int,input().split()))
b = list((x,i) for i,x in enumerate(a))
b.sort(reverse=True)
dp = [[0]*(n+1-i) for i in range(n+1)]
for i in range(n):
x,idx = b[i]
for j in range(i+1):
k = i-j
if dp[j+1][k] < dp[j][k] + x*(idx-j):
dp[j+1][k] = dp[j][k] + x*(idx-j)
if dp[j][k+1] < dp[j][k] + x*((n-1-k)-idx):
dp[j][k+1] = dp[j][k] + x*((n-1-k)-idx)
ans = 0
for i in range(n+1):
if ans < dp[i][n-i]:
ans = dp[i][n-i]
print(ans)
| 1 | 33,706,592,599,208 | null | 171 | 171 |
#coding:utf-8
N = int(input())
taro = 0
hanako = 0
for i in range(N):
strs = input().split()
if strs[0] > strs[1]:
taro += 3
elif strs[0] < strs[1]:
hanako += 3
else:
taro+=1
hanako+=1
print(str(taro) + " " + str(hanako))
|
n = int(input())
a, b = 0, 0
for i in range(n):
s1, s2 = input().split()
if s1 > s2:
a += 3
elif s1 == s2:
a += 1
b += 1
else:
b += 3
print('{0} {1}'.format(a, b))
| 1 | 1,974,937,318,432 | null | 67 | 67 |
import sys
import numpy as np # noqa
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
K = int(readline())
C = readline().rstrip().decode()
a = 0 # a: 赤から白に変える操作
b = 0 # b: 白から赤に変える操作
for i in range(len(C)):
if C[i] == 'R':
a += 1
mn = a
for i in range(len(C)):
if C[i] == 'R':
a -= 1
else:
b += 1
mn = min(mn, min(a, b) + abs(a-b))
print(mn)
|
import sys
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))
N = ins()
K = ini()
def solve():
M = len(N)
dp_eq = [[0 for _ in range(K + 1)] for _ in range(M)]
dp_less = [[0 for _ in range(K + 1)] for _ in range(M)]
dp_eq[0][K - 1] = 1
dp_less[0][K] = 1
dp_less[0][K - 1] = ord(N[0]) - ord("0") - 1
for i in range(1, M):
is_zero = N[i] == "0"
for k in range(K + 1):
if is_zero:
dp_eq[i][k] = dp_eq[i - 1][k]
elif k < K:
dp_eq[i][k] = dp_eq[i - 1][k + 1]
dp_less[i][k] = dp_less[i - 1][k]
if k < K:
dp_less[i][k] += dp_less[i - 1][k + 1] * 9
if not is_zero:
dp_less[i][k] += dp_eq[i - 1][k]
if k < K:
dp_less[i][k] += dp_eq[i - 1][k + 1] * (ord(N[i]) - ord("0") - 1)
return dp_eq[M - 1][0] + dp_less[M - 1][0]
print(solve())
| 0 | null | 41,354,851,006,840 | 98 | 224 |
N = int(input())
S = input()
l = [S[i:i+3] for i in range(0, len(S)-2)]
print(l.count('ABC'))
|
import sys
input = sys.stdin.readline
s = list(input())
a = 0
#print(len(s))
for i in range((len(s) - 1) // 2):
#print(s[i], s[-2-i])
if s[i] != s[-2-i]:
a += 1
print(a)
| 0 | null | 109,643,309,623,652 | 245 | 261 |
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
ok = 10**12
ng = -1
def check(time):
shugyou = 0
for i in range(N):
if A[i]*F[i] > time:
shugyou += A[i] - (time//F[i])
return shugyou <= K
while ok - ng > 1:
mid = (ok + ng)//2
if check(mid):
ok = mid
else:
ng = mid
print(ok)
|
'''
A[i]<A[j]
F[i]<F[j]
となる組み合わせが存在したと仮定
max(A[i]*F[i],A[j]*F[j])=A[j]*F[j]
>A[i]*F[j]
>A[j]*F[i]
より、この場合はiとjを入れ替えたほうが最適となる。
結局
Aを昇順ソート
Fを降順ソート
するのが最適となる。
問題は修行の割当である。
順序を保ったまま修行していっても問題ない
3,2->1,2と
3.2->2,1は同じとみなせるため
二分探索
成績をmidにするために必要な修行コスト
lowならK回より真に大
highならK回以下
'''
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
F=[int(i) for i in input().split()]
A.sort()
F.sort(reverse=True)
low=-1
high=max(A)*max(F)+1
while(high-low>1):
mid=(high+low)//2
tmp=0
for i in range(N):
#A[i]*F[i]-mid<=X*F[i]
x=(A[i]*F[i]-mid+F[i]-1)//F[i]
if x<0:
continue
tmp+=x
if tmp<=K:
high=mid
else:
low=mid
print(high)
| 1 | 164,842,021,426,852 | null | 290 | 290 |
from collections import defaultdict
N, X, Y = map(int, input().split())
ctr = defaultdict(int)
for u in range(1, N):
for v in range(u + 1, N + 1):
d = min(v - u, abs(u - X) + 1 + abs(Y - v))
ctr[d] += 1
for n in range(1, N):
print(ctr[n])
|
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
if __name__ == "__main__":
n,x,y = map(int,input().split())
x-=1
y-=1
ans = [0]*(n)
for i in range(n):
for j in range(i+1,n):
d = min(abs(i-j),abs(x-i) + abs(y-j) + 1)
ans[d] += 1
for i in range(1,n):
print(ans[i])
| 1 | 43,967,612,374,390 | null | 187 | 187 |
k=int(input())
v='ACL'
print(k*v)
|
from sys import stdin
input = stdin.readline
K = int(input())
print("ACL" * K)
| 1 | 2,191,014,565,910 | null | 69 | 69 |
H, N = map(int, input().split())
A = list(map(int, input().split()))
def answer(H: int, N: int, A: list) -> str:
damage = 0 # damageに0を代入します
for i in range(0, N): # 0:Nの間にiがある間は
damage += int(A[i]) # damageにリスト[i]をint(整数)に変え足します
# 6でdamage=0にforの間Aの値を足していく意味
i += 1 # 条件を満たす間はiに1つずつ足していきます。リスト内を進めていく
if damage < H:
return 'No'
else:
return 'Yes'
print(answer(H, N, A))
|
h,n = map(int,input().split())
a = map(int,input().split())
if sum(a) >= h:
print("Yes")
else:
print("No")
| 1 | 77,768,284,436,890 | null | 226 | 226 |
N=int(input())
S=input()
a=ord("A")
z=ord("Z")
result=""
for i in range(len(S)):
word=ord(S[i])+N
if word>z:
word=word-z-1+a
x=chr(word)
result+=x
print(result)
|
import math
from decimal import Decimal
A, B = list(map(str, input().split()))
print(math.floor(Decimal(A) * Decimal(B)))
| 0 | null | 75,837,109,299,468 | 271 | 135 |
n = int(raw_input())
debt=100000
for i in range(n):
debt*=1.05
if debt % 1000 != 0:
debt = (int(debt / 1000)+1) * 1000
else:
debt = int(debt)
print debt
|
money = 100000
for _ in range(int(input())):
money += int(money * 0.05)
if money % 1000 != 0:
while money % 1000 != 0:
money += 1
print(money)
| 1 | 1,258,975,850 | null | 6 | 6 |
#a(n) = 2^ceiling(log_2(n+1))-1
import math
print(2**(math.ceil(math.log2(int(input())+1)))-1)
|
print(2**(len(bin(int(input())))-2)-1)
| 1 | 79,852,593,545,578 | null | 228 | 228 |
N,K=map(int,input().split())
H=sorted(list(map(int,input().split())))
if len(H) < K:
print(0)
else:
print(sum(H[:N-K]))
|
if __name__ == '__main__':
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = sorted(H)[::-1]
cnt = 0
for h in H:
if K>0:
K -= 1
else:
cnt += h
print(cnt)
| 1 | 78,931,078,181,260 | null | 227 | 227 |
n,k = input().split()
k = int(k)-1
a = list()
a = input().split(" ")
b = list()
for i in a:
b.append(int(i))
b.sort()
sum = 0
while k>=0:
sum += b[k]
k -= 1
print(sum)
|
n, k = map(int, input().split())
p = sorted(map(int, input().split()))
ans = sum(p[:k])
print(ans)
| 1 | 11,581,760,521,210 | null | 120 | 120 |
weatherS = input()
p = weatherS[0] == 'R'
q = weatherS[1] == 'R'
r = weatherS[2] == 'R'
if p and q and r:
serial = 3
elif (p and q) or (q and r):
serial = 2
elif p or q or r:
serial = 1
else:
serial = 0
print(serial)
|
N = int(input())
S = input()
res = ''
for c in S:
res += chr((ord(c) - ord('A') + N) % 26 + ord('A'))
print(res)
| 0 | null | 69,614,052,229,240 | 90 | 271 |
# coding=utf-8
a, b, c = map(int, raw_input().rstrip().split())
print sum([1 if not c % i else 0 for i in range(a, b+1)])
|
N = int(input())
print(input().count("ABC"))
""" 3文字ずつスライスし判定
S = input()
count = 0
# スライスでi+2文字目まで行くのでfor文範囲はN-2でとどめる
for i in range(N-2):
if S[i:i+3] == "ABC":
count += 1
print(count) """
| 0 | null | 49,787,915,363,406 | 44 | 245 |
n, x, m = map(int, input().split())
mn = min(n, m)
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [False] * m # for cycle check
for _ in range(mn):
if X[x]: break
X[x] = True
P.append(x)
sum_p += x
x = x*x % m
if len(P) >= mn:
print(sum_p)
exit()
pre_len = P.index(x)
cyc_len = len(P) - pre_len
nxt_len = (n - pre_len) % cyc_len
cyc_num = (n - pre_len) // cyc_len
cyc = sum(P[pre_len:])
remain = sum(P[:pre_len + nxt_len])
print(cyc * cyc_num + remain)
|
# -*- coding: utf-8 -*-
A, B=map(int,input().split())
if(A<=B):
print("unsafe")
else:
print("safe")
| 0 | null | 16,122,992,355,408 | 75 | 163 |
import sys
sys.setrecursionlimit(10**6)
N, u, v = map(int, input().split())
tree = [[] for _ in range(N)]
for _ in range(N-1):
a, b = map(int, input().split())
tree[a-1].append(b-1)
tree[b-1].append(a-1)
#print(tree)
def calc_dist(dlist, n, dist, nnode):
for c in tree[n]:
if c == nnode:
continue
dlist[c] = dist
calc_dist(dlist, c, dist+1, n)
u_dist_list = [0] * N
calc_dist(u_dist_list, u-1, 1, -1)
#print(u_dist_list)
v_dist_list = [0] * N
calc_dist(v_dist_list, v-1, 1, -1)
#print(v_dist_list)
ans = 0
for i in range(N):
if (v_dist_list[i] - u_dist_list[i]) > 0 and v_dist_list[i] > ans:
ans = v_dist_list[i] - 1
print(ans)
|
import os
import sys
import numpy as np
def solve(N, U, V, AB):
G = [[0]*0 for _ in range(N+1)]
for idx_ab in range(len(AB)):
a, b = AB[idx_ab]
G[a].append(b)
G[b].append(a)
P1 = np.zeros(N+1, dtype=np.int64)
def dfs1(u):
st = [u]
while st:
v = st.pop()
p = P1[v]
for u in G[v]:
if p != u:
st.append(u)
P1[u] = v
dfs1(U)
path_u2v = [U]
v = V
while v != U:
v = P1[v]
path_u2v.append(v)
path_u2v.reverse()
l = len(path_u2v)
half = (l-2) // 2
c = path_u2v[half]
ng = path_u2v[half+1]
Depth = np.zeros(N+1, dtype=np.int64)
def dfs2(c):
st = [c]
P = np.zeros(N+1, dtype=np.int64)
while st:
v = st.pop()
p = P[v]
d = Depth[v]
for u in G[v]:
if p != u and u != ng:
st.append(u)
P[u] = v
Depth[u] = d + 1
dfs2(c)
c_ = path_u2v[l-1-half]
v = c_
while v != c:
Depth[v] = 0
v = P1[v]
d = l%2
ans = max(Depth) + half + d
return ans
# >>> numba compile >>>
numba_config = [
[solve, "i8(i8,i8,i8,i8[:,:])"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
from numba import njit
from numba.pycc import CC
cc = CC("my_module")
for func, signature in numba_config:
vars()[func.__name__] = njit(signature)(func)
cc.export(func.__name__, signature)(func)
cc.compile()
exit()
elif os.name == "posix":
exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
from numba import njit
for func, signature in numba_config:
vars()[func.__name__] = njit(signature, cache=True)(func)
print("compiled!", file=sys.stderr)
# <<< numba compile <<<
def main():
N, u, v = map(int, input().split())
if N==2:
print(0)
return
AB = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(N-1, 2)
ans = solve(N, u, v, AB)
print(ans)
main()
| 1 | 117,519,105,954,738 | null | 259 | 259 |
# coding: utf-8
x=int(input())
i=1
while x!=0:
print("Case "+str(i)+": "+str(x))
x=int(input())
i+=1
|
word = list(input())
if word[-1] == "s":
word.append("es")
ans = "".join(word)
else:
word.append("s")
ans = "".join(word)
print(ans)
| 0 | null | 1,445,863,154,880 | 42 | 71 |
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
a,b = input().split()
b = b[:-3] + b[-2:]
ans = int(a) * int(b)
ans = ans // 100
print(ans)
|
from sys import stdin
input = stdin.readline
def main():
A, B = input().split()
A = int(A)
# B = int(100*float(B))
B = int(B[0]+B[2:])
# print((A*B)//100)
if A*B < 100:
print(0)
else:
print(str(A*B)[:-2])
if(__name__ == '__main__'):
main()
| 1 | 16,646,977,041,124 | null | 135 | 135 |
N = int(input())
S = input()
#print(ord("A"))
#print(ord("B"))
#print(ord("Z"))
for i in range(len(S)):
ascii = ord(S[i]) + N #Sのi文字目のアスキーコードを計算してしれにNを足してづらす
if ascii > ord("Z"): #アスキーコードがよりも大きくなった時はA初まりにづらす
ascii = ascii - (ord("Z") - ord("A") + 1)
print(chr(ascii),end = "") #,end = "" は1行ごとに改行しないでプリントする仕組み
|
from collections import defaultdict
N, *A = map(int, open(0).read().split())
ctr1 = defaultdict(int)
ctr2 = defaultdict(int)
for i in range(N):
ctr2[i + A[i]] += 1
if i - A[i] > 0:
ctr1[i - A[i]] += 1
ans = 0
for v in ctr1:
ans += ctr1[v] * ctr2[v]
print(ans)
| 0 | null | 80,587,755,649,468 | 271 | 157 |
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [list(map(int, input().split())) for _ in range(Q)]
ctr = Counter(A)
ans = sum(A)
for b, c in X:
n = ctr[b]
ans += -b * n + c * n
ctr[b] = 0
ctr[c] += n
print(ans)
|
n = int(input())
A = list(map(int, input().split()))
q = int(input())
S = []
for _ in range(q):
x, y = map(int, input().split())
S.append((x, y))
sum = sum(A)
# print(sum)
R = [0 for _ in range(10**5+1)]
for a in A:
R[a-1] += 1
for (x,y) in S:
sum += (y-x)*R[x-1]
R[y-1] += R[x-1]
R[x-1] = 0
print(sum)
| 1 | 12,265,028,968,192 | null | 122 | 122 |
N = int(input())
A = list(map(int,input().split()))
def insertionSort(A,N):
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
outputNum(A)
def outputNum(A):
tmp = map(str,A)
text = " ".join(tmp)
print(text)
outputNum(A)
insertionSort(A,N)
|
def rren(): return list(map(int, input().split()))
N = int(input())
R = rren()
print(" ".join(map(str, R)))
for i in range(1,N):
take = R[i]
j = i-1
while j >= 0 and R[j] > take:
R[j+1] = R[j]
j -= 1
R[j+1] = take
print(" ".join(map(str, R)))
| 1 | 5,672,827,620 | null | 10 | 10 |
mol=[]
for i in range(10):
mol.append(int(input()))
mol.sort(key=None,reverse=True)
for i in range(3):
print(mol[i])
|
a=[int(input()) for i in range(10)]
a.sort(reverse=True)
for i, iv in enumerate(a):
print(iv)
if i >= 2:
break
| 1 | 26,442,172 | null | 2 | 2 |
N = int(input())
l = []
for i in range(N+1):
if i % 5 == 0 or i % 3 == 0:
l.append(0)
else:
l.append(i)
print(sum(l))
|
print(sum([i for i in range(1,int(input())+1) if i%15 and i%5 and i%3]))
| 1 | 34,976,089,922,420 | null | 173 | 173 |
from sys import stdin
N = int(stdin.readline().rstrip())
C = stdin.readline().rstrip()
rc = C.count('R')
ans = 0
for i in range(rc):
if C[i] == 'W':
ans += 1
print(ans)
|
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline()[:-1]
mod = 10**9 + 7
def readInt():return int(input())
def readIntList():return list(map(int,input().split()))
def readStringList():return list(input())
def readStringListWithSpace():return list(input().split())
def readString():return input()
n = readInt()
text = readStringList()
import collections
c = collections.Counter(text)
if c['W'] == 0:
print("0")
exit()
i,j,count = 0,len(text)-1,0
while i <= j and i < len(text):
if text[i] == 'W':
while text[j] != 'R' and j > 0:
j -= 1
if i <= j and j > 0:
text[i],text[j] = text[j],text[i]
count += 1
i += 1
print(count)
| 1 | 6,308,420,847,210 | null | 98 | 98 |
n, m, l = map(int, input().split())
A, B = [], []
for _ in range(n):
A.append(list(map(int, input().split())))
for _ in range(m):
B.append(list(map(int, input().split())))
C = []
B_T = list(zip(*B))
for a_row in A:
C.append([sum(a * b for a, b, in zip(a_row, b_column)) for b_column in B_T])
for row in C:
print(' '.join(map(str, row)))
|
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
A,B,M = mi()
a = li()
b = li()
xyc = [li() for i in range(M)]
ans = min(a)+min(b)
for i in range(M):
ans = min(ans,a[xyc[i][0]-1]+b[xyc[i][1]-1] - xyc[i][2])
print(ans)
| 0 | null | 27,666,764,990,528 | 60 | 200 |
class BIT:
def __init__(self, n):
self.node = [ 0 for _ in range(n+1) ]
def add(self, idx, w):
i = idx
while i < len(self.node) - 1:
self.node[i] += w
i |= (i + 1)
def sum_(self, idx):
ret, i = 0, idx-1
while i >= 0:
ret += self.node[i]
i = (i & (i + 1)) - 1
return ret
def sum(self, l, r):
return self.sum_(r) - self.sum_(l)
n = int(input())
s = list(input())
q = int(input())
tree = [ BIT(n) for _ in range(26) ]
for i in range(n):
tree_id = ord(s[i]) - ord("a")
tree[tree_id].add(i, 1)
for _ in range(q):
query = input().split()
com = int(query[0])
if com == 1:
idx, new_char = int(query[1]), query[2]
idx -= 1
old_char = s[idx]
new_id = ord(new_char) - ord("a")
old_id = ord(old_char) - ord("a")
tree[old_id].add(idx, -1)
tree[new_id].add(idx, 1)
s[idx] = new_char
if com == 2:
l, r = int(query[1]), int(query[2])
ret = 0
for c in range(26):
if tree[c].sum(l-1, r) > 0:
ret += 1
print(ret)
|
import math
import sys
import os
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))
if os.getenv("LOCAL"):
inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
sys.stdin = open(inputFile, "r")
INF = float("inf")
N,K = LI()
P = LI()
C = LI()
P = list(map(lambda x:x-1,P))
res = -INF
used = [False] * N
ss = []
for i in range(N):
# print('i',i)
if used[i]:
continue
cur = i
s = []
while not used[cur]:
used[cur] = True
s.append(C[cur])
cur = P[cur]
# print(cur)
ss.append(s)
# print(ss)
for vec in ss:
M = len(vec)
# 2周の累積和
sum = [0]*(2*M + 1)
for i in range(2*M):
sum[i+1] = sum[i]+vec[i%M]
# amari[r] := 連続するr個の最大値
amari = [-INF]*M
for i in range(M):
for j in range(M):
amari[j] = max(amari[j],sum[i+j] - sum[i])
for r in range(M):
if r>K:
continue
q = (K-r)//M
if r==0 and q==0:
continue
if sum[M]>0:
res = max(res, amari[r] + sum[M] * q)
elif r > 0:
res = max(res, amari[r])
print(res)
| 0 | null | 33,929,748,792,380 | 210 | 93 |
# coding: utf-8
# Your code here!
import bisect
N,D,A=map(int,input().split())
log=[]
loc=[]
ene=[]
for _ in range(N):
X,H=map(int,input().split())
log.append([X,H])
log.sort(key=lambda x:x[0])
for X,H in log:
loc.append(X)
ene.append(H)
syokan=[0]*N
power=0
for i in range(N):
temp=max(-((-ene[i]+power)//A),0)*A
power+=temp
rng=bisect.bisect_right(loc,loc[i]+2*D)
syokan[min(rng-1,N-1)]+=temp
power-=syokan[i]
#print(syokan)
#print(syokan)
print(sum(syokan)//A)
|
from math import ceil
from collections import deque
n,d,a = map(int,input().split())
m = [list(map(int,input().split())) for _ in range(n)]
m.sort()
l = [(x, ceil(h/a)) for x,h in m]
ans = 0
damage = 0
que = deque([])
for x,h in l:
while que and que[0][0] < x:
s,t = que.popleft()
damage -= t
need = max(0, h - damage)
ans += need
damage += need
if need:
que.append((x+2*d, need))
print(ans)
| 1 | 82,123,680,664,480 | null | 230 | 230 |
import math
a, b, x = map(int, input().split())
x /= a
def is_ok(theta):
if math.tan(theta) * b < a:
return x <= math.tan(theta) * b * b / 2
else:
p = a/math.tan(theta)
return x <= a*p/2+a*(b-p)
def bisect(ng, ok):
while (abs(ok - ng) > 1e-9):
mid = (ok + ng) / 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
ans = bisect(0, math.pi)
ans = 90- ans * 180 / math.pi
print(ans)
|
from math import *
a,b,x=map(int,input().split())
x=x/a
if x<=a*b/2:
t=2*x/b
c=sqrt(b**2+t**2)
ans=90-degrees(asin(t/c))
else:
t=2*(a*b-x)/a
c=sqrt(a**2+t**2)
ans=90-degrees(asin(a/c))
print(ans)
| 1 | 163,510,117,023,212 | null | 289 | 289 |
X = int(input())
ans = 0
if X >= 500:
ans += (X//500)*1000
ans += ((X%500)//5)*5
else:
ans += (X//5)*5
print(ans)
|
X=int(input())
ans=1000*(X//500)+5*((X-(X//500*500))//5)
print(ans)
| 1 | 42,618,454,235,800 | null | 185 | 185 |
# 726 diff 1000
# 解法 836 XYへ到達する場合はどの経路も合計の長さは同じ = 動き方1,2の合計は同じ.
# なので合計値 L がわかればLについてmove1=i,move2=L-iとして,実際の経路の構成の仕方は,L_C_iなので
move1 = [1,2]
move2 = [2,1]
X,Y = map(int,input().split())
# まず,目的地へ到達できるか?到達できる場合は合計どれだけの移動量が必要かを計算する
max_move1 = int(Y/2)
move2_num = -1
move1_num = -1
for i in range(max_move1+1):
rest = [X-i,Y-2*i]
if (X-i) == 2*(Y-2*i):
move1_num = i
move2_num = (Y-2*i)
if move1_num == -1:
print(0)
exit()
# 到達するのに必要な合計の移動量がわかったので,それらの並べ替えについて考える.
# 求めるのは nCk すなわち,いつもの奴
large = 10**9 + 7
# mod(x^p-1,p) = 1 よってmod(x^p-2,p) = 1/x となる.
ans = 1
for i in range(move1_num):
ans *= (move1_num+move2_num - i)
ans %= large
for j in range(2,move1_num+1):
ans *= pow(j,large-2,large)
ans %= large
print(ans)
|
n = int(input())
A = list(map(int, input().split()))
inf = 10 ** 18
dp = [[-inf] * 3 for _ in range(n + 1)]
k = 1 + n % 2
dp[0][0] = 0
for i in range(n):
for j in range(k + 1):
if j < k:
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j])
now = dp[i][j]
if (i + j) % 2 == 0:
now += A[i]
dp[i + 1][j] = max(dp[i + 1][j], now)
print(dp[n][k])
| 0 | null | 93,845,695,308,540 | 281 | 177 |
d = {
(1,2): 3,
(1,3): 5,
(1,4): 2,
(1,5): 4,
(2,1): 4,
(2,3): 1,
(2,4): 6,
(2,6): 3,
(3,1): 2,
(3,2): 6,
(3,5): 1,
(3,6): 5,
(4,1): 5,
(4,2): 1,
(4,5): 6,
(4,6): 2,
(5,1): 3,
(5,3): 6,
(5,4): 1,
(5,6): 4,
(6,2): 4,
(6,3): 2,
(6,4): 5,
(6,5): 3
}
v = list(map(int, input().split()))
q = int(input())
questions = []
for _ in range(q):
x, y = map(int, input().split())
questions.append((x,y))
vTod = dict(zip(v,list(range(1,7))))
dTov = {v:k for k, v in vTod.items()}
for top, face in questions:
top = vTod[top]
face = vTod[face]
right = d[(top,face)]
right = dTov[right]
print(right)
|
from sys import stdin
from copy import deepcopy
import queue
class Dice:
def __init__(self, nums):
self.labels = [None] + [ nums[i] for i in range(6) ]
self.pos = {
"E" : 3,
"W" : 4,
"S" : 2,
"N" : 5,
"T" : 1,
"B" : 6
}
def rolled(dice, queries):
d = deepcopy(dice)
for q in queries:
if q == "E":
d.pos["T"], d.pos["E"], d.pos["B"], d.pos["W"] = d.pos["W"], d.pos["T"], d.pos["E"], d.pos["B"]
elif q == "W":
d.pos["T"], d.pos["E"], d.pos["B"], d.pos["W"] = d.pos["E"], d.pos["B"], d.pos["W"], d.pos["T"]
elif q == "S":
d.pos["T"], d.pos["S"], d.pos["B"], d.pos["N"] = d.pos["N"], d.pos["T"], d.pos["S"], d.pos["B"]
elif q == "N":
d.pos["T"], d.pos["S"], d.pos["B"], d.pos["N"] = d.pos["S"], d.pos["B"], d.pos["N"], d.pos["T"]
else:
return d
nums = [int(x) for x in stdin.readline().rstrip().split()]
q = int(stdin.readline().rstrip())
dice = Dice(nums)
# TとSに対応するクエリを記憶し, resをすぐに呼び出せるようにする
memo = [[False] * 7 for i in range(7)]
memo[dice.pos["T"]][dice.pos["S"]] = ""
# クエリとさいころの東面を記憶
res = { "" : dice.labels[dice.pos["E"]] }
# BFSで探索, 解を求めたらreturn Trueで抜け出す
# diceの中身をいじってはならない
def solve(T, S):
que = queue.Queue()
que.put(dice)
sol_q = ["E", "N", "S", "W"]
while not que.empty():
d = que.get()
if d.pos["T"] == T and d.pos["S"] == S:
break
else:
for i in sol_q:
d_next = Dice.rolled(d, i)
que.put(d_next)
memo[d_next.pos["T"]][d_next.pos["S"]] = memo[d.pos["T"]][d.pos["S"]] + i
res[memo[d_next.pos["T"]][d_next.pos["S"]]] = d_next.labels[d_next.pos["E"]]
else:
return memo[T][S]
for i in range(q):
ts = [int(x) for x in stdin.readline().rstrip().split()]
T = dice.labels.index(ts[0])
S = dice.labels.index(ts[1])
if solve(T, S) != False:
print(res[memo[T][S]])
| 1 | 264,369,807,756 | null | 34 | 34 |
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_int_n():
return list(map(int, buff_readline().split()))
def read_float():
return float(buff_readline())
def read_float_n():
return list(map(float, buff_readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
class Mod:
def __init__(self, m):
self.m = m
def add(self, a, b):
return (a + b) % self.m
def sub(self, a, b):
return (a - b) % self.m
def mul(self, a, b):
return ((a % self.m) * (b % self.m)) % self.m
def div(self, a, b):
return self.mul(a, pow(b, self.m-2, self.m))
def pow(self, a, b):
return pow(a, b, self.m)
class Bisect:
def __init__(self, func):
self.__func = func
def bisect_left(self, x, lo, hi):
while lo < hi:
mid = (lo+hi)//2
if self.__func(mid) < x:
lo = mid+1
else:
hi = mid
return lo
def bisect_right(self, x, lo, hi):
while lo < hi:
mid = (lo+hi)//2
if x < self.__func(mid):
hi = mid
else:
lo = mid+1
return lo
def f(n):
r = 1
for i in range(1, n+1):
r *= i
return r
@mt
def slv(N, K, A, F):
A.sort()
F.sort(reverse=True)
def f(x):
y = 0
for a, f in zip(A, F):
b = a - x//f
if b > 0:
y += b
if y <= K:
return 1
return 0
return Bisect(f).bisect_left(1, 0, 10**18)
def main():
N, K = read_int_n()
A = read_int_n()
F = read_int_n()
print(slv(N, K, A, F))
if __name__ == '__main__':
main()
|
S = list(map(str, input().split()))
count = len(S)
stack = []
for i in range(count):
if S[i] == "+":
b = stack.pop()
a = stack.pop()
stack.append(a + b)
elif S[i] == "-":
b = stack.pop()
a = stack.pop()
stack.append(a - b)
elif S[i] == "*":
b = stack.pop()
a = stack.pop()
stack.append(a * b)
else:
stack.append(int(S[i]))
print(stack[0])
| 0 | null | 82,506,073,495,374 | 290 | 18 |
k = int(input())
sum = 0
def gcd(x, y): # ユークリッドの互除法
if y > x:
y,x = x,y
while y > 0:
r = x%y
x = y
y = r
return x
import math
for i in range(1,k+1):
for m in range(1,k+1):
sub = gcd(i,m)
for l in range(1,k+1):
sum += gcd(sub,l)
print(sum)
|
from math import gcd
def main():
K = int(input())
ans = 0
for i in range(1, K+1):
for j in range(1, K+1):
temp = gcd(i, j)
for l in range(1, K+1):
ans += gcd(temp, l)
print(ans)
if __name__ == '__main__':
main()
| 1 | 35,519,094,228,320 | null | 174 | 174 |
b,a = input().split()
print(a + b)
|
x = [x for x in input().split()]
print(x[1]+x[0])
| 1 | 103,011,715,950,470 | null | 248 | 248 |
n = int(input())
a = list(map(int,input().split()))
ans = [0 for i in range(n)]
for i in a:
ans[i-1] += 1
for i in ans:
print(i)
|
N=int(input());
print((N-1)//2);
| 0 | null | 93,031,612,556,060 | 169 | 283 |
line = raw_input()
line2 = ''
for s in line:
if s == '\n':
break
elif s.islower():
line2 += s.upper()
elif s.isupper():
line2 += s.lower()
else:
line2 += s
print line2
|
import sys
sys.setrecursionlimit(10**8)
def ii(): 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 li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
S = input()
N = len(S)
cnt = 0
for i in range(N//2):
if S[i] != S[-1-i]:
cnt += 1
print(cnt)
| 0 | null | 60,972,912,812,854 | 61 | 261 |
import sys
a,v = map(int, sys.stdin.readline().rstrip("\n").split())
b,w = map(int, sys.stdin.readline().rstrip("\n").split())
t = int(sys.stdin.readline().rstrip("\n"))
between = abs(b - a)
speed = v-w
if between <= speed*t:
print('YES')
else:
print('NO')
|
hoge = int(input())
l = [int(i) for i in input().split()]
mn = min(l)
mx = max(l)
sm = sum(l)
print(mn,mx,sm)
| 0 | null | 8,001,253,375,308 | 131 | 48 |
n,m = map(int, input().split())
s_c = [tuple(map(int, input().split())) for _ in range(m)]
ans = -1
for num in range(1000):
if len(str(num)) != n: continue
for s,c in s_c:
if str(num)[s-1] != str(c):
break
else:
ans = num
break
print(ans)
|
import sys
n,m = map(int,input().split())
a = [0]*n
flag = [0]*n
for i in range(m):
s,c = map(int,input().split())
if s == 1 and c == 0 and n != 1:
print(-1)
sys.exit()
if flag[s-1] == 0:
flag[s-1] = 1
else:
#flag[s-1] == 1
if a[s-1] != c:
print(-1)
sys.exit()
a[s-1] = c
if a[0] == 0 and n != 1:
a[0] = 1
print("".join(map(str,a)))
| 1 | 60,596,811,088,550 | null | 208 | 208 |
n = int(input())
for i in range(3,n + 1):
if(i % 3 == 0):
print('',i,end = '')
elif(i >= 10 and i % 10 == 3):
print('',i,end = '')
elif(i > 10):
x = i
while(x > 0):
x = int(x / 10)
if(x % 10 == 3):
print('',i,end = '')
break
print('')
|
n = int(raw_input())
S = map(int, raw_input().split())
cnt = 0
def merge(A, left, mid, right):
global cnt
n1 = mid - left
n2 = right - mid
L = [0] * (n1+1)
R = [0] * (n2+1)
#L[:n1] = A[left:mid]
#R[:n2] = A[mid:right]
for i in xrange(n1):
L[i] = A[left + i]
for i in xrange(n2):
R[i] = A[mid + i]
L[n1] = float('inf')
R[n2] = float('inf')
i = 0
j = 0
for k in xrange(left,right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
cnt += 1
else:
A[k] = R[j]
j += 1
cnt += 1
return 0
def mergeSort(A, left, right):
if right - left > 1:
mid = int((left + right) / 2)
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
return 0
def main():
mergeSort(S, 0, len(S))
print ' '.join(map(str, S))
print cnt
return 0
main()
| 0 | null | 528,463,570,508 | 52 | 26 |
n,p = map(int,input().split())
s = input()
ans = 0
if p == 2 or p == 5:
for i,c in enumerate(s,1):
if int(c)%p == 0: ans += i
else:
cnt = [0]*p
cnt[0] = 1
x = 0
d = 1
for c in s[::-1]:
x += d*int(c)
x %= p
cnt[x%p] += 1
d *= 10
d %= p
# print(cnt)
for v in cnt: ans += v*(v-1)//2
print(ans)
|
# coding: utf-8
import sys
from collections import defaultdict
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# MODの管理をして右から進める
N, P = lr()
S = sr()
dic = defaultdict(int)
dic[0] = 1
num = 0
power = 1
answer = 0
if P%2 == 0 or P%5 == 0:
x = [i for i, y in enumerate(S, 1) if int(y) % P == 0]
answer = sum(x)
else:
for s in S[::-1]:
s = int(s)
num += s * power
num %= P
answer += dic[num]
dic[num] += 1
power *= 10; power %= P
print(answer)
# 14
| 1 | 57,984,939,940,658 | null | 205 | 205 |
if __name__ == '__main__':
n = int(input())
a = list(map(int, input().split()))
ans = 0
x, y, z = [0], [0], [0]
for i in range(n):
if a[i] == 0:
if x[-1] == 0:
x.append(1)
y.append(y[-1])
z.append(z[-1])
elif y[-1] == 0:
x.append(x[-1])
y.append(1)
z.append(z[-1])
else:
x.append(x[-1])
y.append(y[-1])
z.append(1)
else:
if x[-1] == a[i]:
x.append(x[-1] + 1)
y.append(y[-1])
z.append(z[-1])
elif y[-1] == a[i]:
x.append(x[-1])
y.append(y[-1] + 1)
z.append(z[-1])
else:
x.append(x[-1])
y.append(y[-1])
z.append(z[-1] + 1)
ans = 1
for i in range(n):
ans *= [x[i], y[i], z[i]].count(a[i])
ans %= (10 ** 9 + 7)
print(ans)
|
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def main():
N = INT()
A = LI()
RGB = [0,0,0]
answer = 1
MOD = 10**9+7
for i in range(N):
if RGB.count(A[i]) == 0:
print(0)
return
answer *= RGB.count(A[i])
answer %= MOD
for j in range(3):
if RGB[j] == A[i]:
RGB[j] += 1
break
print(answer)
return
if __name__ == '__main__':
main()
| 1 | 129,609,989,756,982 | null | 268 | 268 |
import math
a,b,c = map(float,input().split())
rad = math.radians(c)
x = (a**2+b**2-2*a*b*math.cos(rad))**(1/2)
L = a+b+x
S = a*b*math.sin(rad)/2
h = 2*S/a
print("{:.8f}\n{:.8f}\n{:.8f}".format(S,L,h))
|
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = sum(a)/(4*m)
cnt = 0
for i in a:
if b <= i:
cnt += 1
if cnt >= m:
print("Yes")
exit()
print("No")
| 0 | null | 19,462,792,988,030 | 30 | 179 |
N = [x for x in input().split(' ')]
#データを格納するための整数型1次元配列
stack = []
for i in N:
# +が入っていた場合
if i == '+':
num1 = stack.pop()
num2 = stack.pop()
stack.append(num1 + num2)
# -が入っていた場合
elif i == '-':
num2 = stack.pop()
num1 = stack.pop()
stack.append(num1 - num2)
# *が入っていた場合
elif i == '*':
num1 = stack.pop()
num2 = stack.pop()
stack.append(num1 * num2)
#算術演算子( + - *)以外が入っていた場合
else:
stack.append(int(i))
#表示
print(stack.pop())
|
l,r,d = map(int, input().split())
var=l//d
var1=r//d
ans=var1-var
if l%d==0:
ans+=1
print(ans)
| 0 | null | 3,775,548,670,244 | 18 | 104 |
def main(R,C,K,V):
dp = [0]*(4*R*C)
dp[1+4*0+4*C*0] = V[0][0]
for k in range(4*R*C):
n = k%4
j = k//4%C
i = k//(4*C)
if n!=0:
if i != 0 and n==1:
dp[k] = max(dp[k-4*C-n:k-4*C-n+4])+V[i][j]
if j != 0:
dp[k] = max(dp[k],dp[k-4],dp[k-5]+V[i][j])
if n==0:
if i != 0:
dp[k] = max(dp[k-4*C-n:k-4*C-n+4])
print(max(dp[-5:-1]))
if __name__ == '__main__':
R,C,K = map(int,input().split())
C += 1
rcv = [list(map(int,input().split())) for _ in range(K)]
V = [[0]*C for _ in range(R)]
for i in range(K):
r,c,v = rcv[i]
r -= 1
c -= 1
V[r][c] = v
main(R,C,K,V)
|
from collections import Counter
n, p = map(int, input().split())
s = list(map(int, list(input())))
ans = 0
if p in {2, 5}:
for i, num in enumerate(s):
if num % p == 0:
ans += i + 1
else:
bfo, cnt = 0, [1] + [0] * (p - 1)
for i, num in enumerate(s[::-1]):
bfo = (bfo + pow(10, i, p) * num) % p
ans += cnt[bfo]
cnt[bfo] += 1
print(ans)
| 0 | null | 31,923,666,236,166 | 94 | 205 |
a,b,m = map(int, input().split())
aprice = list(map(int, input().split()))
bprice = list(map(int, input().split()))
c = sorted(aprice)
d = sorted(bprice)
e = c[0]+d[0]
f =0
for i in range(m):
x,y,k = map(int, input().split())
f =aprice[x-1]+bprice[y-1]-k
if(e>f):
e = f
print(e)
|
N = int(raw_input())
A = map(int, raw_input().split())
count = 0
for i in xrange(N):
minj = i
for j in xrange(i, N):
if A[j] < A[minj]:
minj = j
if minj != i:
A[i], A[minj] = A[minj], A[i]
count += 1
print ' '.join(map(str, A))
print count
| 0 | null | 27,028,828,956,448 | 200 | 15 |
N = int(input())
S = input().rstrip()
if S[N//2:] == S[:N//2]:
print("Yes")
else:
print("No")
|
n = int(input())
s = input()
t1 = s[:n//2]
t2 = s[n//2:n]
if(t1 == t2):
print("Yes")
else:
print("No")
| 1 | 146,889,528,413,788 | null | 279 | 279 |
N=int(input())
S=input()
S=list(S)
ans=[]
color = ''
for s in S:
if color != s:
ans.append(s)
color = s
print(len(ans))
|
word = input()
n = int(input())
for _ in range(n):
meirei = input().split()
if meirei[0] == "print":
print(word[int(meirei[1]):int(meirei[2])+1])
elif meirei[0] == "reverse":
word = word[:int(meirei[1])] + word[int(meirei[1]):int(meirei[2])+1][::-1] + word[int(meirei[2])+1:]
elif meirei[0] == "replace":
word = word[:int(meirei[1])] + meirei[3] + word[int(meirei[2])+1:]
| 0 | null | 86,495,948,687,460 | 293 | 68 |
from sys import exit
x, n = map(int, input().split())
p = list(map(int, input().split()))
for d in range(x+1): #0に達した時点で0を出力して終了するので、大きい側は気にしなくてよい
for s in [-1, 1]:
a = x + d*s
if not a in p:
print(a)
exit()
|
x, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()] if n != 0 else []
min_ = 100
cnt = 0
for i in range(100 + 2):
if abs(i - x) < min_ and not i in a:
min_ = abs(i - x)
cnt = i
print(cnt)
| 1 | 14,024,610,239,798 | null | 128 | 128 |
import sys
input = sys.stdin.readline
print((input() + input()).count('ABC'))
|
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
S = input().rstrip()
A = [0]*N
cnt = 0
S = reversed(S)
for i, s in enumerate(S):
if s=="1":
cnt += 1
A[i] = cnt
else:
cnt = 0
dp = 0
res = []
while dp+M <= N-1:
t = M - A[dp+M]
if t>0:
dp += t
else:
print(-1)
exit()
res.append(t)
res.append(N-dp)
res.reverse()
print(*res)
| 0 | null | 119,403,564,100,412 | 245 | 274 |
MOD = 10 ** 9 + 7
# エラトステネスの篩(コピペ用)
table_len = 1010
prime_list = [] # 必要に応じて
isprime = [True] * (1010)
isprime[0] = isprime[1] = False
for i in range(table_len):
if isprime[i]:
for j in range(2*i, table_len, i):
isprime[j] = False
prime_list.append(i) # 必要に応じて
N = int(input())
As = list(map(int, input().split()))
factor_counts = [0] * len(prime_list)
big_factors = set() # 1000より大きな素数がAの素因数として含まれるとき、その素因数は一乗のみだから
for A in As:
for i, p in enumerate(prime_list):
count = 0
while A % p == 0:
A //= p
count += 1
factor_counts[i] = max(factor_counts[i], count)
if A == 1:
break
else:
big_factors.add(A)
A_lcm = 1
for p, count in zip(prime_list, factor_counts):
A_lcm *= pow(p, count, MOD)
A_lcm %= MOD
for p in big_factors:
A_lcm *= p
A_lcm %= MOD
ans = 0
for A in As:
ans += A_lcm * pow(A, MOD - 2, MOD) % MOD
ans %= MOD
print(ans)
|
import fractions
from sys import stdin
def main():
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
sa = set(a)
lcm = 1
for ai in sa:
lcm = (ai * lcm) // fractions.gcd(ai, lcm)
ans = 0
for ai in a:
ans += lcm // ai
ans %= 10 ** 9 + 7
print(ans)
if __name__ == "__main__":
main()
| 1 | 87,418,473,228,172 | null | 235 | 235 |
N = [0] + list(map(int, list(input())))
L = len(N)
ans = 0
for i in range(L-1, -1, -1):
if N[i] == 10:
if i == 0:
ans += 1
else:
N[i-1] += 1
N[i] = 0
if N[i] < 5:
ans += N[i]
elif N[i] > 5:
ans += 10 - N[i]
N[i-1] += 1
else:
if i == 0:
ans += N[i]
else:
if N[i-1] >= 5:
N[i-1] += 1
ans += 5
else:
ans += N[i]
print(ans)
|
n = int(input())
a = [input() for i in range(n)]
c0, c1, c2, c3 = 0, 0, 0, 0
for output in a:
if output == 'AC':
c0 += 1
elif output == 'WA':
c1 += 1
elif output == 'TLE':
c2 += 1
elif output == 'RE':
c3 += 1
print('AC x {}'.format(c0))
print('WA x {}'.format(c1))
print('TLE x {}'.format(c2))
print('RE x {}'.format(c3))
| 0 | null | 39,742,975,628,300 | 219 | 109 |
n = int(input())
S = [int(i) for i in input().split()]
q = int(input())
T = [int(i) for i in input().split()]
ans = 0
for t in T:
if t in S:
ans += 1
print(ans)
|
n = int(input())
S = [int(x) for x in input().split()]
q = int(input())
T = [int(x) for x in input().split()]
count = 0
for target in T:
if target in S:
count += 1
print(count)
| 1 | 63,318,933,018 | null | 22 | 22 |
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
num = [i+1 for i in range(N)]
p_ord = 0
q_ord = 0
cnt = 0
for pre in itertools.permutations(num, N):
if P == list(pre):
p_ord = cnt
if Q == list(pre):
q_ord = cnt
cnt += 1
print(abs(p_ord-q_ord))
|
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
per = list(itertools.permutations(sorted(P)))
print(abs(per.index(tuple(P)) - per.index(tuple(Q))))
| 1 | 101,100,799,663,350 | null | 246 | 246 |
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
temp = 0 if a[i][0] - a[i][1] < 0 else a[i][0] - a[i][1]
a[i][1] = a[i][0]+a[i][1]
a[i][0] = temp
a.sort(key = lambda x:x[1])
ans = n
right = 0
for j in a:
if right > j[0]:
ans -= 1
else:
right = j[1]
print(ans)
|
import heapq
n=int(input())
rb = []
heapq.heapify(rb)
for i in range(n):
x,l= map(int,input().split())
heapq.heappush(rb,(x+l,x-l))
ans=0
mostr=-1
while len(rb)>0:
c=heapq.heappop(rb)
if ans==0:
ans+=1
mostr=c[0]
else:
if mostr<=c[1]:
ans+=1
mostr=c[0]
print(ans)
| 1 | 90,243,721,887,204 | null | 237 | 237 |
from functools import reduce
n, m = map(int, input().split())
a = list(map(int, input().split()))
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
def count(n):
cnt = 0
while n % 2 == 0:
n //= 2
cnt += 1
return cnt
A = [count(i) for i in a]
if len(set(A)) != 1:
print(0)
exit()
aa = [i // 2 for i in a]
LCM = reduce(lcm, aa)
res = m // LCM
if res % 2 == 0:
print(res // 2)
else:
print(res // 2 + 1)
|
from math import gcd
from functools import reduce
import sys
input = sys.stdin.readline
def lcm(a, b):
return a*b // gcd(a, b)
def count_factor_2(num):
count = 0
while num % 2 == 0:
num //= 2
count += 1
return count
def main():
n, m = map(int, input().split())
A = list(map(lambda x: x//2, set(map(int, input().split()))))
check = len(set(map(count_factor_2, A)))
if check != 1:
print(0)
return
lcm_a = reduce(lcm, A)
step = lcm_a * 2
ans = (m + lcm_a) // step
print(ans)
if __name__ == "__main__":
main()
| 1 | 101,928,210,929,622 | null | 247 | 247 |
n = int(raw_input())
maxprofit = -2 * 10 ** 9
minval = int(raw_input())
for _ in xrange(n-1):
R = int(raw_input())
maxprofit = max(maxprofit, R-minval)
minval = min(minval, R)
print maxprofit
|
n, m = map(int, input().split())
if m%2 == 0:
x = m//2
y = m//2
else:
x = m//2
y = m//2+1
for i in range(x):
print(i+1, 2*x+1-i)
for i in range(y):
print(i+2*x+2, 2*y+2*x+1-i)
| 0 | null | 14,442,339,842,688 | 13 | 162 |
# pythonの場合、ライブラリ関数でgcdがある?ただし、配列まとめてというわけではない模様
from math import gcd
def main():
N = int(input())
A = list(map(int,input().split()))
pc = True
maxA = max(A)
cnts = [ 0 for _ in range(maxA+1) ]
for a in A:
cnts[a] += 1
for i in range(2,maxA):
if pc:
# 素因数ごとに、A内での登場回数を数える
# 2つあったらその2つのcommon dividerとして、1以外にそれがあることが確定する
cnt = 0
for j in range(i,maxA+1,i):
cnt += cnts[j]
if 1 < cnt:
# Aの素因数で重複があれば、pcでない
pc = False
break
sc = False
if pc == False:
gcdResult = 0
for a in A:
# 配列全要素に対してgcdを呼んでるが、結果的に、それが1になればsc、ならなければnot sc
gcdResult=gcd(gcdResult,a)
if gcdResult == 1:
sc = True
if pc:
print("pairwise coprime")
elif sc:
print("setwise coprime")
else:
print("not coprime")
if __name__ == "__main__":
main()
|
H = int(input())
count = 0
answer = 0
while(H > 1):
H //= 2
answer += 2**count
count += 1
answer += 2**count
print(answer)
| 0 | null | 41,895,627,838,280 | 85 | 228 |
n = int(input())
mod = 10**9 + 7
contain_zero = 0
contain_nine = 0
not_contain_zero_and_nine = 0
all_per = pow(10,n,mod)
if n < 2:
print(0)
else:
contain_zero = all_per - pow(9,n,mod)
contain_nine = all_per - pow(9,n,mod)
not_contain_zero_and_nine = pow(8,n,mod)
print((-(all_per - not_contain_zero_and_nine -contain_nine - contain_zero))%mod)
|
n=int(input())
mod=10**9+7
ans=10**n
ans-=2*(9**n)
ans+=8**n
ans%=mod
print(ans)
| 1 | 3,148,924,462,618 | null | 78 | 78 |
a=int(input())
b=''
for i in range(a):
b=b+'ACL'
print(b)
|
n=int(input())
s='ACL'
print(s*n)
| 1 | 2,164,208,835,236 | null | 69 | 69 |
N=int(input())
A=list(map(int,input().split()))
for i in A:
if i %2==0:
if i %3==0 or i %5==0:
pass
else:
print('DENIED')
exit()
print('APPROVED')
|
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from math import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
return list(map(int,readline().split()))
def readTuples():
return tuple(map(int,readline().split()))
def I():
return int(readline())
n = I()
A = readInts()
cum = list(accumulate(A))
ans = 0
for i in range(n-1):
ans += A[i] * (cum[n-1]-cum[i])
ans%=mod
print(ans%mod)
| 0 | null | 36,633,154,726,860 | 217 | 83 |
H=int(input())
W=int(input())
N=int(input())
cnt=0
black=0
if H>=W:
for i in range(W):
black+=H
cnt+=1
if black>=N:
break
elif H<W:
for i in range(H):
black+=W
cnt+=1
if black>=N:
break
print(cnt)
|
k=int(input())
u='ACL'
print(u*k)
| 0 | null | 45,274,892,592,448 | 236 | 69 |
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF = float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = A[0]
for i in range(N-2):
ans += A[i//2+1]
print(ans)
|
n = input()
sum_of_digits = 0
for d in n:
sum_of_digits += int(d)
print('Yes' if sum_of_digits%9 == 0 else 'No')
| 0 | null | 6,847,092,044,676 | 111 | 87 |
n = int(input())
def fib(i):
if i<=1: return 1
else:
a=0
b=1
for i in range(n+1):
c=b+a
b=a
a=c
return c
print(fib(n))
|
N = int(input())
memo = [-1]*500
def fibo(x):
if memo[x] != -1:
return memo[x]
if x == 0 or x == 1:
ans=1
memo[x] = ans
return ans
ans = fibo(x-1) + fibo(x-2)
memo[x] = ans
return ans
print(fibo(N))
| 1 | 1,908,508,382 | null | 7 | 7 |
N=int(input())
ans=0
for i in range(N+1):
if i%3!=0 and i%5!=0:
ans+=i
print(ans)
|
n,k=map(int,input().split())
k_list=[]
for i in range(k):
l,r=map(int,input().split())
k_list.append([l,r])
dp=[0]*(n+1)
dp[1]=1
dpsum=[0]*(n+1)
dpsum[1]=1
for i in range(1,n):
dpsum[i]=dp[i]+dpsum[i-1]
for j in range(k):
l,r=k_list[j]
li=i+l
ri=i+r+1
if li<=n:
dp[li]+=dpsum[i]
dp[li]=dp[li]%998244353
if ri<=n:
dp[ri]-=dpsum[i]
dp[ri]=dp[ri]%998244353
print(dp[n])
| 0 | null | 18,699,505,906,638 | 173 | 74 |
import collections
cc = collections.Counter()
count = 0
n = raw_input()
for i,e in enumerate(map(int, raw_input().split())):
count += cc[e - i]
cc[-e- i] +=1
print count
|
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
As = list(mapint())
plus = [i+a for i, a in enumerate(As)]
minus = [i-a for i, a in enumerate(As)]
from collections import Counter
pc = Counter(plus)
mc = Counter(minus)
ans = 0
for p, cnt in pc.most_common():
ans += cnt*mc[p]
print(ans)
| 1 | 25,987,399,325,202 | null | 157 | 157 |
d=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split()))for _ in range(d)]
class Score:
def __init__(self,t):
self.x=[d*[0]for _ in range(26)]
self.scor=0
self.ans=[]
for i in range(d):
v=t[i]
self.scor+=s[i][v]
for j in range(26):
if j!=v:
self.x[j][i]+=1
if i!=0:self.x[j][i]+=self.x[j][i-1]
self.scor-=c[j]*self.x[j][i]
self.ans.append(self.scor)
def solve(self):
return [self.scor,self.ans]
t=[int(input())-1 for _ in range(d)]
x=Score(t)
_,ans=x.solve()
for i in ans:print(i)
|
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
temp = list(map(int,input().split()))
S.append(temp)
T = []
for i in range(D):
temp = int(input())
T.append(temp)
since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
manzoku = 0
for i in range(26):
since[i] += C[i]
for i in range(D):
contest = T[i]-1 #0index
manzoku += S[i][contest]
for j in range(26):
if j != contest:
manzoku -= since[j]
print(manzoku)
for j in range(26):
if j != contest:
since[j] += C[j]
else:
since[j] = C[j]
| 1 | 9,927,831,517,028 | null | 114 | 114 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.