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
|
---|---|---|---|---|---|---|
x = int(input())
a = 0
while True:
b = a
while a**5 - b**5 <= x:
if a**5 - b**5 == x:
print(a, b)
exit()
b -= 1
a += 1
|
N,K=map(int, input().split())
d=list(map(int,input().split()))
ans=0
for i in range(N):
if d[i]>=K:
ans=ans+1
print(ans)
| 0 | null | 102,469,062,326,940 | 156 | 298 |
n=str(input())
a=list(n)
b=0
for i in a:
b=b+int(i)
if b%9==0:
print("Yes")
else:
print("No")
|
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def solve():
N, M = MI()
uf = UnionFind(N)
for i in range(M):
a, b = MI1()
uf.union(a, b)
print(uf.group_count()-1)
if __name__ == '__main__':
solve()
| 0 | null | 3,302,677,917,252 | 87 | 70 |
while True:
entry = map(str, raw_input().split())
a = int(entry[0])
op = entry[1]
b = int(entry[2])
if op == "?":
break
elif op == "+":
ans = a + b
elif op == "-":
ans = a - b
elif op == "*":
ans = a * b
elif op == "/":
ans = a / b
print(ans)
|
import sys
s = sys.stdin.readlines()
for i in s:
a, op, b = i.split()
a, b = int(a), int(b)
if op == '?':
break
elif op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
print(a // b)
| 1 | 682,186,616,772 | null | 47 | 47 |
class UnionFind:
def __init__(self, n):
self.n = n
self.par = [i for i in range(n+1)] # �?要�?の親要�?の番号を�?�納するリス�?
self.rank = [0] * (n+1)
self.size = [1] * (n+1) # 根?��ルート)�?�場合、そのグループ�?�要�?数を�?�納するリス�?
# 要�? x が属するグループ�?�根を返す
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
# 要�? x が属するグループと要�? y が属するグループとを併合す�?
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
# 要�? x, y が同じグループに属するかど�?かを返す
def same_check(self, x, y):
return self.find(x) == self.find(y)
# 要�? x が属するグループに属する要�?をリストで返す
def members(self, x):
root = self.find(x)
return [i for i in range(1,self.n+1) if self.find(i) == root]
# 要�? x が属するグループ�?�サイズ?��要�?数?��を返す
def get_size(self, x):
return self.size[self.find(x)]
# すべての根の要�?をリストで返す
def roots(self):
return [i for i, x in enumerate(self.par) if x == i and i != 0]
# グループ�?�数を返す
def group_count(self):
return len(self.roots())
# {ルート要�?: [そ�?�グループに含まれる要�?のリス�?], ...} の辞書を返す
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
n,m = map(int,input().split())
uf = UnionFind(n)
for i in range(m):
a,b = map(int,input().split())
uf.union(a,b)
print(len(uf.roots())-1)
|
n = int(input())
plus_max = 10**20 * -1
plus_min = 10**20
minus_max = 10**20 * -1
minus_min = 10**20
for _ in range(n):
x, y = map(int, input().split())
plus_max = max(plus_max, x + y)
plus_min = min(plus_min, x + y)
minus_max = max(minus_max, x - y)
minus_min = min(minus_min, x - y)
print(max(plus_max - plus_min, minus_max - minus_min))
| 0 | null | 2,819,532,679,142 | 70 | 80 |
import sys
from collections import deque
n = int(sys.stdin.readline())
que = deque()
for i in range(n):
line = sys.stdin.readline()
op = line.rstrip().split()
if "insert" == op[0]:
que.appendleft(op[1])
elif "delete" == op[0]:
if op[1] in que:
que.remove(op[1])
elif "deleteFirst" == op[0]:
que.popleft()
else:
que.pop()
print(*que)
|
from collections import deque
n = int(input())
dlist = deque()
for i in range(n):
code = input().split()
if code[0] == "insert":
dlist.insert(0,code[1])
if code[0] == "delete":
try:
dlist.remove(code[1])
except:
continue
if code[0] == "deleteFirst":
dlist.popleft()
if code[0] == "deleteLast":
dlist.pop()
print(*dlist,sep=" ")
| 1 | 52,477,938,030 | null | 20 | 20 |
n = input()
print("Yes" if "7" in n else "No")
|
while True:
s = raw_input().split()
a = int(s[0])
b = int(s[2])
if s[1] == "?":
break
elif s[1] == '+':
print a + b
elif s[1] == '-':
print a - b
elif s[1] == '*':
print a * b
elif s[1] == '/':
print a / b
| 0 | null | 17,567,578,979,330 | 172 | 47 |
n = int(input())
a_list = list(map(int, input().split()))
sum_l = sum(a_list)
ans = 0
for i in range(n-1):
sum_l -= a_list[i]
ans += sum_l * a_list[i]
print(ans % 1000000007)
|
n = int(input())
a = list(map(int, input().split()))
length = len(a)
total = sum(a)
ans = 0
for i in range(0,length):
total -= a[i]
ans += (a[i] * total)
print(ans % (10**9 + 7))
| 1 | 3,807,715,640,212 | null | 83 | 83 |
n,d=map(int,input().split())
count=0
for i in range(n):
x,y=map(int,input().split())
if d**2>=x**2+y**2:
count+=1
print(count)
|
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
res = [1]
for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
return res
N=II()
AS=LI()
SM=sum(AS)
B=1
res=0
M = 1
m = 1
for a in AS:
B = M-a
if B < 0:
print(-1)
exit(0)
res += a
res += B
SM -= a
M = min(B*2, SM)
m = B
print(res)
| 0 | null | 12,477,683,561,252 | 96 | 141 |
n = int(input())
x = 7
ans = 1
#print(7%1)
for i in range(3*n):
if x%n==0:
break
ans+=1
x= x*10 + 7
x=x%n
if ans < 3*n:
print(ans)
else:
print(-1)
|
k = int(input())
a = 7%k
flag = False
for i in range(k):
if a == 0:
flag = True
ans = i+1
break
a = (10*a+7)%k
if flag:
print(ans)
else:
print(-1)
| 1 | 6,087,302,719,160 | null | 97 | 97 |
# -*- coding: utf-8 -*-
# F
import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
from itertools import accumulate, combinations
input = sys.stdin.readline
mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0] * (S+1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(1, N+1):
for j in range(S+1):
dp[i][j] += dp[i-1][j] * 2
if j + A[i-1] <= S:
dp[i][j+A[i-1]] += dp[i-1][j]
dp[i][j] %= mod
# print(dp)
print(dp[-1][S] % mod)
|
def main():
n = int(input())
if n % 2 == 0 and n >= 10:
m = n // 2
k = 5
m5 = 0
while k <= m:
m5 += m // k
k *= 5
else:
m5 = 0
print(m5)
if __name__ == '__main__':
main()
| 0 | null | 67,003,287,494,692 | 138 | 258 |
time = int(input())
if time != 0:
h = time // 3600
time = time % 3600
m = time // 60
s = time % 60
print(str(h) + ':' + str(m) + ':' + str(s))
else:
print('0:0:0')
|
S = int(input());print('{0}:{1}:{2}'.format(S//3600, S%3600//60, S%60))
| 1 | 331,133,899,808 | null | 37 | 37 |
import sys
input = sys.stdin.readline
N = [int(x) for x in input().rstrip()][::-1] + [0]
L = len(N)
dp = [[10 ** 18, 10 ** 18] for _ in range(L)]
dp[0] = [N[0], 10 - N[0]]
for i in range(1, L):
n = N[i]
dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1))
dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1)))
print(dp[-1][0])
|
def main():
n = input()
l = len(n)
ans = 0
p = 0
for i in range(l-1, -1, -1):
v = int(n[i])
if p == 1:
v += 1
if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
ans += (10-v)
p = 1
else:
ans += v
p = 0
if p == 1:
ans += 1
print(ans)
main()
| 1 | 70,636,504,995,990 | null | 219 | 219 |
# coding: utf-8
# Here your code !
a,b,c = input().split()
if(a < b < c):
print('Yes')
else:
print('No')
|
def main():
x = input()
x1, x2, x3 = x.split(' ')
a = int(x1)
b = int(x2)
c = int(x3)
if a < b < c:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| 1 | 395,713,085,020 | null | 39 | 39 |
from functools import lru_cache
counter = 0
@lru_cache(maxsize=1024)
def fib(n):
global counter
counter += 1
if n in [0, 1]:
return 1
return fib(n - 1) + fib(n - 2)
if __name__ == '__main__':
print(fib(int(input())))
|
n = int(input())
ss = {}
tt = []
for i in range(n):
s, t = map(str, input().split())
ss[s] = i + 1
tt.append(t)
x = input()
print(sum([int(x) for x in tt[ss[x]:]]))
| 0 | null | 48,440,609,116,548 | 7 | 243 |
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-2):
for j in range(i+1, n-1):
sum = l[i] + l[j]
index = bisect.bisect_left(l, sum)
cnt += index - j - 1
print(cnt)
|
from bisect import bisect_left
N = int(input())
L = sorted(list(map(int,input().split())))
cnt = 0
for i in range(N-2):
for j in range(i+1,N-1):
tmp = bisect_left(L,L[i]+L[j])
cnt += tmp - j - 1
print(cnt)
| 1 | 172,280,388,103,840 | null | 294 | 294 |
def main():
N = int(input())
print(int((N+1)/2))
main()
|
import fileinput
if __name__ == '__main__':
for line in fileinput.input():
tokens = line.strip().split()
a, b = int(tokens[0]), int(tokens[1])
c = a+b
print(len(str(c)))
| 0 | null | 29,346,419,642,500 | 206 | 3 |
from collections import defaultdict
def main():
N, P = list(map(int, input().split()))
S = input()
if P in [2, 5]:
ans = 0
# P = 2, 5の時は一番下の位の数だけ見ればカウント可能
for right in range(N - 1, - 1, - 1):
if int(S[right]) % P == 0:
ans += right + 1
print(ans)
return
# 例:S = 3543, P = 3
# 左からn番目 ~ 1番右の数のmod Pの値を計算
# C = [3543, 543, 43, 3] % P = [0, 0, 1, 0]
# C[m] == C[n] なる m < nのペア数 + C[n] == 0なるnの個数を求める
# 3 + 3 = 6
cur_c = 0
C = [0] * N
pw = 1
for n, s in enumerate(S[::-1]):
cur_c = (cur_c + pw * int(s)) % P
C[N - 1 - n] = cur_c
pw = (pw * 10) % P
counter = defaultdict(int)
for c in C:
counter[c] += 1
ans = 0
for c in C:
counter[c] -= 1
ans += counter[c]
ans += len([c for c in C if c == 0])
print(ans)
if __name__ == '__main__':
main()
|
def digitsum(n):
res = 0
for i in n:
#print(ni)
ni = int(i)
res += ni
return res
s = input()
r = digitsum(s)
if r % 9 == 0:
print("Yes")
else:
print("No")
| 0 | null | 31,455,287,612,060 | 205 | 87 |
H,W,N=[int(input()) for i in range(3)]
print(min((N+H-1)//H,(N+W-1)//W))
|
a = int(input())
b = int(input())
n = int(input())
t = max(a, b)
print((n + t - 1) // t)
| 1 | 88,927,140,591,492 | null | 236 | 236 |
def funcs(a, b, c):
x = int(a)
y = int(b)
z = int(c)
if x < y < z:
print("Yes")
else:
print("No")
tmp = input()
a, b, c = tmp.split(" ")
funcs(a, b, c)
|
import sys
a,b,c = map(int, sys.stdin.readline().rstrip().split(' '))
print('Yes' if (a<b<c) else 'No')
| 1 | 375,477,915,008 | null | 39 | 39 |
from fractions import gcd # for python 3.5~, use math.gcd
def main():
N, M = list(map(int, input().split(' ')))
half_A = list(map(lambda x: int(x) // 2, input().split(' ')))
lcm = 1
for half_a in half_A:
lcm *= half_a // gcd(lcm, half_a)
if lcm > M:
print(0)
exit(0)
for half_a in half_A:
if (lcm // half_a) % 2 == 0:
print(0)
exit(0)
num_multiples = M // lcm
print((num_multiples + 1) // 2) # count only cases that ratios are odd numbers.
if __name__ == '__main__':
main()
|
import queue
def s():
N=int(input())
M=[input().split()[2:]for _ in[0]*N]
q=queue.Queue();q.put(0)
d=[-1]*N;d[0]=0
while not q.empty():
u=q.get()
for v in M[u]:
v=int(v)-1
if d[v]<0:d[v]=d[u]+1;q.put(v)
for i in range(N):print(i+1,d[i])
if'__main__'==__name__:s()
| 0 | null | 51,116,368,692,810 | 247 | 9 |
n,k=map(int,input().split())
h=list(map(int,input().split()))
sum=0
for l in h:
if l>=k:
sum+=1
print(sum)
|
n,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
res = 0
for i in range(n):
if k <= a[i]:
res += 1
print(res)
| 1 | 178,659,343,098,070 | null | 298 | 298 |
n = int(input())
l = [0 for i in range(n)]
r = [0 for i in range(n)]
dat = []
dat2 = []
st = -1
for i in range(n):
s = input()
for ch in s:
if ch == '(':
r[i] += 1
pass
else:
if r[i] > 0:
r[i] -= 1
else:
l[i] += 1
pass
# print(i, l[i], r[i])
if l[i] == 0:
if st == -1 or r[st] < r[i]:
st = i
if r[i] >= l[i]:
dat.append((l[i], i))
else:
dat2.append((r[i], i))
dat.sort()
dat2.sort(reverse=True)
if st == -1:
print("No")
exit()
now = r[st]
# print('now={}'.format(now))
for num, i in dat:
if i == st:
continue
if now < l[i]:
print("No")
exit()
now = now - l[i] + r[i]
for num, i in dat2:
# print('dat2', num, i)
if now < l[i]:
print("No")
exit()
now = now - l[i] + r[i]
if now == 0:
print("Yes")
else:
print("No")
|
n = int(input())
t = 0
h = 0
for i in range(n):
taro,hana = input().split()
if taro == hana:
t += 1
h += 1
else:
m = list((taro,hana))
m.sort()
if m == list((taro,hana)):
h += 3
else:
t += 3
print("{} {}".format(t,h))
| 0 | null | 12,936,639,598,962 | 152 | 67 |
def main():
mod=1000000007
s=int(input())
m=s*2
inv=lambda x: pow(x,mod-2,mod)
Fact=[1] #階乗
for i in range(1,m+1):
Fact.append(Fact[i-1]*i%mod)
Finv=[0]*(m+1) #階乗の逆元
Finv[-1]=inv(Fact[-1])
for i in range(m-1,-1,-1):
Finv[i]=Finv[i+1]*(i+1)%mod
def comb(n,r):
if n<r:
return 0
return Fact[n]*Finv[r]*Finv[n-r]%mod
ans=0
num=s//3
for i in range(1,num+1):
n=s-i*3
ans+=comb(n+i-1,n)
ans%=mod
print(ans)
if __name__=='__main__':
main()
|
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
S = I()
mod = 10**9+7
ANS = [1,0,0]
for _ in range(S):
ANS.append((ANS[-1]+ANS[-3]) % mod)
print(ANS[S])
| 1 | 3,297,558,250,990 | null | 79 | 79 |
N = int(input())
S = []
T = []
for i in range(N):
s, t = input().split()
S.append(s)
T.append(int(t))
X = input()
print(sum(T[S.index(X)+1:]))
|
n = int(input())
s, t = [], []
for i in range(n):
ss = input().split()
s.append(ss[0])
t.append(int(ss[1]))
x = input()
start = n
for i in range(n):
if s[i] == x:
start = i + 1
res = 0
for i in range(start, n):
res += t[i]
print(res)
| 1 | 96,879,277,320,162 | null | 243 | 243 |
n = int(input())
for i in range(n):
a = list(map(int, input().split()))
m = max(a)
b = sum([i**2 for i in a if i != m])
if (m**2 == b):
print("YES")
else:
print("NO")
|
import sys
n = int(input())
l = sys.stdin.readlines()
s = ""
for i in l:
x, y, z = sorted(map(lambda x:x*x,map(int, i.split())))
if x + y == z:
s += "YES\n"
else:
s += "NO\n"
print(s,end="")
| 1 | 230,869,440 | null | 4 | 4 |
s, w = map(int, input().split())
ans = "safe" if s > w else 'unsafe'
print(ans)
|
a,b = map(int,input().split())
if b-a>=0:
print('unsafe')
else:
print('safe')
| 1 | 29,057,152,214,090 | null | 163 | 163 |
n = int(raw_input())
print ' '.join(raw_input().split()[::-1])
|
n,k=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
t=""
count=0
for i in range(n):
if i>k-1:
if T[i]=="r":
if t[i-k]!="p":
t=t+"p"
count+=P
else:
t=t+" "
if T[i]=="s":
if t[i-k]!="r":
t=t+"r"
count+=R
else:
t=t+" "
if T[i]=="p":
if t[i-k]!="s":
t=t+"s"
count+=S
else:
t=t+" "
else:
if T[i]=="r":
t=t+"p"
count+=P
if T[i]=="p":
t=t+"s"
count+=S
if T[i]=="s":
t=t+"r"
count+=R
print(count)
| 0 | null | 53,720,830,670,782 | 53 | 251 |
N, M, Q = map(int, input().split())
L = list(tuple(map(int, input().split())) for _ in range(Q))
ans = 0
A = []
def dfs():
global A, ans
if len(A) == N:
res = sum(t[3] for t in L if A[t[1]-1] - A[t[0]-1] == t[2])
ans = max(ans, res)
else:
for a in range(A[-1] if A else 1, M + 1):
A.append(a)
dfs()
A.pop()
dfs()
print(ans)
|
s = input()
week = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
ans_dic = {k: v for k, v in zip(week, range(7, 0, -1))}
print(ans_dic[s])
| 0 | null | 80,395,457,630,490 | 160 | 270 |
from collections import deque
N = int(input())
g = {i: deque() for i in range(1,N+1)} #1-index
for i in range(1, N+1):
u, k, *v = map(int, input().split())
for j in v:
g[u].append(j)
seen = [0]*(N+1) #1-index
que = deque([])
dist = [-1]*(N+1) #1-index
seen[1] = 1
que.append(1)
dist[1] = 0
while que: #queが空になるまで
q_no = que.popleft()
if not g[q_no]: continue #点q-noから他の点に伸びていない
for next_v in g[q_no]:
if seen[next_v] == 1: continue #訪問済
seen[next_v] = 1 #訪問済にする
dist[next_v] = dist[q_no] + 1 #距離情報を格納
que.append(next_v) #queに追加
for i in range(1, N+1):
print(i, dist[i])
|
from collections import deque
n = int(input())
adj = [0]*n # adjacent list
d = [-1]*n # distance from the source vertex
c = ['w']*n # the 'color' of each vertex
# receiving input (the structure of the graph)
for i in range(n):
tmp = list(map( int, input().split() ) )
adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:]))
# set the source vertex
s = 0
d[s] = 0
c[s] = 'g'
Q = deque()
Q.append(s)
while Q:
u = Q.popleft()
for v in adj[u]:
if c[v] == 'w':
c[v] = 'g'
d[v] = d[u] + 1
Q.append(v)
c[u] = 'b'
for i in range(n):
print(f'{i+1} {d[i]}')
| 1 | 4,775,661,990 | null | 9 | 9 |
t = input()
ans = t
tem = ''
if '?' in t :
if len(t) == 1 :
ans = 'D'
elif len(t) == 2 :
if t[0] == '?' and t[1] == '?' :
ans = 'PD'
elif t[0] == '?' and t[1] == 'D':
ans = 'PD'
elif t[0] == '?' and t[1] == 'P':
ans ='DP'
elif t[1] == '?' and t[0] == 'P':
ans = 'PD'
elif t[1] == '?' and t[0] == 'D':
ans = 'DD'
else:
for i in range(0,len(t)):
if i == 0 :
if t[i] == '?' and t[i+1] == 'P':
ans = 'D'
elif t[i] == '?' :
ans = 'P'
else :
ans = t[i]
elif i == len(t)-1 :
if t[i] == '?' :
ans += 'D'
else:
ans += t[i]
else:
if t[i] == '?':
if ans[i-1] == 'P':
ans += 'D'
elif t[i+1] == 'D' or t[i+1] == '?':
ans += 'P'
else:
ans += 'D'
else:
ans += t[i]
print(ans)
|
data = []
try:
while True:
data.append(raw_input())
except EOFError:
pass
n = int(data[0])
if n <= 1000:
for i in range(1, n+1):
x, y, z = data[i].split()
x, y, z = int(x), int(y), int(z)
if x <= 1000 and y <= 1000 and z <= 1000:
if (z * 0.8 == x and z * 0.6 == y
or z * 0.8 == y and z * 0.6 == x
or y * 0.8 == x and y * 0.6 == z
or y * 0.8 == z and y * 0.6 == x
or x * 0.8 == z and x * 0.6 == y
or x * 0.8 == y and x * 0.6 == z):
print "YES"
else:
print "NO"
| 0 | null | 9,251,908,483,200 | 140 | 4 |
import sys
import math
r, c = map(int, raw_input().split())
b = [0 for i in xrange(c+1)]
for i in xrange(r):
a = map(int, raw_input().split())
for j in xrange(c):
b[j] += a[j]
sys.stdout.write(str(a[j]) + " ")
print sum(a)
b[c] += sum(a)
for j in xrange(c+1):
sys.stdout.write(str(b[j]))
if j < c:
sys.stdout.write(" ")
print
|
# coding: utf-8
n, m = map(int, input().split())
total = [0] * m
for i in range(n+1):
if i < n:
line = [int(j) for j in input().split()]
print(" ".join([str(k) for k in line]),sum(line))
total = [total[i] + line[i] for i in range(m)]
else:
print(" ".join([str(k) for k in total]),sum(total))
| 1 | 1,376,958,818,080 | null | 59 | 59 |
from math import pow
print(int(pow(int(input()),2)))
|
def main():
L, R, d = (int(i) for i in input().split())
ans = R//d - (L-1)//d
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 76,091,637,134,718 | 278 | 104 |
def main():
a, b, c = map(int, input().split())
tmp = c - a - b
if tmp > 0 and 4*a*b < tmp*tmp:
print('Yes')
else:
print('No')
main()
|
# Author: cr4zjh0bp
# Created: Mon Mar 23 15:10:04 UTC 2020
import sys
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()
from decimal import *
a, b, c = na()
d, e, f = Decimal(a), Decimal(b), Decimal(c)
if d.sqrt() + e.sqrt() < f.sqrt():
print("Yes")
else:
print("No")
| 1 | 51,779,389,845,348 | null | 197 | 197 |
from copy import deepcopy
N, M = map(int, input().split())
pairs = [0] * M
for i in range(M):
pairs[i] = list(map(int, input().split()))
# N = 5
# pairs = [[1,2], [3,4], [5,1]]
class UnionFind:
def __init__(self, N):
self.r = [-1] * 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)]
uf = UnionFind(N)
for pair in pairs:
a = pair[0] - 1
b = pair[1] - 1
uf.unite(a, b)
ans = 0
for i in range(N):
ans = max(ans, uf.size(i))
print(ans)
|
n = int(input())
a = [int(i) for i in input().split()]
max_a = max(a)
cnt_d = [0] * (max_a + 1)
for ai in a:
for multi in range(ai, max_a + 1, ai):
cnt_d[multi] += 1
print(sum(cnt_d[ai] == 1 for ai in a))
| 0 | null | 9,254,711,986,828 | 84 | 129 |
k=int(input())
if k==1:
print('ACL')
elif k==2:
print('ACLACL')
elif k==3:
print('ACLACLACL')
elif k==4:
print('ACLACLACLACL')
elif k==5:
print('ACLACLACLACLACL')
|
s = ''
for i in range(int(input())):
s += 'ACL'
print(s)
| 1 | 2,195,975,637,112 | null | 69 | 69 |
while True:
x,y = map(int,input().split())
if x == 0 and y == 0:
break
if x > y:
print(f"{y} {x}")
elif x <= y:
print(f"{x} {y}")
|
N,M,X = map(int,input().split())
data = []
ans = 0
for i in range(N):
d = list(map(int,input().split()))
data.append(d)
ans += d[0]
x = 2**N + 1
ans_candidate = []
valid = False
ans += 1
ans_old = ans
for i in range(x):
if i != 0:
for l in range(len(sum)):
if sum[l] >= X and l == len(sum) -1:
valid = True
if sum[l] >= X:
continue
else:
valid = False
break
if valid and price < ans:
ans = price
sum = [0] * M
price = 0
for j in range(N):
if i >> j & 1 == 1:
price += data[j][0]
for k in range(M):
sum[k] += data[j][k+1]
if ans_old == ans:
print(-1)
else:
print(ans)
| 0 | null | 11,506,626,332,280 | 43 | 149 |
X = int(input())
i = 1
while (360*i) % X != 0:
i += 1
ans = 360*i//X
print(ans)
|
x = int(input())
xSum = 0
cnt = 1
while True:
xSum += x
if xSum%360 == 0:
break
cnt += 1
print(cnt)
| 1 | 13,120,415,530,560 | null | 125 | 125 |
H, W, M = map(int, input().split())
bomb = set()
row = [0] * H
column = [0] * W
for i in range(M):
h, w = map(int, input().split())
h -= 1
w -= 1
bomb.add((h, w))
row[h] += 1
column[w] += 1
row_max = max(row)
col_max = max(column)
r_check = []
c_check = []
for i in range(H):
if row[i] == row_max:
r_check.append(i)
for i in range(W):
if column[i] == col_max:
c_check.append(i)
num = len(r_check) * len(c_check)
ans = row_max + col_max - 1
flg = False
if M < num:
ans += 1
else:
for r in r_check:
if flg:
break
for c in c_check:
if not (r, c) in bomb:
ans += 1
flg = True
break
print(ans)
|
# import sys
# input = sys.stdin.readline
import collections
def main():
n = int(input())
s = input()
a_list = []
for i in range(n):
if s[i] == "W":
a_list.append(0)
else:
a_list.append(1)
r_count = sum(a_list)
print(r_count - sum(a_list[0:r_count]))
def input_list():
return list(map(int, input().split()))
def input_list_str():
return list(map(str, input().split()))
if __name__ == "__main__":
main()
| 0 | null | 5,497,602,892,660 | 89 | 98 |
A, B, C = map(int, input().split())
if A+B+C > 21:
print('bust')
else:
print('win')
|
print('bust' if sum(list(map(int,input().split()))) > 21 else 'win')
| 1 | 119,031,304,426,588 | null | 260 | 260 |
n = int(input())
s = input().split()
arr = [int(s[i]) for i in range(n)]
arr.sort()
sum = 0
for i in range(len(arr)):
sum += arr[i]
print(arr[0], arr[len(arr)-1], sum)
|
def main():
n = int(input())
s = input()
if n % 2 == 0:
if s[:n // 2] == s[n // 2:]:
ans = "Yes"
else:
ans = "No"
else:
ans = "No"
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 74,009,081,381,308 | 48 | 279 |
n,k=map(int,input().split())
A=[0]*k
mod=10**9+7
def power(a,x): #a**xの計算
T=[a]
while 2**(len(T))<mod: #a**1,a**2.a**4,a**8,...を計算しておく
T.append(T[-1]**2%mod)
b=bin(x) #xを2進数表記にする
ans=1
for i in range(len(b)-2):
if int(b[-i-1])==1:
ans=ans*T[i]%mod
return ans
for i in range(k):
cnt=power(k//(k-i),n)
now=(k-i)*2
while now<=k:
cnt-=A[now-1]
now+=k-i
A[k-i-1]=cnt
ans=0
for i in range(k):
ans+=(i+1)*A[i]%mod
ans%=mod
print(ans)
|
def main():
N, K = map(int, input().split())
mod = 10**9 + 7
r = 0
D = [0] * (K + 1)
for i in reversed(range(1, K + 1)):
D[i] = pow(K // i, N, mod) - sum(D[::i])
return sum(i * j for i, j in enumerate(D)) % mod
print(main())
| 1 | 36,663,867,736,610 | null | 176 | 176 |
from collections import deque
def solve(n,t):
q=deque([])
for i in range(n):
l=list(input().split())
q.append(l)
time=0
while not len(q)==0:
x=q.popleft()
if int(x[1])-t>0:
x[1]=str(int(x[1])-t)
time+=t
q.append(x)
else:
print(x[0]+" "+str(time+int(x[1])))
time+=int(x[1])
n,t=map(int,input().split())
solve(n,t)
|
time = 0
n, q = map(int, input().split())
qname = list(range(n))
qtime = list(range(n))
ename = []
etime = []
for i in range(n):
inp = input().split()
qname[i], qtime[i] = inp[0], int(inp[1])
while len(qtime) > 0:
if qtime[0] <= q:
time += qtime.pop(0)
ename.append(qname.pop(0))
etime.append(time)
else:
time += q
qname.append(qname.pop(0))
qtime.append(qtime.pop(0)-q)
for (a, b) in zip(ename, etime):
print(a, b)
| 1 | 44,491,576,480 | null | 19 | 19 |
import queue
h,w=map(int,input().split())
s=[]
for _ in range(h):
s.append(input())
ans=0
for i in range(h):
for j in range(w):
if s[i][j]==".":
seen=[[0 for _ in range(w)] for _ in range(h)]
length=[[0 for _ in range(w)] for _ in range(h)]
q=queue.Queue()
q.put([i,j])
seen[i][j]=1
while not q.empty():
ci,cj=q.get()
for ni,nj in [[ci-1,cj],[ci+1,cj],[ci,cj-1],[ci,cj+1]]:
if 0<=ni<h and 0<=nj<w and s[ni][nj]=="." and seen[ni][nj]==0:
q.put([ni,nj])
length[ni][nj]=length[ci][cj]+1
seen[ni][nj]=1
ans=max(ans,length[ci][cj])
print(ans)
|
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
H, W = list(map(int, input().split()))
S = []
for i in range(H):
S.append(input())
def bfs(u):
stack = deque([u])
visited = set()
seen = set()
p = [[INF for j in range(W)] for i in range(H)]
p[u[0]][u[1]] = 0
for i in range(H):
for j in range(W):
if S[i][j] == "#":
p[i][j] = -1
while len(stack) > 0:
v = stack.popleft()
###
visited.add(v)
###
a = (v[0] + 1, v[1])
b = (v[0] , v[1] + 1)
c = (v[0] - 1, v[1])
d = (v[0] , v[1] - 1)
e = [a, b, c, d]
for ee in e:
if ee[0] >= 0 and ee[0] <= H-1 and ee[1] >= 0 and ee[1] <= W-1:
if S[ee[0]][ee[1]] == ".":
if ee not in visited:
p[ee[0]][ee[1]] = min(p[ee[0]][ee[1]], p[v[0]][v[1]] + 1)
if ee not in seen:
stack.append(ee)
seen.add(ee)
ans = 0
for i in range(H):
ans = max(ans, max(p[i]))
return ans
sol = 0
for i in range(H):
for j in range(W):
if S[i][j] == ".":
sol = max(sol, bfs((i,j)))
print(sol)
| 1 | 94,573,485,844,342 | null | 241 | 241 |
def gcd(a, b):
"""Return greatest common divisor using Euclid's Algorithm."""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""Return lowest common multiple."""
return a * b // gcd(a, b)
try:
while True:
a, b = map(int, raw_input().split(' '))
print gcd(a, b), lcm(a, b)
except EOFError:
pass
|
import math
john=[]
while True:
try:
john.append(input())
except EOFError:
break
for i in john:
k=list(map(int,i.split()))
print(int(math.gcd(k[0],k[1])),int(k[0]*k[1]/math.gcd(k[0],k[1])))
| 1 | 671,743,910 | null | 5 | 5 |
import collections
n = int(input())
numbers = []
while n%2 == 0:
numbers.append(2)
n //= 2
i = 3
while i*i <= n:
if n%i == 0:
numbers.append(i)
n //= i
else:
i += 2
if n != 1:
numbers.append(n)
factors = collections.Counter(numbers)
ans = 0
for num in factors.values():
k = 1
while num >= k:
num -= k
ans += 1
k += 1
print(ans)
|
N = int(input())
def factorize(n):
result = []
tmp = n
for i in range(2, int(n**0.5)+1):
if tmp % i == 0:
cnt = 0
while tmp % i == 0:
cnt += 1
tmp //= i
result.append(cnt)
if tmp != 1:
result.append(1)
if len(result) == 0:
result.append(1)
return result
def nearest_sum(n):
num_list = [1]
if n <= 2:
return 1
i = 2
while True:
num_list.append(num_list[-1]+i)
i += 1
if num_list[-1]+i > n:
break
return i-1
if N == 1:
print(0)
else:
ind_list = factorize(N)
cnt = 0
for ind in ind_list:
cnt += nearest_sum(ind)
print(cnt)
| 1 | 16,844,913,382,368 | null | 136 | 136 |
N=int(input())
S=[input()for _ in range(N)]
from collections import*
C=Counter(S)
print('AC','x',C['AC'])
print('WA','x',C['WA'])
print('TLE','x',C['TLE'])
print('RE','x',C['RE'])
|
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10 ** 9)
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()))
N=INT()
memo=[0]*50
memo[0]=memo[1]=1
def rec(n):
if memo[n]:
return memo[n]
memo[n]=rec(n-1)+rec(n-2)
return memo[n]
print(rec(N))
| 0 | null | 4,346,734,300,688 | 109 | 7 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#inf = 10**17
#mod = 10**9 + 7
n = input().rstrip()
k = int(input())
ln = len(n)
# dp[i][j]:左からi桁まで見たとき0でない桁がj個ある場合の数
dp1 = [[0]*(k+1) for _ in range(ln+1)]
dp2 = [[0]*(k+1) for _ in range(ln+1)]
dp2[0][0] = 1
cnt = 0
for i in range(ln):
if n[i] != '0':
if cnt < k:
cnt += 1
dp2[i+1][cnt] = 1
else:
dp2[i+1][cnt] = dp2[i][cnt]
for i in range(ln):
dp1[i+1][0] = 1
for j in range(1, k+1):
dp1[i+1][j] += dp1[i][j] + dp1[i][j-1] * 9
if n[i] != '0':
dp1[i+1][j] += dp2[i][j-1] * (int(n[i])-1)
if j < k:
dp1[i+1][j] += dp2[i][j]
print(dp1[-1][k] + dp2[-1][k])
if __name__ == '__main__':
main()
|
import numpy as np
N = int(input())
A = []
B = []
for i in range(N):
a,b=map(int, input().split())
A.append(a)
B.append(b)
Aarray = np.array(A)
Barray = np.array(B)
medA = np.median(Aarray)
medB = np.median(Barray)
if N%2 == 1:
ans = medB-medA+1
else:
ans = 2*(medB-medA)+1
print(str(int(ans)))
| 0 | null | 46,728,257,395,428 | 224 | 137 |
# -*- coding: utf-8 -*-
import sys
N,K,C=map(int, sys.stdin.readline().split())
S=sys.stdin.readline().strip()
#左から
L=[]
over=-1
L_cnt=0
for i,x in enumerate(S):
if x=="o" and over<i:
L.append(i)
L_cnt+=1
if L_cnt==K: break
over=i+C
#右から
R=[]
over=-1
R_cnt=0
for i,x in enumerate(S[::-1]):
if x=="o" and over<i:
R.append(N-1-i)
R_cnt+=1
if R_cnt==K: break
over=i+C
L.sort()
R.sort()
for x,y in zip(L,R):
if x==y:
print x+1
|
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('')
| 0 | null | 20,973,877,463,100 | 182 | 52 |
# ??\?????????
N = int(input())
r = [int(input()) for i in range(N)]
# ?????§????¨????
max_profit = (-1) * (10 ** 9)
min_value = r[0]
for j in range(1, len(r)):
max_profit = max(max_profit, r[j] - min_value)
min_value = min(min_value, r[j])
print(max_profit)
|
import sys
n = int(input())
minv = int(input())
maxv = -1000000000
for r in map(int,sys.stdin.readlines()):
m = r-minv
if maxv < m:
maxv = m
if m < 0: minv = r
elif m < 0: minv = r
print(maxv)
| 1 | 13,166,798,330 | null | 13 | 13 |
import math
def lcm_base(x, y):
return (x * y) // math.gcd(x, y)
x = int(input())
lcm = lcm_base(360, x)
print(lcm//x)
|
def gcd(a, b):
if a==0:
return b
else:
return gcd(b%a, a)
X = int(input())
LCM = X*360/gcd(360, X)
print(int(LCM/X))
#a, b = map(int, input().split())
#print(gcd(a, b))
| 1 | 13,078,559,639,200 | null | 125 | 125 |
def insertion_sort(arr):
print(" ".join(map(str, arr)))
for i in range(1,len(arr)):
j = i
while arr[j] < arr[j-1] and j > 0:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
print(" ".join(map(str, arr)))
input()
arr = list(map(int, input().split()))
insertion_sort(arr)
|
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 | 6,232,041,130 | null | 10 | 10 |
D = int(input())
c = list(map(int, input().split()))
s = []
c_sum = sum(c)
for i in range(D):
tmp = list(map(int, input().split()))
s.append(tmp)
t = []
for i in range(D):
tmp = int(input())
t.append(tmp-1)
cnt = [-1 for j in range(26)]
now = 0
for i in range(D):
cnt[t[i]] = i
now += s[i][t[i]]
tmp = 0
for j in range(26):
if j != t[i]:
if cnt[t[i]] == -1:
tmp += c[j] * (i + 1)
else:
tmp += c[j] * (i - cnt[j])
now -= tmp
print(now)
|
from collections import defaultdict
N,K=map(int,input().split())
alist=list(map(int,input().split()))
#print(alist)
slist=[0]
for i in range(N):
slist.append(slist[-1]+alist[i])
#print(slist)
sslist=[]
for i in range(N+1):
sslist.append((slist[i]-i)%K)
#print(sslist)
answer=0
si_dic=defaultdict(int)
for i in range(N+1):
if i-K>=0:
si_dic[sslist[i-K]]-=1
answer+=si_dic[sslist[i]]
si_dic[sslist[i]]+=1
print(answer)
| 0 | null | 73,899,365,012,928 | 114 | 273 |
def main():
N, M = tuple([int(_x) for _x in input().split()])
A = [int(_x) for _x in input().split()]
total = sum(A)
if total % (4*M) == 0:
min_vote = total // (4 * M)
else:
min_vote = 1 + total // (4 * M)
cnt = 0
for vot in A:
if vot >= min_vote:
cnt += 1
if (cnt >= M):
print("Yes")
else:
print("No")
main()
|
x, y = map(int, input().split())
l = list(map(int, input().split()))
count = 0
for i in range(x):
if l[i] >= sum(l)*(1/(4*y)):
count += 1
if count >= y:
print("Yes")
else:
print("No")
| 1 | 38,729,370,899,138 | null | 179 | 179 |
def resolve():
# 十進数表記で1~9までの数字がK個入るN桁の数字の数を答える問題
S = input()
K = int(input())
n = len(S)
# dp[i][k][smaller]:
# i:桁数
# K:0以外の数字を使った回数
# smaller:iまでの桁で値以上になっていないかのフラグ
dp = [[[0] * 2 for _ in range(4)] for _ in range(105)]
dp[0][0][0] = 1
for i in range(n):
for j in range(4):
for k in range(2):
nd = int(S[i])
for d in range(10):
ni = i+1
nj = j
nk = k
if d != 0:
nj += 1
if nj > K:
continue
if k == 0:
if d > nd: # 値を超えている
continue
if d < nd:
nk += 1
dp[ni][nj][nk] += dp[i][j][k]
ans = dp[n][K][0] + dp[n][K][1]
print(ans)
if __name__ == "__main__":
resolve()
|
from scipy.sparse import csr_matrix #pypyだとエラーになるので、pythonを使うこと
from scipy.sparse.csgraph import shortest_path, floyd_warshall
import numpy as np
import sys
def input():
return sys.stdin.readline().strip()
N, M, L = list(map(int, input().split()))
A = [0]*M
B = [0]*M
C = [0]*M
for i in range(M):
Ai, Bi, Ci = list(map(int, input().split()))
A[i] = Ai - 1
B[i] = Bi - 1
C[i] = Ci
graph = csr_matrix((C, (A, B)), shape=(N, N))
#print(graph)
dist = floyd_warshall(graph, directed=False)
graph2 = np.full((N, N), np.inf)
graph2[dist <= L] = 1
graph2 = csr_matrix(graph2)
num = floyd_warshall(graph2, directed=False)
num = num.tolist()
Q = int(input())
for q in range(Q):
s, t = list(map(int, input().split()))
if num[s - 1][t - 1] < 10000000:
print(int(num[s - 1][t - 1] - 1))
else:
print(-1)
| 0 | null | 124,760,874,365,780 | 224 | 295 |
N, K = map(int, input().split())
L = [0] * K
R = [0] * K
for i in range(0, K):
L[i], R[i] = map(int, input().split())
moves = [0] * N
moves[0] = 1
rui_wa = [0] * N
rui_wa[0] = 1
for i in range(1, N):
for j in range(0, K):
l = max(i - L[j], 0)
r = max(i - R[j], 0)
if i - L[j] < 0:
continue
moves[i] += (rui_wa[l] - rui_wa[r - 1]) % 998244353
rui_wa[i] = (moves[i] + rui_wa[i - 1]) % 998244353
print(moves[N - 1] % 998244353)
|
# coding: utf-8
alphabet = 'abcdefghijklmnopqrstuvwxyz'
S = ''
while True:
try:
n = input()
S += n
except EOFError:
break
ans_dict = {}
for char in alphabet:
ans_dict[char] = S.count(char)
ans_dict[char] += S.count(char.upper())
for k, v in sorted(ans_dict.items(), key=lambda x: x[0]):
print('{} : {}'.format(k, v))
| 0 | null | 2,198,506,662,406 | 74 | 63 |
from collections import deque
import sys
def input():
return sys.stdin.readline().strip()
q = deque(input())
Q = int(input())
flipped = False
for _ in range(Q):
q_t, *q_b = input().split()
if q_t == "1":
flipped = not flipped
else:
F, C = q_b
if (F == "2" and not flipped) or (F == "1" and flipped):
q.append(C)
else:
q.appendleft(C)
q = list(q)
if flipped:
print("".join(q[::-1]))
else:
print("".join(q))
|
from collections import deque
s = input()
Q = int(input())
que = deque(s)
cnt = 0
for i in range(Q):
q = input().split()
if q[0] == '1':
cnt += 1
else:
reverse = cnt % 2
if q[1] == '1':
if reverse == 0:
s = que.appendleft(q[2])
else:
s = que.append(q[2])
else:
if reverse == 0:
s = que.append(q[2])
else:
s = que.appendleft(q[2])
ans = ''.join(que)
if cnt % 2 == 0:
print(ans)
else:
print(ans[::-1])
| 1 | 57,572,516,892,280 | null | 204 | 204 |
import itertools
N = int(input())
L = list(map(int,(input().split())))
L.sort()
Combi = itertools.combinations(L, 3)
count = 0
for i, j, k in Combi:
if((i+j)> k and i!=j!=k and i<j <k ):
count+=1
print(count)
|
petern = 0
n = input()
Ln = input()
L = list(map(int, Ln.split(' ')))
for i in range(0, len(L)) :
moji1 = L[i]
for j in range(i+1, len(L)):
moji2 = L[j]
for k in range(j+1, len(L)):
moji3 = L[k]
if moji1 != moji2 and moji2 != moji3 and moji3 != moji1 :
if moji1+moji2 > moji3 and moji2+moji3 > moji1 and moji3+moji1 > moji2 :
petern += 1
print(petern)
| 1 | 5,091,712,856,058 | null | 91 | 91 |
val = input()
print(val*val)
|
n = int(input())
my_result = n * n
print(int(my_result))
| 1 | 145,462,103,929,548 | null | 278 | 278 |
num = int(input())
A = input().split(" ")
B = A.copy()
#bubble sort
for i in range(num):
for j in range(num-1,i,-1):
#英字ではなく、数字で比較する
m1 = int(A[j][1:])
m2 = int(A[j-1][1:])
if m1 < m2:
A[j],A[j-1] = A[j-1],A[j]
print(*A)
print("Stable")
#selection sort
for i in range(num):
minv = i
for j in range(i+1,num):
m1 = int(B[minv][1:])
m2 = int(B[j][1:])
if m1 > m2:
minv = j
B[i],B[minv] = B[minv],B[i]
print(*B)
if A == B:
print("Stable")
else:
print("Not stable")
|
n=int(input())
num_list1=list(map(str,input().split()))
num_list2=[]
input_list=[]
for k in num_list1:
num_list2.append(k)
input_list.append(k)
def bubble(num_list):
i=0
flag=True
while flag:
flag=False
for j in range(len(num_list)-1,i,-1):
if int(num_list[j-1][1])>int(num_list[j][1]):
tmp=num_list[j-1]
num_list[j-1]=num_list[j]
num_list[j]=tmp
flag=True
return num_list
def select(num_list):
for i in range(0,len(num_list)):
min=i
for j in range(min+1,len(num_list)):
if int(num_list[min][1])>int(num_list[j][1]):
min=j
tmp=num_list[i]
num_list[i]=num_list[min]
num_list[min]=tmp
return num_list
def stable(input_list,output_list):
for i in range(0,len(input_list)):
for j in range(i+1,len(output_list)):
for k in range(0,len(output_list)):
for l in range(k+1,len(output_list)):
if input_list[i][1]==input_list[j][1] and input_list[i]==output_list[l] and input_list[j]==output_list[k]:
return "Not stable"
return "Stable"
bubble=bubble(num_list1)
select=select(num_list2)
print(" ".join(bubble))
print(stable(input_list,bubble))
print(" ".join(select))
print(stable(input_list,select))
| 1 | 26,148,348,968 | null | 16 | 16 |
N = int(input())
R = []
for _ in range(N):
X, L = map(int, input().split())
R.append([X-L, X+L])
R = sorted(R, key=lambda x: x[1])
ans = 1
p = R[0][1]
for i in range(1, N):
if p <= R[i][0]:
ans += 1
p = R[i][1]
print(ans)
|
n = int(input())
xl = []
for _ in range(n):
x, l = map(int,input().split())
xl.append((x-l, x+l, x, l))
xl.sort()
count = n
flag = 0
for i in range(n-1):
if flag == 1: # this machine was removed.
flag = 0
else:
left, right, x, l = xl[i]
if xl[i+1][0] < right: # overlap
if xl[i+1][1] <= right: # i includes i+1
count -= 1 # remove i
else:
flag = 1
count -= 1 # remove i+1
print(count)
| 1 | 90,057,856,163,590 | null | 237 | 237 |
# 1 <= N <= 1000000
N = int(input())
total = []
# N項目までに含まれる->N項目は含まない。だからN項目は+1で外す。
for x in range(1, N+1):
if x % 15 == 0:
"FizzBuzz"
elif x % 5 == 0:
"Buzz"
elif x % 3 == 0:
"Fizz"
else:
total.append(x) #リストに加える
print(sum(total))
|
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())
mod = 10**9+7
bits = [0]*70
for a in As:
for l in range(a.bit_length()):
if (a>>l)&1:
bits[l] += 1
ans = 0
for i in range(70):
one = bits[i]
zero = N-one
mul = pow(2, i, mod)
ans += one*zero*mul
ans %= mod
print(ans)
| 0 | null | 78,648,675,114,150 | 173 | 263 |
h,n = (int(a) for a in input().split())
a = list(map(int,input().split()))
if sum(a) >= h :
print("Yes")
else : print("No")
|
N = int(input())
A = list(map(int, input().split()))
A.sort()
MAX = 10**6+1
cnt = [0]*MAX
for x in A:
if cnt[x] != 0:
cnt[x] = 2
else:
for i in range(x, MAX, x):
cnt[i] += 1
ans = 0
for x in A:
# 2回以上通った数字 (cnt[x]>1) は,その数字より小さい約数が存在する (割り切れる他の数が存在する)。
if cnt[x] == 1:
ans += 1
print(ans)
| 0 | null | 46,079,996,614,080 | 226 | 129 |
import sys
for line in sys.stdin:
a, b = sorted(map(int, line.split(' ')))
gcd = 1
d = 2
while a >= d:
amod, bmod = a % d, b % d
if amod == 0 and bmod == 0:
gcd *= d
a, b = a/d, b/d
d = 2
continue
else:
d += 1
lcm = gcd * a * b
print gcd, lcm
|
N =int(input())
c = 0
for i in range(N):
c += (N-1)//(i+1)
print(c)
| 0 | null | 1,301,726,192,220 | 5 | 73 |
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]))
|
#!/usr/bin/env python3
r, c, k = map(int, input().split())
items = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
R, C, V = map(int, input().split())
items[R][C] = V
dp1 = [[0] * (c+1) for _ in range(r+1)]
dp2 = [[0] * (c+1) for _ in range(r+1)]
dp3 = [[0] * (c+1) for _ in range(r+1)]
for i in range(1, r+1):
for j in range(1, c+1):
v = items[i][j]
dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + v, dp2[i-1][j] + v, dp3[i-1][j] + v)
dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1] + v)
dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1] + v)
ans = max(dp1[r][c], dp2[r][c], dp3[r][c])
print(ans)
| 1 | 5,557,243,510,700 | null | 94 | 94 |
import sys
import numpy as np
sys.setrecursionlimit(10 ** 7)
N, K = map(int, input().split())
MOD = 10 ** 9 + 7
# 階乗、Combinationコンビネーション(numpyを使う)
def cumprod(arr, MOD):
L = len(arr)
Lsq = int(L**.5+1)
arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
for n in range(1, Lsq):
arr[:, n] *= arr[:, n-1]
arr[:, n] %= MOD
for n in range(1, Lsq):
arr[n] *= arr[n-1, -1]
arr[n] %= MOD
return arr.ravel()[:L]
def make_fact(U, MOD):
x = np.arange(U, dtype=np.int64)
x[0] = 1
fact = cumprod(x, MOD)
x = np.arange(U, 0, -1, dtype=np.int64)
x[0] = pow(int(fact[-1]), MOD-2, MOD)
fact_inv = cumprod(x, MOD)[::-1]
return fact, fact_inv
U = 10**6
fact, fact_inv = make_fact(N * 2 + 10, MOD)
fact, fact_inv = fact.tolist(), fact_inv.tolist()
def mod_comb_k(n, k, mod):
return fact[n] * fact_inv[k] % mod * fact_inv[n - k] % mod
ans = 0
for i in range(N):
if K < i:
continue
if N - 1 <= K:
ans = mod_comb_k(N + N - 1, N - 1, MOD)
break
if i == 0:
ans += 1
continue
a = int(mod_comb_k(N - 1, i, MOD)) * int(mod_comb_k(N, i, MOD))
a %= MOD
ans += a
ans %= MOD
'''
a = int(fact[N]) * int(fact_inv[i]) % MOD * int(fact_inv[N - 1])
a = a * int(fact[N-1]) % MOD * int(fact_inv[i]) % MOD * \
int(fact_inv[N-i-1]) % MOD
ans = (a + ans) % MOD
'''
print(ans)
|
N = int(input())
arr = list(map(int, input().split()))
result = 1
if 0 in arr:
result = 0
else:
for i in arr:
result *= i
if result > 10**18:
result = -1
break
print(result)
| 0 | null | 41,703,762,626,022 | 215 | 134 |
s = input()
days = ["", "SAT", "FRI", "THU", "WED", "TUE", "MON", "SUN"]
print(days.index(s))
|
N = int(input())
A = N // 2
B = N % 2
print(A + B)
| 0 | null | 95,989,864,181,860 | 270 | 206 |
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, k = map(int, readline().split())
p = list(map(int, readline().split()))
tmp = [(i+1)/2 for i in p]
cs = list(np.cumsum(tmp))
if n == k:
print(cs[-1])
exit()
ans = 0
for i in range(n - k):
ans = max(ans, cs[i + k] - cs[i])
print(ans)
|
n = int(input())
graph = [[] for _ in range(n)]
for i in range(n):
in_list = list(map(int, input().split()))
u = in_list[0]
graph[u - 1] = in_list[2:]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]
time = 0
def dfs(now, prev, graph, d, f):
if d[now] != 0:
return d, f
global time
time += 1
d[now] = time
if not graph[now]:
f[now] = time + 1
time += 1
return
for k in graph[now]:
if k - 1 == prev or d[k-1] !=0:
continue
dfs(k - 1, now, graph, d, f)
else:
time += 1
f[now] = time
return d, f
for i in range(n):
d, f = dfs(i, 0, graph, d, f)
print(i+1, d[i], f[i])
| 0 | null | 37,663,944,632,562 | 223 | 8 |
n=int(input())
import bisect
L=[]
for i in range(n):
x,l=map(int,input().split())
a=x-l+0.1
b=x+l-0.1
L.append((b,a))
L=sorted(L)
rob=1
bottom=L[0][0]
for i in range(1,n):
if L[i][1]>bottom:
rob += 1
bottom=L[i][0]
print(rob)
|
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 8 10:56:33 2018
@author: maezawa
"""
n = int(input())
adj = [[] for _ in range(n)]
d = [0 for _ in range(n)]
f = [0 for _ in range(n)]
for j in range(n):
ain = list(map(int, input().split()))
u = ain[0]
k = ain[1]
for i in range(k):
adj[u-1].append(ain[i+2]-1)
adj[u-1].sort()
#print(*adj[u-1])
t = 1
for j in range(n):
if d[j] == 0:
stack = [j]
while stack:
#print(stack)
current = stack[-1]
if d[current] == 0:
d[current] = t
t += 1
flag = 0
for k in adj[current]:
if d[k] == 0:
stack.append(k)
flag = 1
break
if flag == 0:
f[current] = t
t += 1
stack.pop()
#print('current = {}, t = {}'.format(current, t))
#print('stack', *stack)
for i in range(n):
print('{} {} {}'.format(i+1, d[i], f[i]))
| 0 | null | 44,948,506,246,540 | 237 | 8 |
k = int(input())
s = input()
print(s[:k] + ('...' if len(s) > k else ''))
|
N , M , X = list(map(int, input().split()))
C = []
for _ in range(N):
C.append(list(map(int, input().split())))
ans = 10**9
for i in range(2**N):
i_bin = (bin(i)[2:]).zfill(N)
flag = True
for j in range(1,M+1):
tot = sum([ int(i_bin[k])* C[k][j] for k in range(N)])
if tot < X:
flag = False
am = sum([ int(i_bin[k]) * C[k][0] for k in range(N)])
if flag and am<ans:
ans = am
if ans == 10**9:
print(-1)
else:
print(ans)
| 0 | null | 20,888,342,898,608 | 143 | 149 |
n, k = map(int, input().split())
positions = [int(i) for i in input().split()]
scores = [int(i) for i in input().split()]
if max(scores) <= 0:
print(max(scores))
else:
positions = [0] + positions
scores = [0] + scores
max_point = -pow(10, 9)
#print(max_point)
dic = {}
for i in range(1, n + 1):
f_pos = i
#print('f_pos',f_pos)
if f_pos not in dic:
point_loop = 0
loop = 0
pos_set = set([f_pos])
while True:
f_pos = positions[f_pos]
point_loop += scores[f_pos]
#print('pos', f_pos)
#print(point)
loop += 1
if f_pos in pos_set:
for j in pos_set:
dic[j] = [loop, point_loop]
break
pos_set.add(f_pos)
else:
loop, point_loop = dic[f_pos]
#print(loop, point)
loop_c = k // loop
r = k % loop
if loop_c == 0 or point_loop <= 0:
if point_loop <= 0:
if loop_c > 0:
r = loop
point = 0
for _ in range(r):
f_pos = positions[f_pos]
point += scores[f_pos]
max_point = max(max_point, point)
else:
point1, point2 = 0, 0
point1 = loop_c * point_loop
max_point1 = point1
for _ in range(r):
f_pos = positions[f_pos]
point1 += scores[f_pos]
max_point1 = max(point1, max_point1)
f_pos = i
point2 = (loop_c - 1) * point_loop
max_point2 = point2
for _ in range(loop):
f_pos = positions[f_pos]
point2 += scores[f_pos]
max_point2 = max(point2, max_point2)
max_point = max(max_point, max(max_point1, max_point2))
print(max_point)
|
import sys
readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')
def solve():
n, k = nm()
p = nl()
_c = nl()
c = [0]*n
for i in range(n):
p[i] -= 1
c[i] = _c[p[i]]
m = 31
MIN = - (1 << 63)
vertex = list()
score = list()
vertex.append(p)
score.append(c)
for a in range(1, m+1):
p_ath = [0] * n
c_ath = [0] * n
for i in range(n):
p_ath[i] = vertex[a-1][vertex[a-1][i]]
c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
vertex.append(p_ath)
score.append(c_ath)
prv = [[MIN, 0] for _ in range(n)]
nxt = [[MIN, MIN] for _ in range(n)]
for b in range(m, -1, -1):
for i in range(n):
if (k >> b) & 1:
nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
else:
nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
nxt[i][0] = max(nxt[i][0], prv[i][0])
nxt[i][1] = max(nxt[i][1], prv[i][1])
prv, nxt = nxt, prv
ans = max(max(x) for x in prv)
if ans == 0:
ans = max(c)
print(ans)
return
solve()
| 1 | 5,338,880,794,752 | null | 93 | 93 |
r = input()
p = 3.141592653589
print "%.6f %.6f" % (p*r*r,2*p*r)
|
diceA = list(map(int, input().split()))
diceB = [[1,2,3,5,4,2],[2,1,4,6,3,1],[3,1,2,6,5,1],[4,1,5,6,2,1],[5,1,3,6,4,1],[6,2,4,5,3,2]]
q = int(input())
for i in range(q):
quest = list(map(int, input().split()))
ans = 0
for j in range(6):
if quest[0] == diceA[j]:
for k in range(6):
if quest[1] == diceA[k]:
for l in range(1,5):
if diceB[j][l] == k+1:
ans = diceB[j][l+1]
print(diceA[ans-1])
| 0 | null | 444,555,626,372 | 46 | 34 |
import sys
if __name__ == '__main__':
letter_counts = [0] * 26 # ?????¢????????????????????????????????° [0]???'a'???[25]???'z'??????????????°
for line in sys.stdin:
# ?°?????????¨??§??????????????\????????????
lower_text = line.lower()
# ??¢???????????????????????¨??????????????°???????¨?
for c in lower_text:
if 'a' <= c <= 'z':
letter_counts[ord(c)-ord('a')] += 1
# ????????? 'a : a????????°' ?????¢??§z?????§1????????¨?????????
for i, n in enumerate(letter_counts):
print('{0} : {1}'.format(chr(ord('a')+i), n))
|
a = 'abcdefghijklmnopqrstuvwxyz'
b = [0 for i in range(len(a))]
while True:
try:
S = raw_input().lower()
except:
break
for j in range(26):
b[j] += S.count(a[j])
for i in range(len(a)):
print str(a[i])+" : "+str(b[i])
| 1 | 1,657,175,816,672 | null | 63 | 63 |
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
d=[0 for i in range(pow(10,5)+1)]
s=sum(a)
for i in range(n):
d[a[i]]+=1
for i in range(q):
s+=(bc[i][1]-bc[i][0])*d[bc[i][0]]
print(s)
d[bc[i][1]]+=d[bc[i][0]]
d[bc[i][0]]=0
|
#!/usr/bin/env python3
def next_line():
return input()
def next_int():
return int(input())
def next_int_array_one_line():
return list(map(int, input().split()))
def next_int_array_multi_lines(size):
return [int(input()) for _ in range(size)]
def next_str_array(size):
return [input() for _ in range(size)]
def main():
n = next_int()
ar = next_int_array_one_line()
num = [0] * 100001
for a in ar:
num[a] += 1
q = next_int()
res = 0
for i in range(len(num)):
res += num[i] * i
for i in range(q):
b, c = map(int, input().split())
res += (c-b) * num[b]
num[c] += num[b]
num[b] = 0
print(res)
if __name__ == '__main__':
main()
| 1 | 12,114,879,432,292 | null | 122 | 122 |
x1, y1, x2, y2 = map(float,input().split())
print(f'{((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5:8f}')
|
n = int(input())
a = []
for i in range(n):
s,t = map(str, input().split())
t = int(t)
a.append([s,t])
x = input()
a.reverse()
ss = 0
for i in range(n):
if a[i][0] == x:
print (ss)
exit()
ss += a[i][1]
print (ss)
| 0 | null | 48,678,882,242,418 | 29 | 243 |
N = int(input())
Count = 0
for T in range(1,N+1):
if not (T%3==0 or T%5==0):
Count = Count+T
print(Count)
|
n = int(input())
print(sum(i for i in range(1, n + 1) if i % 3 != 0 and i % 5 != 0))
| 1 | 34,780,297,610,208 | null | 173 | 173 |
n = int(input())
ans = 0
t = 0
J = 0
for i in range(1,n):
for j in range(i,n):
if i*j >= n:break
if i == j : t+=1
ans +=1
J = j
print(2*ans -t)
|
n=int(input())
def f(i):
if n%i==0:
return n//i-1
else:
return n//i
sum=0
for i in range(1,n):
sum+=f(i)
print(sum)
| 1 | 2,580,801,955,302 | null | 73 | 73 |
h,n = map(int,input().split())
Mag = []
for i in range(n):
AB = list(map(int,input().split()))
Mag.append(AB)
dp = [[float("inf")]*(h+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
for j in range(h+1):
dp[i+1][j] = min(dp[i+1][j],dp[i][j])
dp[i+1][min(j+Mag[i][0],h)] = min(dp[i+1][min(j+Mag[i][0],h)],dp[i+1][j]+Mag[i][1])
print(dp[n][h])
|
h,n=map(int,input().split())
a_=0
magic=[]
for i in range(n):
a,b=map(int,input().split())
a_=max(a,a_)
c=[a,b]
magic.append(c)
dp=[10**8+1]*(h+a_+1)
dp[0]=0
for i in range(n):
a,b=magic[i]
for j in range(len(dp)):
if j+a<=h+a_:
dp[j+a]=min(dp[j+a],dp[j]+b)
print(min(dp[h:]))
| 1 | 81,097,060,031,390 | null | 229 | 229 |
n, m, l = [int(i) for i in input().split()]
A = []
B = []
C = []
for ni in range(n):
A.append([int(i) for i in input().split()])
for mi in range(m):
B.append([int(i) for i in input().split()])
for i in range(n):
C.append([])
for j in range(l):
C[i].append(0)
for k in range(m):
C[i][j] += A[i][k] * B[k][j]
for li in range(n):
print(" ".join([str(s) for s in C[li]]))
|
# coding: utf-8
# Here your code !
def func():
try:
(n,m,l) = [ int(item) for item in input().rstrip().split(" ") ]
matrix_A = [ [ int(item) for item in input().rstrip().split(" ") ] for i in range(n) ]
matrix_B = [ [ int(item) for item in input().rstrip().split(" ") ] for i in range(m) ]
except:
return inputError()
matrix_C = [ [ sum([ matrix_A[i][j]*matrix_B[j][k] for j in range(m) ]) for k in range(l) ] for i in range(n) ]
result=""
for vector in matrix_C:
for item in vector:
result+=str(item)+" "
result=result.rstrip()
result+="\n"
print(result.rstrip())
def inputError():
'''
print("input Error")
return -1
'''
print("input Error")
return -1
func()
| 1 | 1,442,032,857,510 | null | 60 | 60 |
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A = sorted(A)
F = sorted(F, reverse=True)
def check(x):
s = 0
for i in range(N):
s += max(0, A[i] - x // F[i])
return s <= K
l = -1
r = 10 ** 18
while r - l > 1:
m = (r + l) // 2
if check(m):
r = m
else:
l = m
print(r)
|
#!/usr/bin/env python3
import sys
INF = float("inf")
def solve(T: "List[int]", A: "List[int]", B: "List[int]"):
if A[0] < B[0]:
A, B = B, A
if A[1] > B[1]:
print(0)
return
elif A[0]*T[0]+A[1]*T[1] > B[0]*T[0]+B[1]*T[1]:
print(0)
return
elif A[0]*T[0]+A[1]*T[1] == B[0]*T[0]+B[1]*T[1]:
print("infinity")
return
K = (B[0]*T[0]+B[1]*T[1])-(A[0]*T[0]+A[1]*T[1])
L = (A[0]-B[0])*T[0] // K
if (B[0]-A[0])*T[0] % K == 0:
print(2*L)
else:
print(2*L+1)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
T = [int(next(tokens)) for _ in range(2)] # type: "List[int]"
A = [int(next(tokens)) for _ in range(2)] # type: "List[int]"
B = [int(next(tokens)) for _ in range(2)] # type: "List[int]"
solve(T, A, B)
if __name__ == '__main__':
main()
| 0 | null | 148,749,717,883,870 | 290 | 269 |
#!/usr/bin/env python3
import collections as cl
import sys
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():
_a, _b = input().split()
a = _a * int(_b)
b = _b * int(_a)
print(a if a < b else b)
main()
|
n = int(input())
for i in range(n):
if (i+1)%3==0:
print(" "+str(i+1), end="")
else:
x = i+1
while(x>0):
if x%10==3:
print(" "+str(i+1), end="")
break
else:
x = x//10
print("")
| 0 | null | 42,856,634,321,660 | 232 | 52 |
X,Y = map(int,input().split())
MAX_NUM = 10**6 + 1
MOD = 10**9+7
fac = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv = [0 for _ in range(MAX_NUM)]
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2,MAX_NUM):
fac[i] = fac[i-1] * i % MOD
inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
finv[i] = finv[i-1] * inv[i] % MOD
def combinations(n,k):
if n < k:
return 0
if n < 0 or k < 0:
return 0
return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
if (X+Y)%3 != 0:
answer = 0
else:
up = -(X+Y)//3+Y
right = -(X+Y)//3+X
answer = combinations(up+right,up)
print(answer)
|
N = int(input())
S = input()
count = 0
RGBlist = [[S.count('R')],[S.count('G')],[S.count('B')]]
for i in range(1,N):
RGBlist[0].append(RGBlist[0][-1] - (S[i-1] == 'R'))
RGBlist[1].append(RGBlist[1][-1] - (S[i-1] == 'G'))
RGBlist[2].append(RGBlist[2][-1] - (S[i-1] == 'B'))
for i in range(N-2):
si = S[i]
for j in range(i+1,N-1):
sj = S[j]
if si==sj:
continue
else:
RGB = set(['R','G','B'])
RGB -= set([si,sj])
k=j+1
kji=j-i
if RGB == {'R'}:
rgb = 0
elif RGB =={'G'}:
rgb = 1
else:
rgb = 2
if k+kji-1 < len(S):
count += RGBlist[rgb][k] - ({S[k+kji-1]} == RGB)
else:
count += RGBlist[rgb][k]
print(count)
| 0 | null | 93,160,634,998,480 | 281 | 175 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N, X, Y = mapint()
X, Y = X-1, Y-1
dist = [0]*(N-1)
for i in range(N-1):
for j in range(i+1, N):
d = min(j-i, abs(i-X)+abs(j-Y)+1, abs(i-Y)+abs(j-X)+1)
dist[d-1] += 1
for k in range(N-1):
print(dist[k])
|
# coding: utf-8
import math
import numpy as np
#N = int(input())
#A = list(map(int,input().split()))
x = int(input())
#x, y = map(int,input().split())
for i in range(1,10**5):
if (i*x)%360==0:
print(i)
break
| 0 | null | 28,710,883,421,572 | 187 | 125 |
h = -1
w = -1
while (h != 0) and (w != 0):
input = map(int, raw_input().split(" "))
h = input[0]
w = input[1]
if (h == 0 and w == 0):
break
i = 0
j = 0
line = ""
while j < w:
line += "#"
j += 1
while i < h:
print line
i += 1
print ""
|
while True:
H,W = map(int, input().split())
if H == 0 and W == 0:
break
a = "#" * W
for i in range(H):
print(a)
print()
| 1 | 771,737,482,880 | null | 49 | 49 |
N = int(input())
A = list(map(int, input().split()))
max_a = max(A) + 1
counter = [0 for _ in range(max_a)]
for i in A:
for j in range(i, max_a, i):
counter[j] += 1
res = 0
for a in A:
if counter[a] == 1:
res += 1
#print(counter)
print(res)
|
import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]
#隣接リスト 1-order
def make_adjlist_d(n, edges):
res = [[] for _ in range(n + 1)]
for edge in edges:
res[edge[0]].append(edge[1])
res[edge[1]].append(edge[0])
return res
def make_adjlist_nond(n, edges):
res = [[] for _ in range(n + 1)]
for edge in edges:
res[edge[0]].append(edge[1])
return res
#nCr
def cmb(n, r):
return math.factorial(n) // math.factorial(r) // math.factorial(n - r)
def main():
N = NI()
A = NLI()
A = sorted(A)
ma = A[-1]
hurui = [0] * (ma + 10)
for a in A:
if hurui[a] == 0:
hurui[a] = 1
else:
hurui[a] = 10
for a in A:
if hurui[a] == 0:
continue
for i in range(a*2, ma+10, a):
hurui[i] = 0
print(hurui.count(1))
if __name__ == "__main__":
main()
| 1 | 14,414,158,745,888 | null | 129 | 129 |
S=str(input())
if 'SUN'in S:
print(7)
elif 'MON' in S:
print(6)
elif 'TUE' in S:
print(5)
elif 'WED' in S:
print(4)
elif 'THU' in S:
print(3)
elif 'FRI' in S:
print(2)
elif 'SAT' in S:
print(1)
|
s = input()
x = 7
if s == "SUN":
pass
elif s == "SAT":
x = x - 6
elif s == "FRI":
x = x - 5
elif s == "THU":
x = x - 4
elif s == "WED":
x = x - 3
elif s == "TUE":
x = x - 2
elif s == "MON":
x = x - 1
print(x)
| 1 | 133,042,513,388,510 | null | 270 | 270 |
n="0"+input()
k=int(input())
dp=[[[0]*5,[0]*5] for _ in range(len(n))]
dp[0][1][0]=1
for i in range(1,len(n)):
x=int(n[i])
for j in range(4):
if dp[i-1][1][j]:
dp[i][1][j+(x!=0)]=1
if x>1:
dp[i][0][j+1]+=(x-1)
dp[i][0][j]+=1
elif x==1:
dp[i][0][j]+=1
if dp[i-1][0][j]:
dp[i][0][j]+=dp[i-1][0][j]
dp[i][0][j+1]+=(dp[i-1][0][j]*9)
dp[i][0][0]=1
print(dp[-1][0][k]+dp[-1][1][k])
|
def prime_decomposition(n):
i = 2
table = []
while i * i <= n:
while n % i == 0:
n //= i
table.append(i)
i += 1
if n > 1:
table.append(n)
return table
from itertools import groupby
N = int(input())
P = prime_decomposition(N)
ans = 0
for v, g in groupby(P):
n = len(list(g))
for i in range(1, 100000):
n -= i
if n >= 0:
ans += 1
else:
break
print(ans)
| 0 | null | 46,336,533,362,282 | 224 | 136 |
def A():
n, x, t = map(int, input().split())
print(t * ((n+x-1)//x))
def B():
n = list(input())
s = 0
for e in n:
s += int(e)
print("Yes" if s % 9 == 0 else "No")
def C():
int(input())
a = list(map(int, input().split()))
l = 0
ans = 0
for e in a:
if e < l: ans += l - e
l = max(l, e)
print(ans)
C()
|
x,k,d = map(int,input().split())
count = abs(x)//d
if x<0:
before_border = x+d*count
after_border = x+d*(count+1)
else:
before_border = x-d*count
after_border = x-d*(count+1)
if count >= k:
if x<0:
print(abs(x+d*k))
else:
print(abs(x-d*k))
else:
if (count-k)%2 == 0:
print(abs(before_border))
else:
print(abs(after_border))
| 0 | null | 4,870,757,997,998 | 88 | 92 |
x, k, d = map(int, input().split())
if x < 0:
x = -x
if k * d <= x:
print(x - k * d)
else:
t = x // d
x -= t * d
k -= t
if x >= d:
x -= d
k -= 1
if k % 2 == 1:
x -= d
print(abs(x))
|
H,N=map(int,input().split())
A=map(int,input().split())
if H<=sum(A):
print('Yes')
else:
print('No')
| 0 | null | 41,519,916,797,008 | 92 | 226 |
from collections import deque
import sys
N, u, v = map(int, input().split())
u -= 1; v -= 1
edge = [[] for _ in range(N)]
for s in sys.stdin.readlines():
A, B = map(lambda x: int(x) - 1, s.split())
edge[A].append(B)
edge[B].append(A)
INF = float('inf')
# Aoki
pathV = [INF] * N
pathV[v] = 0
q = deque()
q.append((v, 0))
while q:
p, d = q.popleft()
nd = d + 1
for np in edge[p]:
if pathV[np] > nd:
pathV[np] = nd
q.append((np, nd))
# Takahashi
ans = 0
pathU = [INF] * N
pathU[u] = 0
q = deque()
q.append((u, 0))
while q:
p, d = q.popleft()
nd = d + 1
if len(edge[p]) == 1:
ans = max(ans, pathV[p] - 1)
for np in edge[p]:
if pathU[np] > nd and pathV[np] > nd:
pathU[np] = nd
q.append((np, nd))
print(ans)
|
def main():
N, S = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353
dp = [[0] * (S+5) for _ in range(N+5)]
dp[0][0] = 1
for i in range(N):
for j in range(S+1):
dp[i+1][j] += 2 * dp[i][j]
dp[i+1][j] %= MOD
if j + A[i] <= S:
dp[i+1][j+A[i]] += dp[i][j]
dp[i+1][j+A[i]] %= MOD
print(dp[N][S])
if __name__ == "__main__":
main()
| 0 | null | 67,958,362,734,812 | 259 | 138 |
def bubble_sort(r, n):
flag = True # ??£??\????´?????????¨????????°
while flag:
flag = False
for i in range(n - 1, 0, -1):
if r[i - 1][1] > r[i][1]:
r[i - 1], r[i] = r[i], r[i - 1]
flag = True
return r
def select_sort(r, n):
for i in range(0, n):
minj = i
for j in range(i, n):
if r[j][1] < r[minj][1]:
minj = j
if i != minj:
r[i], r[minj] = r[minj], r[i]
return r
def stable_sort(r, sort_r):
for i in range(len(r)):
for j in range(i + 1, len(r)):
for a in range(len(sort_r)):
for b in range(a + 1, len(sort_r)):
if r[i][1] == r[j][1] and r[i] == sort_r[b] and r[j] == sort_r[a]:
return "Not stable"
return "Stable"
N = int(input())
R = list(input().split())
C = R[:]
BS_R = bubble_sort(R, N)
SS_R = select_sort(C, N)
print(*BS_R)
print(stable_sort(R, BS_R))
print(*SS_R)
print(stable_sort(R, SS_R))
|
n=int(input())
a=[]
from collections import deque as dq
for i in range(n):
aaa=list(map(int,input().split()))
aa=aaa[2:]
a.append(aa)
dis=[-1]*n
dis[0]=0
queue=dq([0])
while queue:
p=queue.popleft()
for i in range(len(a[p])):
if dis[a[p][i]-1]==-1:
queue.append(a[p][i]-1)
dis[a[p][i]-1]=dis[p]+1
for i in range(n):
print(i+1,dis[i])
| 0 | null | 15,159,649,406 | 16 | 9 |
from collections import deque
n, k = map(int, input().split())
a = list(map(int, input().split()))
#kを法とする配列を作る
a = [(i - 1) for i in a]
acc_a = [0 for i in range(n+1)]
acc_a[0] = 0
acc_a[1] = a[0] % k
#kを法とする累積和
for i in range(2,len(a) + 1):
acc_a[i] = (a[i - 1] + acc_a[i-1]) % k
ans = 0
count = {}
q = deque()
for i in acc_a:
if i not in count:
count[i] = 0
ans += count[i]
count[i] += 1
q.append(i)
if len(q) == k:
count[q.popleft()] -= 1
print(ans)
|
if __name__ == "__main__":
while True:
nums = map( int, raw_input().split())
H = nums[0]
W = nums[1]
if H == 0:
if W == 0:
break
s = ""
j = 0
while j < W:
s += "#"
j += 1
i = 0
while i < H:
print s
i += 1
print
| 0 | null | 69,404,882,765,010 | 273 | 49 |
a,b = map(int, input().split())
d = a/b
r = a%b
print('%d %d %f' % (d,r,d))
|
a,b = [float(i) for i in input().split()]
d = int(a/b)
r = int(a) % int(b)
f = a/b
f = "{:.9f}".format(f)
print(d,r,f)
| 1 | 605,127,032,132 | null | 45 | 45 |
a = [input() for i in range(2)]
b = [int(s) for s in a]
print(6 - sum(b))
|
a=int(input())
b=int(input())
ans=6-a-b
print(ans)
| 1 | 110,992,090,810,260 | null | 254 | 254 |
n = int(input())
brackets_plus = []
brackets_minus = []
right = 0
left = 0
for i in range(n):
s = input()
cur = 0
m = 0
for j in range(len(s)):
if s[j] == "(":
left += 1
cur += 1
else:
right += 1
cur -= 1
m = min(m,cur)
if cur >= 0:
brackets_plus.append((m,cur))
else:
brackets_minus.append((m,cur,m-cur))
if right != left:
print("No")
exit()
cur = 0
brackets_plus.sort(reverse = True)
for i in brackets_plus:
if i[0] + cur < 0:
print("No")
exit()
cur += i[1]
brackets_minus.sort(key = lambda x:x[2])
for i in brackets_minus:
if i[0] + cur < 0:
print("No")
exit()
cur += i[1]
print("Yes")
|
n = int(input())
li = []
for _ in range(n):
s = input()
count = 0
while True:
if count + 1 >= len(s):
break
if s[count] == '(' and s[count+1] == ')':
s = s[:count] + s[count+2:]
count = max(0, count-1)
else:
count += 1
li.append(s)
li2 = []
for i in li:
count = 0
for j in range(len(i)):
if i[j] == ')':
count += 1
else:
break
li2.append((count, len(i) - count))
li3 = []
li4 = []
for i in li2:
if i[0] <= i[1]:
li3.append(i)
else:
li4.append(i)
li3.sort()
li4.sort(key = lambda x: x[1], reverse = True)
now = [0,0]
for l,r in li3:
if l == 0 and r == 0:
pass
elif l == 0:
now[1] += r
else:
now[1] -= l
if now[1] < 0:
print('No')
exit()
now[1] += r
for l,r in li4:
if l == 0 and r == 0:
pass
elif r == 0:
now[1] -= l
if now[1] < 0:
print('No')
exit()
else:
now[1] -= l
if now[1] < 0:
print('No')
exit()
now[1] += r
if now[0] == 0 and now[1] == 0:
print('Yes')
else:
print('No')
| 1 | 23,577,201,935,782 | null | 152 | 152 |
#!/usr/bin/env python3
import sys
from itertools import chain
# from itertools import combinations as comb
# form bisect import bisect_left, bisect_right, insort_left, insort_right
# from collections import Counter
# import numpy as np
YES = "Yes" # type: str
NO = "No" # type: str
def solve(X: int, Y: int):
if Y % 2 == 1:
return NO
if Y > X * 4:
return NO
if Y < X * 2:
return NO
return YES
def main():
tokens = chain(*(line.split() for line in sys.stdin))
X = int(next(tokens)) # type: int
Y = int(next(tokens)) # type: int
answer = solve(X, Y)
print(answer)
if __name__ == "__main__":
main()
|
x,y = map(int,input().split())
A = y//2
B = (x-A)
while A>-1:
if y==2*A + 4*B:
print("Yes")
break
A = A-1
B = B+1
if A==-1:
print("No")
| 1 | 13,839,725,353,344 | null | 127 | 127 |
import pprint
N=int(input())
A=list(map(int,input().split()))
if N%2==0:#偶数なら1回まで2こ飛ばしで無茶できる
dp=[[-float("inf") for _ in range(2)] for _ in range(N+10)]#dpはj=0の時は無茶をしていない系列j=1の時は1回無茶をした系列である
if N==2:#2の時は個別
print(max(A[0],A[1]))
else:#それ以外の時は
for i in range(N):
if i==0: #i=0をとる時の任意の系列は無茶を確実にしていないのでdp[i][0]のみA[0]
dp[0][0]=A[0]
elif i==1: #i=1をとる時の任意の系列は一回無茶をして取っているはずである、よってdp[i][1]のみA[1]
dp[1][1]=A[1]
elif i==2: #i=2をとる時の任意の系列は一回も無茶せずに先頭からとっているのでdp[i][0]=A[0]+A[2]
dp[2][0]=A[0]+A[2]
else: #そのほかは再帰的に考える
for j in range(2):
if j==0: #無茶しない系列を生成する時
dp[i][0]=max(dp[i][0],dp[i-2][0]+A[i]) #そこまでの無茶しなかった系列に自らを足していけば良い
elif j==1: #一回無茶する系列を生成する時
dp[i][1]=max(dp[i][1],dp[i-2][1]+A[i],dp[i-3][0]+A[i]) #すでにある無茶した時の値(いらない?)と、前までの系列で無茶していて今回は無茶できないパターン、今回で無茶をするパターンのうち最大を用いて系列生成すれば良い
print(max(dp[N-1][1],dp[N-2][0])) #1回も飛ばさない時は後ろから二番目のdp値が相当することに注意
else:#奇数なら計2回まで無茶できる(2飛ばし×2、3飛ばし×1まで許容)
#print(A[0],A[1])
dp=[[-float("inf") for _ in range(3)] for _ in range(N+10)]#dpはj=0の時は無茶をしない系列j=1は1回無茶をした系列、j=2は2回無茶をしている系列
if N==3:#N=3の時は個別
print(max(A[0],A[1],A[2]))
else:#それ以外の時は
for i in range(N):
if i<4:
if i==0: #i=0をとる任意の系列は無茶を確実にしていないのでdp[i][0]のみA[0]
dp[0][0]=A[0]
if i==1: #i=1をとる任意の系列は確実に1回無茶をしているのでdp[i][1]のみA[1]
dp[1][1]=A[1]
if i==2: #i=2をとる時は2回分の無茶をしてA[2]を得る時(dp[i][2]=A[2])および1かいも無茶せずに撮っている時(dp[i][0]=A[2]+A[0])
dp[2][2]=A[2]
dp[2][0]=A[0]+A[2]
if i==3: #i=3をとる時は1回目で無茶をしてそのあと無茶していないパターン(dp[1][1]+A[3])といきなり1回無茶をしたパターン(dp[0][0]+A[3])があるのでその最大を
dp[3][1]=max(dp[1][1]+A[3],dp[0][0]+A[3])
else: #そのほかは再帰的に
for j in range(3):
if j==0: #無茶してない系列を生成する時、
dp[i][0]=max(dp[i][0],dp[i-2][0]+A[i]) #そこまでの無茶しなかった系列に自らを足していけば良い
elif j==1: #1回だけ無茶した系列を生成する時
dp[i][1]=max(dp[i][1],dp[i-2][1]+A[i],dp[i-3][0]+A[i]) #すでにある1回無茶した時の値(いらない?)と、前までの系列で1回無茶していて今回は無茶しないパターン、今回で初めて無茶をするパターンのうち最大を用いて系列生成すれば良い
else: #2回無茶した系列を生成する時
dp[i][2]=max(dp[i][2],dp[i-2][2]+A[i],dp[i-3][1]+A[i],dp[i-4][0]+A[i]) #すでにある2回無茶した時の値(いらない?)と、もう二回無茶していて無茶できないパターン、前までの系列で1回無茶していて今回も1回無茶するしないパターン、今回でいきなり2回無茶をするパターンのうち最大を用いて系列生成すれば良い
print(max(dp[N-1][2],dp[N-2][1],dp[N-3][0])) #1回も飛ばさない時は後ろから3番目が、1回だけ飛ばした時は後ろから2番目のdp値が相当することに注意
|
s = input()
if s[-1] == "s":
s += "e"
print(s + "s")
| 0 | null | 19,762,154,652,350 | 177 | 71 |
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
cnt=0
for i in range(n):
for j in range(i+1,n):
cnt+=bisect.bisect_left(a,a[i]+a[j])-(j+1)
print(cnt)
|
n=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(n):
for j in range(i+1,n):
l=j;r=n
while abs(l-r)>1:
mid=(r+l)//2
if L[i]<L[j]+L[mid] and L[j]<L[i]+L[mid] and L[mid]<L[i]+L[j]:
l=mid
else: r=mid
ans+=l-j
print(ans)
| 1 | 171,789,264,054,772 | null | 294 | 294 |
s=input()
a,b,c,d=0,0,0,0
for i in range(len(s)):
if s[i]==">":
c=0;b+=1
if b<=d:a+=b-1
else:a+=b
else:b=0;c+=1;a+=c;d=c
print(a)
|
import math
n = int(input())
ans = 100000
for i in range(n):
ans += ans * 0.05
ans = int(math.ceil(ans / 1000) * 1000)
print(ans)
| 0 | null | 78,134,311,175,312 | 285 | 6 |
M1, D1 = input().split()
M2, D2 = input().split()
M1 = int(M1)
M2 = int(M2)
if M1!=M2:
print('1')
else:
print('0')
|
m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
if d1 == 31:
print("1")
elif d1 == 30 and d2 == 1:
print("1")
elif d1 == 28 and m1 == 2:
print("1")
else:
print("0")
| 1 | 124,919,462,300,166 | null | 264 | 264 |
s = list(input())
q = int(input())
before = []
after = []
cnt = 0
for i in range(q):
t = list(input().split())
if t[0] == "1":
cnt += 1
else:
f = t[1]
c = t[2]
if f == "1":
if cnt%2 == 0:
before.append(c)
else:
after.append(c)
else:
if cnt%2 == 0:
after.append(c)
else:
before.append(c)
if cnt%2 == 1:
s.reverse()
after.reverse()
#before.reverse()
for i in after:
print(i,end = "")
for i in s:
print(i,end = "")
for i in before:
print(i,end = "")
else:
before.reverse()
before.extend(s)
before.extend(after)
for i in before:
print(i,end = "")
|
S = input()
top = 0
end = len(S)-1
toright = True
N = int(input())
Q = [S[i] if i <= end else "" for i in range(end+1+N)]
for i in range(N):
q = input().split()
if q[0]=="1":
toright = not toright
else:
if not(toright ^ (q[1]=="1")):
Q[top-1]=q[2]
top -= 1
else:
Q[end+1]=q[2]
end += 1
ans = ""
#print(Q)
#print(toright)
for i in range(top, end+1):
ans += Q[i]
if not toright:
ans = ans[::-1]
print(ans)
| 1 | 57,583,866,096,162 | null | 204 | 204 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.