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 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)
from scipy.sparse.csgraph import floyd_warshall import numpy as np N, M, L = map(int, input().split()) inf = 10**9 + 1 dist = [[inf] * N for i in range(N)] for i in range(M): A, B, C = map(int, input().split()) dist[A-1][B-1] = dist[B-1][A-1] = C dist = floyd_warshall(dist, directed=False) dist = np.where(dist <= L, 1, inf) dist = floyd_warshall(dist, directed=False) dist = np.where(dist < inf, dist-1, -1) Q = int(input()) for i in range(Q): s, t = map(int, input().split()) print(int(dist[s-1, t-1]))
0
null
120,783,114,740,742
215
295
# AC: 753 msec(Python3) import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines class UnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.size = [1] * (n+1) def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.find(self.par[x]) def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.size[x] < self.size[y]: self.par[x] = y self.size[y] += self.size[x] else: self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): return self.size[self.find(x)] def main(): N,M,K,*uv = map(int, read().split()) ab, cd = uv[:2*M], uv[2*M:] uf = UnionFind(N) friend = [[] for _ in range(N+1)] for a, b in zip(*[iter(ab)]*2): uf.unite(a, b) friend[a].append(b) friend[b].append(a) ans = [uf.get_size(i) - 1 - len(friend[i]) for i in range(N+1)] for c, d in zip(*[iter(cd)]*2): if uf.is_same(c, d): ans[c] -= 1 ans[d] -= 1 print(*ans[1:]) if __name__ == "__main__": main()
N = int(input()) S = list(map(int, input().split())) Q = int(input()) T = list(map(int, input().split())) C = 0 for i in range(Q): if T[i] in S: C+=1 print(C)
0
null
30,987,146,484,480
209
22
import sys H = int(next(sys.stdin.buffer)) ans = 0 i = 1 while H > 0: H //= 2 ans += i i *= 2 print(ans)
n = int(input()) L11= [0] * 10 L12= [0] * 10 L13= [0] * 10 L21= [0] * 10 L22= [0] * 10 L23= [0] * 10 L31= [0] * 10 L32= [0] * 10 L33= [0] * 10 L41= [0] * 10 L42= [0] * 10 L43= [0] * 10 for i in range(n): b,f,r,v = map(int, input().split()) r -= 1 if b == 1: if f == 1: L11[r] += v if f == 2: L12[r] += v if f == 3: L13[r] += v if b == 2: if f == 1: L21[r] += v if f == 2: L22[r] += v if f == 3: L23[r] += v if b == 3: if f == 1: L31[r] += v if f == 2: L32[r] += v if f == 3: L33[r] += v if b == 4: if f == 1: L41[r] += v if f == 2: L42[r] += v if f == 3: L43[r] += v def PV(L): for i in range(9): print(" " + str(L[i]), end="") print(" " + str(L[9])) def PL(): print("#" * 20) PV(L11) PV(L12) PV(L13) PL() PV(L21) PV(L22) PV(L23) PL() PV(L31) PV(L32) PV(L33) PL() PV(L41) PV(L42) PV(L43)
0
null
40,445,993,426,098
228
55
import itertools def abc167c_skill_up(): n, m, x = map(int, input().split()) c = [] a = [] for _ in range(n): v = list(map(int, input().split())) c.append(v[0]) a.append(v[1:]) pattern = itertools.product([0,1], repeat=n) best = float('inf') for p in pattern: cost = 0 skill = [0] * m for i, v in enumerate(p): if v == 1: cost += c[i] if cost > best: break check = True for j in range(m): skill[j] += a[i][j] if skill[j] < x: check = False if check: best = cost break if best == float('inf'): print('-1') else: print(best) abc167c_skill_up()
import math a, b, C = map(float, input().split()) h = b * math.sin(math.radians(C)) S = a * h / 2 a1 = b * math.cos(math.radians(C)) cc = math.sqrt(h * h + (a - a1) * (a - a1)) print("{a:5f}".format(a=S)) print("{a:5f}".format(a=a + b + cc)) print("{a:5f}".format(a=h))
0
null
11,114,996,549,024
149
30
N=int(input()) A=list(map(int, input().split())) if N==2: print(max(A)) exit() dp=[[0,0,0] for i in range(N)] dp[0][0]=A[0] dp[1][1]=A[1] dp[2][2]=A[2] for i in range(N): if i>1: dp[i][0]=dp[i-2][0]+A[i] if i>2: dp[i][1]=max(dp[i-3][0],dp[i-2][1])+A[i] if i>3 : dp[i][2]=max(dp[i-4][0],dp[i-3][1],dp[i-2][2])+A[i] if N%2==1: ans=max(dp[-1][2],dp[-2][1],dp[-3][0]) else: ans=max(dp[-1][1],dp[-2][0]) print(ans)
from itertools import accumulate n = int(input()) A = list(map(int, input().split())) L = [0] R = [] min_L = float("inf") for i, a in enumerate(A): b = A[-(i+1)] if i%2 == 0: L.append(a) else: R.append(a) R.append(0) L = list(accumulate(L)) R = list(accumulate(R[::-1]))[::-1] ans = -float("inf") if n%2: def f(A): temp = 0 left = 0 right = 0 for i in range(2, n, 2): temp += A[i] res = max(ans, temp) for i in range(1, n//2): temp -= A[i*2] left, right = left+A[2*(i-1)], max(left, right)+A[2*(i-1)+1] res = max(res, temp+max(left, right)) return res ans = max(f(A), f(A[::-1])) temp = 0 for i in range(1, n, 2): temp += A[i] ans = max(ans, temp) else: for l, r in zip(L, R): ans = max(ans, l+r) print(ans)
1
37,578,721,262,810
null
177
177
n = int(input()) s = input() if n % 2 == 1: print('No') else: sa = s[:n//2] sb = s[n//2:] if sa == sb: print('Yes') else: print('No')
N=int(input()) S=input() if N%2==1:print('No') else: s1,s2=S[:N//2],S[N//2:] ok=1 for i in range(N//2): if s1[i]!=s2[i]: print('No') ok=0 break if ok==1:print('Yes')
1
146,709,730,051,538
null
279
279
def factorization(n): arr = [] temp = n for i in range(2, int(-(-n**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([n, 1]) return arr N = int(input()) if N == 1: print("0") exit() factors = factorization(N) ans = 0 for factor in factors: num = factor[1] x = 1 for i in range(num+1): if i**2 + i > 2*num: x = i-1 break # print(num,x) ans += x print(ans)
from collections import Counter def prime_factorize(n): res = [] while n % 2 == 0: res.append(2) n //= 2 f = 3 while f * f <= n: if n % f == 0: res.append(f) n //= f else: f += 2 if n != 1: res.append(n) return res n = int(input()) prime_numbers = prime_factorize(n) prime_counter = Counter(prime_numbers) ans = 0 for v in prime_counter.values(): i = 1 while v >= i: ans += 1 v -= i i += 1 print(ans)
1
16,901,199,249,744
null
136
136
def solve(string): return str(max(map(len, string.split("S")))) if __name__ == '__main__': import sys print(solve(sys.stdin.read().strip()))
s=input() print(('RRR'in s)+('RR'in s)+('R'in s))
1
4,835,436,012,670
null
90
90
three_num = input() three_num = [int(i) for i in three_num.split(" ")] a = three_num[0] b = three_num[1] c = three_num[2] cnt = 0 for i in range(a, b + 1): if c % i == 0: cnt += 1 print(cnt)
def f(i): if i == 2:return 1 return pow(2, i-1, i) == 1 print(sum(1 for n in range(int(input())) if f(int(input()))))
0
null
283,025,025,870
44
12
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()
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) # l[i] = (a[j] >= i を満たすjの個数) l = [n for i in range(a[0]+1)] for i in range(2, n+1): for j in range(a[-i], a[-i+1], -1): l[j] = n - i + 1 # 二分探索 # a[i] + a[j] >= x を満たす(i, j)がm組以上存在する最小のxを求める start = 2 * a[-1] stop = 2 * a[0] while start < stop: i = (start + stop + 1) // 2 num = 0 for j in a: if i - j < 0: num += n elif i - j <= a[0]: num += l[i - j] else: break if num >= m: start = i else: stop = i - 1 num = 0 for i in a: if start - i < 0: num += n elif 0 <= start - i <= a[0]: num += l[start - i] else: break # start <= a[i]+a[j] を満たす a[i]+a[j] を全て足す ans = 0 for i in a: if start - i < 0: ans += 2 * i * n elif start - i <= a[0]: ans += 2 * i * l[start - i] else: break ans -= (num - m) * start print(ans)
0
null
92,164,524,489,348
224
252
n = int(input()) A = [int(i) for i in input().split()] flag = 1 count = 0 while flag: flag = 0 for j in range(n-1,0,-1): if A[j] < A[j-1]: A[j],A[j-1] = A[j-1],A[j] flag = 1 count += 1 print(" ".join(map(str,A))) print(count)
n = int(input()) a = [int(i) for i in input().split()] num = 0 is_swapped = True i = 0 while is_swapped: is_swapped = False for j in range(n-1, i, -1): if a[j] < a[j-1]: tmp = a[j] a[j] = a[j-1] a[j-1] = tmp is_swapped = True num += 1 i += 1 print(' '.join([str(i) for i in a])) print(num)
1
16,916,602,784
null
14
14
from itertools import combinations_with_replacement n,m,q = map(int, input().split()) abcd = [list(map(int, input().split())) for _ in range(q)] ans = 0 for a in combinations_with_replacement(range(1,m+1), n): t = 0 for i in abcd: if a[i[1]-1] - a[i[0]-1] == i[2]: t += i[3] if ans < t: ans = t print(ans)
import copy def main(): N, M, Q = [int(n) for n in input().split(" ")] q = [[int(a) for a in input().split(" ")] for i in range(Q)] all_series = get_series(N, M) points = [0] for l in all_series: points.append(get_score(l, q)) print(max(points)) def get_score(l, q): return sum([q[i][3] if l[q[i][1] - 1] - l[q[i][0] - 1] == q[i][2] else 0 for i in range(len(q))]) def get_series(N, M): # N: number of elms # M: upper limit of val of elm all_series = [] checked = [[0] * M for i in range(N)] to_check = [[0, j + 1] for j in range(M)] series = [0 for k in range(N)] while len(to_check) > 0: checking = to_check.pop(-1) series[checking[0]] = checking[1] if checking[0] == N - 1: l = copy.deepcopy(series) all_series.append(l) else: to_check.extend([[checking[0] + 1, k] for k in range(checking[1], M + 1)]) return all_series main()
1
27,572,158,715,648
null
160
160
import sys w = input().lower() lines = sys.stdin.read() line=lines[0:lines.find('END_OF_TEXT')].lower().split() print(line.count(w))
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 def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 N = INT() D = [len(set(LIST()))==1 for _ in range(N)] for i in range(N): if sum(D[i:i+3]) == 3: print("Yes") break else: print("No")
0
null
2,164,353,987,910
65
72
import sys input = sys.stdin.readline def main(): X = int(input()) for A in range(-119, 120 + 1): for B in range(-119, 120 + 1): if A ** 5 - B ** 5 == X: print(A, B) exit() if __name__ == "__main__": main()
x = int(input().strip()) for i in range(120): for j in range(120): y = i**5 + j**5 z = i**5 - j**5 if z == x: a = i b = j print("{} {}".format(a, b)) exit() if y == x: a = i b = -j print("{} {}".format(a, b)) exit()
1
25,557,271,414,302
null
156
156
import numpy as np import sys readline = sys.stdin.readline N = int(input()) A = np.array(readline().split(), np.int64) all_xor = 0 for a in A: all_xor = all_xor^a for a in A: xor = all_xor^a print(xor, end = ' ')
# B - Digits N,K = map(int,input().split()) i = 1 while K**i-1<N: i += 1 print(i)
0
null
38,279,728,017,818
123
212
A=input() print(A.swapcase())
s = input() cs = list(s) for c in cs: if c.islower(): print(c.upper(),end="") else : print(c.lower(),end="") print()
1
1,497,987,431,210
null
61
61
def sieve_of_eratosthenes(N): N += 1 sieve = list(range(N)) for p in range(2, int(N ** 0.5) + 1): if sieve[p] == p: for i in range(p << 1, N, p): sieve[i] = p return sieve def lcm_with_sieve(numbers, N): sieve = sieve_of_eratosthenes(N) lcm = dict() for n in numbers: d = dict() while n > 1: factor = sieve[n] n //= factor d[factor] = d.get(factor, 0) + 1 for factor, power in d.items(): lcm[factor] = max(lcm.get(factor, 0), power) return lcm MOD = 10 ** 9 + 7 input() A = tuple(map(int, input().split())) lcm = 1 for factor, power in lcm_with_sieve(A, 10 ** 6).items(): lcm = lcm * pow(factor, power, MOD) % MOD total = 0 for a in A: total = (total + lcm * pow(a, MOD - 2, MOD)) % MOD print(total)
import sys sys.setrecursionlimit(10000000) input=sys.stdin.readline n = int(input()) a = list(map(int,input().split())) def gcd(a, b): return a if b == 0 else gcd(b, a%b) def lcm(a, b): return a // gcd(a, b) * b mod = 10**9+7 x=a[0] for i in a[1:]: x=lcm(x,i) ans=0 for i in range(n): ans+= x//a[i] print(ans%mod)
1
87,436,999,340,960
null
235
235
import sys sys.setrecursionlimit(10 ** 6) # input = sys.stdin.readline #### int1 = lambda x: 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)] INF = float('inf') def solve(): n = II() print((n + 1) // 2) if __name__ == '__main__': solve()
import sys N = int(input()) + 1 ans = N // 2 print(ans)
1
59,251,114,247,780
null
206
206
import sys sys.setrecursionlimit(300000) def I(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split()) def LMI(): return list(map(int, sys.stdin.readline().split())) def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split())) MOD = 10 ** 9 + 7 INF = float('inf') K = I() num = 7 for i in range(10 ** 6): if num % K == 0: print(i + 1) exit() num = (num * 10 + 7) % K print(-1)
def main(): k=int(input()) s=7 for i in range(10**6): if s%k == 0: print(i+1) break s = (s*10+7)%k else: print(-1) if __name__ == "__main__": main()
1
6,079,119,123,672
null
97
97
h = int(input()) w = int(input()) n = int(input()) print((n-1)//max(h,w)+1)
n = input() s = 100000 for i in range(n): s = s * 1.05 if s % 1000 != 0: s = ((s // 1000) + 1) * 1000 print "%d" % s
0
null
44,467,882,680,930
236
6
from collections import Counter def solve(string): n, *aqbc = map(int, string.split()) a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:] s = sum(a) t = Counter(a) ans = [] for b, c in zip(*[iter(bc)] * 2): if b in t.keys(): s += (c - b) * t[b] t[b], t[c] = 0, t[b] + t[c] ans.append(s) return "\n".join(map(str, ans)) if __name__ == '__main__': import sys print(solve(sys.stdin.read().strip()))
from collections import deque input_list = list(input()) left_idx_stack = deque() puddle_stack = deque() for idx, s in enumerate(input_list): if s == "\\": left_idx_stack.append(idx) elif s == '/': if left_idx_stack: latest_left_idx = left_idx_stack.pop() area = idx - latest_left_idx if not puddle_stack: puddle_stack.append((latest_left_idx, area)) elif latest_left_idx > puddle_stack[-1][0]: puddle_stack.append((latest_left_idx, area)) else: while len(puddle_stack) != 0 and (latest_left_idx < puddle_stack[-1][0]): latest_puddle = puddle_stack.pop() area += latest_puddle[1] puddle_stack.append((latest_left_idx, area)) else: pass areas = [p[1] for p in puddle_stack] print(sum(areas)) print(len(areas), *areas)
0
null
6,138,296,033,822
122
21
import sys readline = sys.stdin.readline MOD = 10 ** 9 + 7 INF = float('INF') sys.setrecursionlimit(10 ** 5) def main(): N, K = map(int, readline().split()) digit = 0 cur = 0 while cur < N: digit += 1 cur = K ** digit - 1 print(digit) if __name__ == '__main__': main()
n,k=map(int,input().split()) a,b=0,0 for i in range(1000000000): if n//k ==0: break else: n=n//k a+=1 print(a+1)
1
64,570,048,738,272
null
212
212
A, B, M = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) price_list = [] for i in range(M): x, y, c = map(int, input().split()) price = a[x-1] + b[y-1] - c price_list.append(price) a.sort() b.sort() price_list.append(a[0]+b[0]) min_price = price_list[0] for n in price_list: if min_price >= n: min_price = n elif min_price < n: pass print(min_price)
a, b, m = map(int, input().split()) reizo = list(map(int, input().split())) renji = list(map(int, input().split())) p = [list(map(int,input().split())) for i in range(m)] ans = [] for i in range(m): cost = reizo[p[i][0] - 1] + renji[p[i][1] - 1] - p[i][2] ans.append(cost) ans.append(min(reizo) + min(renji)) print(min(ans))
1
54,049,220,343,132
null
200
200
n = int(input()) a = [int(x) for x in input().split(" ")] print(min(a), max(a), sum(a))
#!/usr/bin/env python """NOMURA プログラミングコンテスト 2020: C - Folia https://atcoder.jp/contests/nomura2020/tasks/nomura2020_c """ def main(): N = int(input()) As = list(map(int, input().split())) leaves_count = sum(As) node_count = 1 ans = 0 for i, a in enumerate(As): leaves_count -= a next_node_count = min(leaves_count, (node_count - a) * 2) if next_node_count < 0: ans = -1 break ans += node_count node_count = next_node_count print(ans) if __name__ == '__main__': main()
0
null
9,761,474,208,320
48
141
def srch(u, cnt, Dst, Fst, Lst): S = [u] Fst[u] = cnt while(len(S) > 0): cnt += 1 u = S.pop() if Dst[u] is not None and len(Dst[u]) > 0: while(len(Dst[u])): u1 = Dst[u].pop() if Fst[u1] == 0: S.append(u) S.append(u1) Fst[u1] = cnt break else: Lst[u] = cnt else: Lst[u] = cnt return cnt + 1 def main(): num = int(input()) Dst = [None for i in range(num + 1)] Fst = [0] * (num + 1) Lst = Fst[:] for n in range(1, num+1): a = list(map(int,input().split())) u = a[0] if a[1] > 0: Dst[u] = a[2:] Dst[u].reverse() cnt = 1 for i in range(1,num): if Fst[i] == 0: cnt = srch(i, cnt, Dst, Fst, Lst) for n in range(1, num+1): print("{} {} {}".format(n,Fst[n],Lst[n])) if __name__ == '__main__': main()
cnt = 1 def dep(G,s_cnt,f_cnt,np): global cnt s_cnt[np] = cnt cnt += 1 for i in range(n): if G[np][i] == 1 and s_cnt[i] == 0: dep(G,s_cnt,f_cnt,i) f_cnt[np] = cnt cnt += 1 n = int(input()) G = [[0 for i in range(n)] for j in range(n)] s_cnt = [0 for i in range(n)] f_cnt = [0 for i in range(n)] for i in range(n): S = list(map(int, input().split())) tmp_n = S[1] for j in range(tmp_n): G[i][S[j+2]-1] = 1 for i in range(n): if s_cnt[i]==0: dep(G,s_cnt,f_cnt,i) for i in range(n): print(i+1,s_cnt[i],f_cnt[i])
1
2,737,668,480
null
8
8
import sys N=int(input()) alist=list(map(int,input().split())) if N==0 and alist[0]!=1: print(-1) sys.exit(0) elif N>0 and alist[0]>0: print(-1) sys.exit(0) #pow2d=1<<N blist=[0]*(N+1) for d in reversed(range(1,N+1)): if d>50: blist[d-1]=alist[d]+blist[d] else: blist[d-1]=min(alist[d]+blist[d],(1<<(d-1))-alist[d-1]) if blist[d-1]<0: print(-1) sys.exit(0) #pow2d>>=1 #print(alist,blist) cmax_bu=[0]*(N+1) for d in range(N+1): cmax_bu[d]=alist[d]+blist[d] #print(cmax_bu) pow2d=1 blist=[0]*(N+1) if alist[0]==0: blist[0]=1 for d in range(1,N+1): if pow2d<10**10: pow2d<<=1 c=min(2*blist[d-1],pow2d,cmax_bu[d]) else: c=min(2*blist[d-1],cmax_bu[d]) blist[d]=c-alist[d] if blist[d]<0: print(-1) sys.exit(0) #print(alist,blist) answer=0 for i in range(N+1): answer+=alist[i]+blist[i] print(answer)
import copy n = int(input()) a = list(map(int,input().split())) a.sort(reverse=True) count = 0 for i in range(len(a)-1): count += a[(i+1)//2] print(count)
0
null
14,020,403,155,260
141
111
from sys import stdin input = stdin.readline n = int(input()) A = list(map(int, input().split())) q = int(input()) m = list(map(int, input().split())) set_m = set([]) for i in range(2 ** n): tmp = 0 for j in range(n): if (i >> j) & 1: tmp += A[j] set_m.add(tmp) for i in m: if i in set_m: print("yes") else: print("no")
# coding: utf-8 # Your code here! n = int(input()) A = [int(e) for e in input().split()] q = int(input()) M = [int(e) for e in input().split()] def solve(i,m): if m == 0: return True if i >= n: return False if m > sum(A[i:]): return False res = solve(i + 1,m) or solve(i + 1,m - A[i]) return res for m in M: if(solve(0,m)): print("yes") else: print("no")
1
99,775,911,148
null
25
25
import sys import time import random import math from collections import deque import heapq import itertools from decimal import Decimal import bisect from operator import itemgetter MAX_INT = int(10e18) MIN_INT = -MAX_INT mod = 1000000000+7 sys.setrecursionlimit(1000000) def IL(): return list(map(int,input().split())) def SL(): return input().split() def I(): return int(sys.stdin.readline()) def S(): return input() def tami(ans): res = 0 LastDay = [0]*26 for i in range(D): res += s[i][ans[i]] for j in range(26): if ans[i] == j: continue res -= (i+1 - LastDay[j]) * c[j] LastDay[ans[i]] = i+1 return res def nasu(): res = [] LastDay = [0]*26 for d in range(1,D+1): tmp = ["", 0] for i in range(26): if tmp[1] < s[d-1][i] + (d - LastDay[i]) * c[i]: tmp = [i, s[d-1][i] + (d - LastDay[i]) * c[i]] res.append(tmp[0]) LastDay[tmp[0]] = d return res def nbo(ans, score, num): tmp_ans = ans[:] for _ in range(num): random_date = random.randint(0,D-1) random_val = random.randint(0,25) tmp_ans[random_date] = random_val scoscore = tami(tmp_ans) if scoscore > score: return (True, tmp_ans, scoscore) else: return (False, tmp_ans, scoscore) start = time.time() D = I() c = IL() s = [IL() for i in range(D)] ans = nasu() #print(ans) score = tami(ans) #print(score) end = time.time() num = 10 cnt = 0 while (time.time() - start) < 1.8: judge, anans, scoscore = nbo(ans, score, num) if judge == True: ans, score = anans, scoscore #print(ans) #print(tami(ans)) cnt += 1 if cnt == 1000: cnt = 0 num = max(2, num-1) for i in ans: print(i+1)
n,k = map(int,input().split()) h = list(map(int,input().split())) h = sorted(h,reverse = True) h1 = h[k:n] print(sum(h1))
0
null
44,468,211,060,440
113
227
x = int(input()) print(2*x*3.14)
print(6.28318530717958623200*int(input()))
1
31,310,192,156,528
null
167
167
a,b = map(str, input().split()) a2 = a*int(b) b2 = b*int(a) print(a2) if a2 < b2 else print(b2)
import sys input = sys.stdin.buffer.readline H, W, K = map(int, input().split()) B = {} for _ in range(K): r, c, v = map(int, input().split()) B[(r, c)] = v dp = [[0]*(W+1) for _ in range(4)] for i in range(1, H+1): for j in range(1, W+1): if (i, j) in B: v = B[(i, j)] dp[0][j] = max(dp[0][j-1], dp[0][j], dp[1][j], dp[2][j], dp[3][j]) dp[1][j] = max(dp[1][j-1], dp[0][j]+v) dp[2][j] = max(dp[2][j-1], dp[1][j-1]+v) dp[3][j] = max(dp[3][j-1], dp[2][j-1]+v) else: dp[0][j] = max(dp[0][j-1], dp[0][j], dp[1][j], dp[2][j], dp[3][j]) dp[1][j] = dp[1][j-1] dp[2][j] = dp[2][j-1] dp[3][j] = dp[3][j-1] print(max(dp[i][-1] for i in range(4)))
0
null
45,224,954,492,188
232
94
n = int(input()) result = "" for i in (range(1,n+1)): if i % 3 == 0: result += " " + str(i) continue j = i while j > 0: if j % 10 == 3: result += " " + str(i) break else: j //= 10 print (result)
#!usr/bin/env python3 import sys def int_fetcher(): num = int(sys.stdin.readline()) return num def call(num): res = '' for i in range(1, num+1): x = i if x % 3 == 0 or x % 10 == 3: res += ' ' + str(i) continue while x: if x % 10 == 3: res += ' ' + str(i) break x //= 10 return res def main(): print(call(int_fetcher())) if __name__ == '__main__': main()
1
927,207,609,958
null
52
52
n = int(input()) alp = [chr(ord("a") + x) for x in range(26)] def dfs(S): if len(S) == n: print(S) else: max_s = ord(max(S)) - 96 #print(max_s) for i in range(max_s + 1): dfs(S + alp[i]) dfs("a")
def BubbleSort(A): for i in range(len(A)): for j in range(len(A)-1,i,-1): if A[j][1] < A[j-1][1]: # リストの要素の文字列は2次元配列として記憶される A[j],A[j-1] = A[j-1],A[j] return A def SelectionSort(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 A[i],A[mini] = A[mini],A[i] return A N = int(input()) # example for input to array : H4 C9 S4 D2 C3 (from cards) array = [i for i in input().split()] array_cp = list(array) result_bs = BubbleSort(array) print(*result_bs) print('Stable') # Bubble sort is stable result_ss = SelectionSort(array_cp) print(*result_ss) if result_ss == result_bs: print('Stable') else: print('Not stable')
0
null
26,390,535,335,232
198
16
A, B, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = [] y = [] c = [] for i in range(m): xx, yy, cc = map(int, input().split()) x.append(xx) y.append(yy) c.append(cc) ans = min(a) + min(b) for i in range(m): ans = min(ans,a[x[i]-1] + b[y[i]-1] - c[i]) print (ans)
list = map(int, raw_input().split()) print list[0] / list[1], list[0] % list[1], "%.5f" %(1.0*list[0] / list[1])
0
null
27,422,845,417,018
200
45
while 1: n,m=map(int,input().split()) if n==0 and m==0: break if n<m: print(n,m) else: print(m,n)
K = int(input()) id = 1 cnt = 7 while cnt < K: cnt = cnt * 10 + 7 id += 1 visited = [0] * K while True: remz = (cnt % K) if remz == 0: break visited[remz] += 1 if visited[remz] > 1: id = -1 break cnt = remz * 10 + 7 id += 1 print(id)
0
null
3,286,549,252,000
43
97
def resolve(): import numpy as np d = int(input()) C = list(map(int, input().split())) S = [] for _ in range(d): _S = list(map(int, input().split())) S.append(_S) S = np.array(S) T = [] for _ in range(d): T.append(int(input())) last = [-1 for i in range(26)] score = 0 for i in range(d): # max_idx = np.argmax(S[i]) # print(max_idx + 1) score += S[i][T[i] - 1] last[T[i] - 1] = i for j in range(26): score -= C[j] * (i - last[j]) print(score) resolve()
MOD = 10**9 + 7 dp = {0: 1} S = int(input()) for s in range(3, S+1): for term in range(3, s+1): if s - term < 0: break else: dp[s] = dp.get(s-term, 0) + dp.get(s, 0) % MOD print(dp.get(S, 0))
0
null
6,540,639,225,082
114
79
N=int(input());M=N//500;M*=1000;X=500*(N//500);N-=X;M+=(N//5)*5;print(M)
def threeDivCheck(x): if x % 3 == 0: return x return 0 def threeIncludeCheck(x): y = x while y!=0: if y%10 == 3: return x y=int(y/10) return 0 def main(): n = int(input()) p = 0 for i in range(n+1): p = threeDivCheck(i) if p: print(' {}'.format(p), end='') else: p=threeIncludeCheck(i) if p: print(' {}'.format(p), end='') print() main()
0
null
21,835,552,730,382
185
52
def main(): S = input() lst = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] ans = 7 - lst.index(S) print(ans) if __name__ == "__main__": main()
import sys input = lambda: sys.stdin.readline().rstrip() def main(): s = input() days = ['','SAT','FRI','THU','WED','TUE','MON','SUN'] print(days.index(s)) if __name__ == '__main__': main()
1
132,914,531,562,652
null
270
270
import math a,b=map(int,input().split()) ans=a*b//(math.gcd(a,b)) print(ans)
from collections import deque import sys sys.setrecursionlimit(10**9) def mi(): return map(int,input().split()) def ii(): return int(input()) def isp(): return input().split() def deb(text): print("-------\n{}\n-------".format(text)) INF=10**20 def main(): N=ii() G = [[] for _ in range(N)] E = [] _E = set() for i in range(N-1): a,b=mi() G[a-1].append(b-1) G[b-1].append(a-1) E.append((a-1,b-1)) _E.add((a-1,b-1)) leaf = 0 for i in range(N): g = G[i] if len(g) == 1: leaf = i stack = deque([(leaf,1,None)]) seen = set() path = {x:0 for x in E} K = 0 def save(edge,color): if edge == None: return a,b = edge if (a,b) in _E: path[(a,b)] = color else: path[(b,a)] = color while stack: current,color,edge = stack.popleft() seen.add(current) save(edge,color) K = max(K,color) # print(current,color,edge) i = 1 for next in G[current]: if next in seen: continue if color == i: i += 1 stack.append((next,i,(current,next))) i += 1 if N == 2: print(1) print(1) exit() print(K) for i in range(N-1): a,b = E[i] print(path[(a,b)],sep="") if __name__ == "__main__": main()
0
null
124,198,179,468,068
256
272
s = input().split(" ") n = int(s[0]) m = int(s[1]) l = int(s[2]) a = [[0 for i in range(m)] for j in range(n)] b = [[0 for i in range(l)] for j in range(m)] for i in range(n): s = input().split(" ") for j in range(m): a[i][j] = int(s[j]) for i in range(m): s = input().split(" ") for j in range(l): b[i][j] = int(s[j]) for i in range(n): for j in range(l): sk = 0 for k in range(m): sk += a[i][k] * b[k][j] if j == l-1: print(sk) else: print(sk,end=" ")
# coding: UTF-8 n,m,l = map(int,raw_input().split(" ")) a_matrix = [map(int,raw_input().split()) for i in range(n)] b_matrix = [map(int,raw_input().split()) for i in range(m)] tmp3_matrix = [] c_matrix = [] for i in range(n): tmp2_matrix = [] for j in range(l): sum = 0 for k in range(m): sum += a_matrix[i][k] * b_matrix[k][j] tmp2_matrix.append(sum) tmp3_matrix.extend([tmp2_matrix]) c_matrix.extend(tmp3_matrix) for x in range(n): print " ".join(map(str,c_matrix[x]))
1
1,437,553,667,470
null
60
60
a, b = map(int, input().split()) c, d = map(int, input().split()) print(1 if c != a else 0)
import sys input = sys.stdin.readline def main(): M1, D1 = map(int, input().split()) M2, D2 = map(int, input().split()) if M1 == M2: ans = 0 else: ans = 1 print(ans) if __name__ == "__main__": main()
1
124,735,401,768,732
null
264
264
a,b=[int(i) for i in input().split()] c=[int(i) for i in input().split()] if(sum(c)>=a): print('Yes') else: print('No')
def solve(): n, m = map(int, input().split()) s = list(map(int, input().split())) if sum(s) >= n: print("Yes") else: print("No") solve()
1
77,761,529,839,290
null
226
226
N = int(input()) A = list(map(int, input().split())) x = 0 for a in A: x ^= a ans = [] for a in A: ans.append(a ^ x) print(" ".join(map(str, ans)))
from functools import reduce from operator import xor # via https://drken1215.hatenablog.com/entry/2020/06/22/122500 N, *A = map(int, open(0).read().split()) B = reduce(xor, A) print(*map(lambda a: B^a, A))
1
12,585,384,300,588
null
123
123
A, B, K = map(int, input().split()) print(max(0, A-K), end=" ") K = max(0, K-A) print(max(0, B-K))
n, x, m = [int(x) for x in input().split()] A = [x] while len(A) < n: a = (A[-1] * A[-1]) % m if a not in A: A.append(a) else: i = A.index(a) loop_len = len(A) - i print(sum(A[:i]) + sum(A[i:]) * ((n - i) // loop_len) + sum(A[i:i + ((n - i) % loop_len)])) break else: print(sum(A))
0
null
53,442,487,906,578
249
75
A = list(map(float, input().split())) if(A[0]/A[2] <= A[1]): print("Yes") else: print("No")
cost,m,=map(int,input().split()) c=list(map(int,input().split())) DP=[[10**10 for i in range(60001)] for j in range(21)] for i in range(m): DP[i][c[i]]=1 for j in range(60001): if c[i]>j: if i==0: DP[i][j]=0 else:DP[i][j]=DP[i-1][j] elif j+c[i]<=60000: DP[i][j]=min(DP[i][j-c[i]]+1,DP[i-1][j]) print(DP[m-1][cost])
0
null
1,842,088,337,738
81
28
from math import sqrt n = int(input()) x = list(map(int, input().split())) y = list(map(int, input().split())) p_1 = 0 p_2 = 0 p_3 = 0 p_inf = 0 for i, j in zip(x, y): p_1 += abs(i - j) p_2 += (i - j) ** 2 p_3 += abs((i - j)) ** 3 p_inf = max(p_inf, abs(i - j)) print(p_1) print(sqrt(p_2)) print(p_3 ** (1 / 3)) print(p_inf)
nMonsters, nSpecial = [int(x) for x in input().split()] monst = [int(x) for x in input().split()] monst.sort() nMonsters -= nSpecial if nMonsters < 0: print(0) else: print(sum(monst[0:nMonsters]))
0
null
39,434,645,132,080
32
227
H, N = map(int, input().split()) A = [int(x) for x in input().split()] AA = sum(A) if AA >= H: print("Yes") else: print("No")
n=int(input()) arr=list(map(int,input().split())) ans=sum(arr) if n==0: if arr[0]==1: print(1) else: print(-1) exit() for i in range(n): if i<=30 and arr[i]>=2**i: print(-1) exit() if n<=30 and arr[n]>2**n: print(-1) exit() maxs=[0]*(n+1) maxs[0]=1 for i in range(1,n): maxs[i]=2*maxs[i-1]-arr[i] tmp=arr[n] for i in range(n-1,-1,-1): maxs[i]=min(maxs[i],tmp) tmp+=arr[i] mins=[0]*(n+1) for i in range(n-1,-1,-1): mins[i]=(mins[i+1]+arr[i+1]+1)//2 for i in range(n): if maxs[i]<mins[i]: print(-1) exit() ans+=sum(maxs) print(ans)
0
null
48,697,673,841,220
226
141
R, C, K = map(int, input().split()) V = [[0]*(C+1) for _ in range(R+1)] for _ in range(K): r, c, v = map(int, input().split()) V[r][c] = v max_pre = [0]*(C+1) for i in range(R+1): now = [[0]*(3+1) for _ in range(C+1)] for j in range(C+1): v = V[i][j] la = lb = 0 if j > 0: la = now[j-1][0] lb = now[j-1][1] now[j][2] = max(now[j][2], now[j-1][2], lb + v) now[j][3] = max(now[j][3], now[j-1][3], now[j-1][2] + v) if i > 0: now[j][0] = max(now[j][0], max_pre[j], la) now[j][1] = max(now[j][1], max_pre[j] + v, la + v, lb) max_pre[j] = max(now[j]) print(max(now[C]))
n = int(input()) s = [] t = [] for i in range(n): s_, t_ = map(str, input().split()) s.append(s_) t.append(int(t_)) x = input() for i in range(n): if s[i] == x: break ans = 0 for j in range(n-1, i, -1): ans += t[j] print(ans)
0
null
51,462,408,823,828
94
243
def main(): k = int(input()) s = input() if len(s) > k: print(s[:k]+"...") else: print(s) if __name__ == '__main__': main()
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)
0
null
36,888,103,086,800
143
200
r=input();p=3.1415926535897;print "%.9f"%(p*r*r),r*2*p
import math def insertionSort(A, n, g): cnt = 0 for i in range(g, n): v = A[i] j = i - g while j >= 0 and A[j] > v: A[j + g] = A[j] j -= g cnt += 1 A[j + g] = v return cnt def shellSort(A, n): cnt = 0 j = int(math.log(2 * n + 1, 3)) + 1 G = list(reversed([(3 ** i - 1)// 2 for i in range(1, j)])) m = len(G) for i in range(m): cnt += insertionSort(A, n, G[i]) return A, cnt, G, m n = int(input()) A = [int(input()) for i in range(n)] ans, count, G, m = shellSort(A, n) print(m) print(' '.join(map(str,G))) print(count) [print(i) for i in A]
0
null
337,833,296,842
46
17
for i in range(9): for k in range(9): p = (i+1)*(k+1) print('{}x{}={}'.format(i+1,k+1,p))
#coding:utf-8 #1_2_C def BubbleSort(cards, n): for i in range(n): for j in range(n-1, i, -1): if cards[j][1] < cards[j-1][1]: cards[j], cards[j-1] = cards[j-1], cards[j] return cards def SelectionSort(cards, n): for i in range(n): minj = i for j in range(i, n): if cards[minj][1] > cards[j][1]: minj = j cards[i], cards[minj] = cards[minj], cards[i] return cards n = int(input()) cards = input().split() copy = list(cards) print(*BubbleSort(cards, n)) print("Stable") print(*SelectionSort(copy, n)) print("Stable" if cards == copy else "Not stable")
0
null
11,745,822,790
1
16
r = int(input()) import math ans = 2 * math.pi * r print(ans)
n,m = map(int,input().split()) a = [int(s) for s in input().split()] daycount = 0 for i in range(m): daycount += a[i] if n - daycount >= 0: print(n - daycount) else: print(-1)
0
null
31,747,684,749,472
167
168
# -*- coding: utf-8 -*- mat1 = {} mat2 = {} n, m, l = map(int, raw_input().split()) for i in range(1, n+1): list = map(int, raw_input().split()) for j in range(1, m+1): mat1[(i, j)] = list[j-1] for j in range(1, m+1): list = map(int, raw_input().split()) for k in range(1, l+1): mat2[(j, k)] = list[k-1] for i in range(1, n+1): buf = "" for k in range(1, l+1): res = 0 for j in range(1, m+1): res += mat1[(i, j)]*mat2[(j, k)] buf += str(res) +" " buf = buf.rstrip() print buf
R, C, K = map(int, input().split()) # = int(input()) p = [] for k in range(K): p.append(list(map(int, input().split()))) maps = [[0 for _ in range(C)] for _ in range(R)] for k in range(K): maps[p[k][0]-1][p[k][1]-1] += p[k][2] point1 = [[0 for _ in range(C)] for _ in range(R)] point2 = [[0 for _ in range(C)] for _ in range(R)] point3 = [[0 for _ in range(C)] for _ in range(R)] point1[0][0] = maps[0][0] for r in range(R): for c in range(C): a, b, d = point1[r][c], point2[r][c], point3[r][c] if c < C - 1: x = maps[r][c+1] point1[r][c+1] = max(point1[r][c+1], x, a) point2[r][c+1] = max(point2[r][c+1], a + x, b) point3[r][c+1] = max(point3[r][c+1], b + x, d) if r < R - 1: point1[r+1][c] = maps[r+1][c] + max(a, b, d) print(max(point1[-1][-1], point2[-1][-1], point3[-1][-1]))
0
null
3,523,486,454,586
60
94
import string x = '' allcase = string.ascii_lowercase while True: try: x+=input().lower() except:break for letter in allcase: print(letter + " : " + repr(x.count(letter)))
def solve(n, a, b): l = (n+1)//2 - 1 r = l + 1 + (n%2==0) lb = sum(sorted(a)[l:r]) ub = sum(sorted(b)[l:r]) return (ub - lb) + 1 n = int(input()) a = [0] * n b = [0] * n for i in range(n): a[i], b[i] = map(int, input().split()) print(solve(n, a, b))
0
null
9,453,837,308,230
63
137
import math x = int(input()) z = (x * 360) // math.gcd(x, 360) print(z//x)
import math if __name__ == "__main__": deg = int(input()) print(360//math.gcd(deg, 360))
1
13,112,344,704,224
null
125
125
k = int(input()) if k % 2 == 0 or k % 5 == 0: print(-1) else: ans = 7 cnt = 1 while 1: if ans % k == 0: break else: cnt += 1 ans *= 10 ans += 7 ans %= k print(cnt)
n = int(input()) A = list(map(int, input().split())) P = [0]*n for i in range(n): P[A[i]-1] = i+1 print(*P,sep=' ')
0
null
93,556,790,824,400
97
299
N = int(input()) for i in range(N+1): X = i * 1.08 if int(X) == N: print(i) exit() print(':(')
import bisect,collections,copy,heapq,itertools,math,numpy,string import sys sys.setrecursionlimit(10**7) def S(): return sys.stdin.readline().rstrip() def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LS(): return list(sys.stdin.readline().rstrip().split()) def main(): N = I() print(int(N/2) if N%2==0 else int(N/2)+1) main()
0
null
92,792,526,579,632
265
206
s = input() t = input() ls = len(s) lt = len(t) ret = lt for i in range(ls+1 - lt): diff = 0 for j in range(lt): diff += (t[j] != s[i+j]) if diff == 0: ret = diff break if diff < ret: ret = diff print(ret)
S = input() T = input() ans = 1000 for i in range(len(S)-len(T)+1): diff = 0 for j in range(len(T)): if S[i+j] != T[j]: diff += 1 if ans > diff: ans = diff print(ans)
1
3,683,036,359,268
null
82
82
from numpy import* n,m,*a=int_(open(0).read().split()) a=int_(fft.irfft(fft.rfft(bincount(a,[1]*n,2**18))**2)+.5) c=0 for i in where(a>0)[0][::-1]: t=min(m,a[i]) c+=i*t m-=t if m<1:break print(c)
import bisect n,m = map(int,input().split()) A = list(map(int,input().split())) A.sort() S = [0]*(n+1) for i in range(n): S[i+1] = S[i] + A[i] def cnt(x,A,S): res = 0 for i,a in enumerate(A): res += bisect.bisect_left(A,x - a) return res def ans(x,A,S): res = 0 for i,a in enumerate(A): res += a*bisect.bisect_left(A,x-a) + S[bisect.bisect_left(A,x-a)] return res top = A[-1]*2+1 bottom = 0 # mid以上が何個あるか while top - bottom > 1: mid = (top + bottom)//2 if n*n - cnt(mid,A,S) > m: bottom = mid else: top = mid print(S[-1]*2*n - ans(top,A,S) + bottom*(m - (n*n - cnt(top,A,S))))
1
108,385,440,513,680
null
252
252
n = int(input()) ans = int(n/1.08) for i in range(2): if int(ans*1.08) == n: print(ans) exit() ans += 1 print(":(")
def selectionSort(A, N): count = 0 for i in xrange(N): minj = i cflag = 0 for j in xrange(i, N): if A[j] < A[minj]: minj = j cflag = 1 A[i], A[minj] = A[minj], A[i] count+=cflag return count # MAIN N = input() A = map(int, raw_input().split()) c = selectionSort(A, N) print " ".join(map(str, A)) print c
0
null
62,973,937,334,140
265
15
#!/usr/bin/env python3 from scipy.sparse.csgraph import csgraph_from_dense, dijkstra import numpy as np def main(): H, W = list(map(int, input().split())) N = H * W S = [[line for line in range(W)] for row in range(H)] for i in range(H): row = input() for j, line in enumerate(row): S[i][j] = line data = np.zeros([N, N]) for i in range(H): for j in range(W): n = W * i + j if j<W-1 and S[i][j]=='.' and S[i][j+1]=='.': data[n][n+1] = data[n+1][n] = 1 if i<H-1 and S[i][j]=='.' and S[i+1][j]=='.': data[n][n+W] = data[n+W][n] = 1 G = csgraph_from_dense(data) answer = 0 for n in range(N): result = dijkstra(G, indices=n) result = [x for x in result if x!=np.inf] answer = max(answer, max(result)) print(int(answer)) pass if __name__ == '__main__': main()
from collections import deque import copy H,W=map(int,input().split()) S=[list(input()) for _ in range(H)] S1=copy.deepcopy(S) ans=[] sy=0 sx=0 for i in range(H): for j in range(W): c=0 route=deque([(i,j,0)]) S=copy.deepcopy(S1) while route: a,b,n=route.popleft() c=n if 0<=a<=H-1 and 0<=b<=W-1: if S[a][b]=='.': S[a][b]='#' route.append((a+1,b,n+1)) route.append((a-1,b,n+1)) route.append((a,b+1,n+1)) route.append((a,b-1,n+1)) ans.append(c-1) print(max(ans))
1
95,080,453,310,404
null
241
241
n = int(input()) num_lis = [list(map(int, input().split())) for i in range(n)] c = 0 for i,j in num_lis: if c == 3: break if i == j: c += 1 else: c = 0 if c == 3: print("Yes") else: print("No")
a, b, c, d = map(int, input().split(" ")) while True: c -= b if c <= 0: print("Yes") break a -= d if a <= 0: print("No") break
0
null
16,168,430,898,212
72
164
n = int(input()) s = list(input()) cnt = 0 if n % 2 != 0 : print('No') exit() for i in range(n//2): if s[i] != s[i + n // 2]: print('No') exit() print('Yes')
S = input() n = len(S) S_a = list(S) if S[n-1] == 's' : print(S + 'es') else : print(S + 's')
0
null
74,698,587,067,440
279
71
l = input() n = l**3 print n
a,b = input().split() print(a*int(b)) if a < b else print(b*int(a))
0
null
42,391,550,262,530
35
232
processedIp = input().split(" ") k = int(processedIp[0]) x = int(processedIp[1]) totalValue = k*500 if (totalValue >= x): print("Yes") else: print("No")
#ライブラリの読み込み import math #入力値の格納 k,x = map(int,input().split()) #判定 if k * 500 >= x: text = "Yes" else: text = "No" #表示 print(text)
1
97,947,531,937,902
null
244
244
def bubbleSort(A, N): global count flag = True while flag: flag = False for j in range(N-1, 0, -1): if A[j] < A[j-1]: A[j], A[j-1] = A[j-1], A[j] flag = True count += 1 N = int(input()) A = [int(i) for i in input().split()] count = 0 bubbleSort(A, N) print(*A) print(count)
n = int(input()) ac = 0 wa = 0 tle = 0 re = 0 for i in range(n): x = input() if (x == "AC"): ac = ac + 1 elif (x == "WA"): wa = wa + 1 elif (x == "TLE"): tle = tle + 1 else: re = re + 1 print("AC", "x", ac) print("WA", "x", wa) print("TLE", "x", tle) print("RE", "x", re)
0
null
4,386,941,014,472
14
109
class Stack(object): def __init__(self, _max): if type(_max) == int: self._array = [None for i in range(0, _max)] self._next = 0 def push(self, value): if self.isFull(): raise IndexError self._array[self._next] = value self._next += 1 def pop(self): if self.isEmpty(): raise IndexError self._next -= 1 value = self._array[self._next] self._array[self._next] = None return value def isEmpty(self): return self._next <= 0 def isFull(self): return self._next >= len(self._array) def calculator(exp): stack = Stack(100) ope = ["+", "-", "*"] for item in exp: if item in ope: val1 = stack.pop() val2 = stack.pop() if item == "+": stack.push(val2 + val1) elif item == "-": stack.push(val2 - val1) elif item == "*": stack.push(val2 * val1) else: raise ValueError else: stack.push(int(item)) return stack.pop() if __name__ == "__main__": exp = input().split() print(calculator(exp))
import sys a = list(input().split()) stack = [] for i in range(len(a)): n = len(stack) if a[i] == '+': if (n-2) <0: sys.exit("スタックアンダーフロー") tmp = int(stack.pop(n-1)) + int(stack.pop(n-2)) stack.append(tmp) elif a[i] == '-': if (n-2) <0: sys.exit("スタックアンダーフロー") tmp_1 = int(stack.pop(n-1)) tmp_2 = int(stack.pop(n-2)) tmp = tmp_2 - tmp_1 stack.append(tmp) elif a[i] == '*': if (n-2) <0: sys.exit("スタックアンダーフロー") tmp = int(stack.pop(n-1)) * int(stack.pop(n-2)) stack.append(tmp) elif a[i] == '/': if (n-2) <0: sys.exit("スタックアンダーフロー") tmp_1 = int(stack.pop(n-1)) tmp_2 = int(stack.pop(n-2)) tmp = tmp_2 / tmp_1 stack.append(tmp) else: stack.append(int(a[i])) print(stack[0])
1
34,367,191,898
null
18
18
a,*b=[], for z in[*open(0)][1:]:x,y=map(int,z.split());a+=x+y,;b+=x-y, print(max(max(a)-min(a),max(b)-min(b)))
import sys input = sys.stdin.readline INF = 10**18 sys.setrecursionlimit(10**6) def li(): return [int(x) for x in input().split()] N = int(input()) xs, ys = [], [] for i in range(N): x, y = li() xs.append(x) ys.append(y) def get_max_manhattan_distanse(xs, ys): X = [xs[i] + ys[i] for i in range(len(xs))] Y = [xs[i] - ys[i] for i in range(len(xs))] # return max(max(X) - min(X), max(Y) - min(Y)) X.sort() Y.sort() return max(X[-1] - X[0], Y[-1] - Y[0]) ans = get_max_manhattan_distanse(xs, ys) print(ans)
1
3,436,643,181,390
null
80
80
# coding:utf-8 class Dice: def __init__(self): self.f = [0 for i in range(6)] def roll_z(self): self.roll(1,2,4,3) def roll_y(self): self.roll(0,2,5,3) def roll_x(self): self.roll(0,1,5,4) def roll(self,i,j,k,l): t = self.f[i]; self.f[i] = self.f[j]; self.f[j] = self.f[k]; self.f[k] = self.f[l]; self.f[l] = t def move(self,ch): if ch == 'E': for i in range(3): self.roll_y() if ch == 'W': self.roll_y() if ch == 'N': self.roll_x() if ch == 'S': for i in range(3): self.roll_x() dice = Dice() dice.f = map(int, raw_input().split()) com = raw_input() for i in range(len(com)): dice.move(com[i]) print dice.f[0]
class Dice(object): def __init__(self, num): self.num = num def rotate_S(self): self.num = [self.num[4], self.num[0], self.num[2], self.num[3], self.num[5], self.num[1]] def rotate_N(self): self.num = [self.num[1], self.num[5], self.num[2], self.num[3], self.num[0], self.num[4]] def rotate_W(self): self.num = [self.num[2], self.num[1], self.num[5], self.num[0], self.num[4], self.num[3]] def rotate_E(self): self.num = [self.num[3], self.num[1], self.num[0], self.num[5], self.num[4], self.num[2]] dice = Dice(map(int, raw_input().split())) order = raw_input() for i in range(len(order)): if order[i] == 'S': dice.rotate_S() elif order[i] == 'N': dice.rotate_N() elif order[i] == 'W': dice.rotate_W() elif order[i] == 'E': dice.rotate_E() print dice.num[0]
1
231,287,751,952
null
33
33
import sys def resolve(in_): N, X, T = map(int, next(in_).split()) minute = 0 tako = 0 while tako < N: tako += X minute += T return minute def main(): answer = resolve(sys.stdin) print(answer) if __name__ == '__main__': main()
a,b = input().split() if int(a * int(b))>= int(b *int(b)): print(b*int(a)) else: print(a*int(b))
0
null
44,165,522,146,660
86
232
a, b =map(int, input().split()) m = a*b lst = [] for i in range(1, b+1): if m < a*i: break lst.append(a*i) for j in lst: if j%b == 0: print(j) break
from collections import defaultdict N, X, Y = map(int, input().split()) ctr = defaultdict(int) for i in range(1, N + 1): for j in range(i + 1, N + 1): d = min(j - i, abs(i - X) + 1 + abs(j - Y)) ctr[d] += 1 for i in range(1, N): print(ctr[i])
0
null
78,428,388,343,148
256
187
N = int(input()) A = [int(_) for _ in input().split()] A.sort(reverse=True) ans = 0 for i in range(N-1): ans += A[(i+1) // 2] print(ans)
import heapq def main(): _ = int(input()) A = sorted(list(map(int, input().split())), reverse=True) q = [] heapq.heapify(q) ans = A[0] tmp_score = min(A[0], A[1]) heapq.heappush(q, (-tmp_score, A[0], A[1])) heapq.heappush(q, (-tmp_score, A[1], A[0])) for a in A[2:]: g = heapq.heappop(q) ans += -g[0] tmp_score1 = min(a, g[1]) tmp_push1 = (-tmp_score1, a, g[1]) tmp_score2 = min(a, g[2]) tmp_push2 = (-tmp_score2, a, g[2]) heapq.heappush(q, tmp_push1) heapq.heappush(q, tmp_push2) print(ans) if __name__ == '__main__': main()
1
9,152,763,934,340
null
111
111
i = raw_input().strip().split() W = int(i[0]) H = int(i[1]) x = int(i[2]) y = int(i[3]) r = int(i[4]) flag = True if x-r < 0 or x+r > W: flag = False elif y-r < 0 or y+r > H: flag = False if flag: print "Yes" else: print "No"
a = map(int, raw_input().split()) w = a[0] h = a[1] x = a[2] y = a[3] r = a[4] if x+r <= w and x-r >= 0 and y+r <= h and y-r >= 0: print 'Yes' else: print 'No'
1
440,375,148,304
null
41
41
n,k = map(int, input().split()) price = list(map(int, input().split())) count = 0 sum = 0 while count < k: abc = min(price) sum += abc price.pop(price.index(abc)) count += 1 print(sum)
def cin(): in_ = list(map(int,input().split())) if len(in_) == 1: return in_[0] else: return in_ def fact_prime(n, p): ret = 0 temp = p while(n >= p): ret += n // p p *= temp return ret n = cin() if n % 2: n = (n + 1) // 2 a2 = fact_prime(2 * n, 2) a5 = fact_prime(2 * n, 5) b2 = fact_prime(n, 2) + n b5 = fact_prime(n, 5) ans = min(a2 - b2, a5 - b5) else: n = n // 2 ans = fact_prime(n, 5) print(ans)
0
null
64,049,253,279,200
120
258
def main(): from random import sample from operator import itemgetter e=enumerate n,a=open(0) n=int(n) d=[0]+[-2**64]*n for j,(a,i)in e(sorted(sample([(a,i)for i,a in e(map(int,a.split()))],n),key=itemgetter(0),reverse=1)): d=[max(t+a*(~i-j+k+n),d[k-1]+a*abs(~i+k))for k,t in e(d)] print(max(d)) main()
n, m = map(int, input().split()) *C, = map(int, input().split()) dp = [n]*(n+1) dp[0] = 0 for c in C: for i in range(n+1): if i+c > n: break dp[i+c] = min(dp[i+c], dp[i] + 1) print(dp[n])
0
null
16,782,397,319,450
171
28
s=str(input()) c=len(s)//2 e=0 for i in range(0,c): if(s[i]!=s[len(s)-1-i]): e=e+1 print(e)
a,b,c=map(int, input().split()) #たこ焼きが余るかの場合分け if a%b==0: Q=a//b #ちょうど焼ききる else: Q=a//b+1 #余る print(Q*c)#回数×時間 #完了
0
null
61,948,004,181,378
261
86
n, k, s = map(int, input().split()) ans = [s]*k if s != 1: ans += [s-1]*(n-k) else: ans += [2]*(n-k) print(*ans)
n,k,s=map(int,input().split()) a=[s]*(k) for i in range(n-k): a.append(max((s+1)%(10**9),1)) b=[] for i in range(n): b.append(str(a[i])) print(" ".join(b))
1
90,922,155,434,720
null
238
238
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))
from collections import defaultdict N, P = map(int, input().split()) S = input() if P == 2 or P == 5: ans = 0 for i in range(N): if int(S[i]) % P == 0: ans += i + 1 print(ans) else: ctr = defaultdict(int) ctr[0] = 1 cur = 0 for i in reversed(range(N)): cur = (cur + int(S[i]) * pow(10, N - i - 1, P)) % P ctr[cur] += 1 ans = 0 for v in ctr.values(): ans += v * (v - 1) // 2 print(ans)
0
null
79,436,344,921,450
246
205
import os, sys, re, math (N, M) = [int(n) for n in input().split()] if N == M: print('Yes') else: print('No')
from sys import stdin N, M = [int(x) for x in stdin.readline().rstrip().split()] if N == M: print("Yes") else: print("No")
1
83,600,057,266,660
null
231
231
N = int(input()) A = list(map(int,input().split())) #t: 1, 2, 3,..., N #A: A1,A2,A3,...,AN #|t-s| = At+As となるt,sのペアの数は、 # t-s = At+As かつ t>s となるt,sのペアの数と等しく、 # t-At = s+As かつ t>s となるt,sのペアの数と等しい ms = [0]*N ps = [0]*N for i in range(N): ms[i],ps[i] = (i+1)-A[i],(i+1)+A[i] msc,psc = {},{} mi,pi = 0,0 out = 0 while mi<N: msc[ms[mi]] = msc.get(ms[mi],0)+1 out += psc.get(ms[mi],0) psc[ps[mi]] = psc.get(ps[pi],0)+1 mi+=1 pi+=1 print(out)
from collections import defaultdict import bisect def f(): return [] d=defaultdict(f) n=int(input()) a=list(map(int,input().split())) x=[i+1+a[i] for i in range(n)] y=[i+1-a[i] for i in range(n)] for i in range(n): d[y[i]].append(i) ans=0 for i in range(n): ans+=len(d[x[i]]) print(ans)
1
25,977,452,035,830
null
157
157
K=int(input()) ans=[] for i in range(K): ans.extend("ACL") print("".join(ans))
a = int(input()) b = int(input()) ans_list = [1, 2, 3] ans_list.remove(a) ans_list.remove(b) print(ans_list[-1])
0
null
56,328,163,124,026
69
254
def insert_sort(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; print(*A) N = int(input()) A = [int(n) for n in input().split()]; print(*A) insert_sort(A, N)
import math import numpy as np N = int(input()) N_half = math.floor(N/2) counts = 0 for i in np.arange(1, N): j = N-i if(i != j ): counts += 1 print(int(counts/2))
0
null
76,752,552,951,552
10
283
n,m=map(int,input().split()) su=input() rev=su[::-1] if "1"*m in su: print(-1) exit() dist_f_n=[float('inf')]*(n+1) dist_f_n[0]=0 for i in range(1,m+1): if rev[i]=="0": dist_f_n[i]=1 last=i if i==n: break while last<n: for i in range(last+1,last+m+1): if rev[i]=="0": dist_f_n[i]=dist_f_n[last]+1 _last=i if i==n: break last=_last cur=dist_f_n[-1] _i=0 ans=[] for i,x in enumerate(dist_f_n[::-1]): if x==cur-1: ans.append(i-_i) cur-=1 _i=i print(" ".join(map(str,ans)))
import sys sys.setrecursionlimit(10 ** 9) N, M = map(int, input().split()) root = [-1] * N def r(x): if root[x] < 0: return x else: root[x] = r(root[x]) return root[x] def unite(x, y): x = r(x) y = r(y) if x == y: return root[x] += root[y] root[y] = x def size(x): return -r(x) for i in range(M): a, b = map(int, input().split()) a -= 1 b -= 1 unite(a, b) max_size = -min(root) print(max_size)
0
null
71,296,488,076,190
274
84
N = int(input()) for i in range(10): ans = (i+1)*1000-N if ans >= 0: print(ans) exit()
import math i = 0 k = 0 q = 0 while k == 0 : N = int(input()) if N == 0 : k = k + 1 else : S = list(map(float, input().split())) while k ==0 and i < N : m = sum(S) / N q = q + ((S[i] - m)**2) i = i + 1 print(math.sqrt(q / N)) q = 0 i = 0
0
null
4,367,187,519,980
108
31
a,b,c = list(map(int, input().split())) if a+b > c: print('No') else: if 4*a*b < ((c-a-b)**2): print('Yes') else: print('No')
a,b,c = [int(i) for i in input().split()] print("Yes" if(a * a + b * b + c * c - 2 * a * b - 2 * b * c - 2 * c * a > 0 and c - a - b > 0) else "No")
1
51,543,814,253,472
null
197
197
t=str(input()) print(t.replace("?","D"))
def main(): T = input() T = T.replace("?", "D") print(T) if __name__ == '__main__': main()
1
18,459,185,284,704
null
140
140
import fractions N = int(input()) A = list(map(int,input().split())) MOD = 10** 9 + 7 lcm = 1 for a in A: lcm = a // fractions.gcd(a,lcm) * lcm print(sum(lcm//a for a in A)%MOD)
class dise: def __init__(self, label): self.label1 = label[0] self.label2 = label[1] self.label3 = label[2] self.label4 = label[3] self.label5 = label[4] self.label6 = label[5] def N_rotation(self): reg = self.label1 self.label1 = self.label2 self.label2 = self.label6 self.label6 = self.label5 self.label5 = reg def E_rotation(self): reg = self.label1 self.label1 = self.label4 self.label4 = self.label6 self.label6 = self.label3 self.label3 = reg def S_rotation(self): reg = self.label1 self.label1 = self.label5 self.label5 = self.label6 self.label6 = self.label2 self.label2 = reg def W_rotation(self): reg = self.label1 self.label1 = self.label3 self.label3 = self.label6 self.label6 = self.label4 self.label4 = reg dise1 = dise(input().split()) order = list(input()) for i in order: if i == 'N': dise1.N_rotation() elif i == 'E': dise1.E_rotation() elif i == 'S': dise1.S_rotation() elif i == 'W': dise1.W_rotation() print(dise1.label1)
0
null
43,926,371,839,670
235
33
#!python3 from time import perf_counter tick = perf_counter() + 1.92 import sys iim = lambda: map(int, sys.stdin.readline().rstrip().split()) from random import randrange from bisect import bisect def resolve(): it = map(int, sys.stdin.read().split()) D = next(it) C = [next(it) for i in range(26)] S = [[next(it) for i in range(26)] for i in range(D)] L = [[-1]] * 26 T = [0] * D def update(di, qi): score = 0 t0 = T[di] score += S[di][qi]-S[di][t0] a1 = li = L[t0][:] ii = li.index(di) la = D if len(li)-1 == ii else li[ii+1] score -= C[t0] * (di-li[ii-1])*(la-di) li.pop(ii) a2 = li = L[qi][:] ii = bisect(li, di) li.insert(ii, di) la = D if len(li)-1 == ii else li[ii+1] score += C[qi] * (di-li[ii-1])*(la-di) return score, (a1, a2) score = 0 for i in range(D): score += S[i][0] L[0] = [i for i in range(-1, D)] x = (1+D)*D//2 for i in range(1, 26): score -= C[i] * x for di in range(D): for qi in range(26): diff, swap = update(di, qi) if diff > 0: score += diff q0, T[di] = T[di], qi L[q0], L[qi] = swap while perf_counter() < tick: di, qi = randrange(0, D), randrange(0, 26) diff, swap = update(di, qi) if diff > 0: score += diff q0, T[di] = T[di], qi L[q0], L[qi] = swap print(*(i+1 for i in T), sep="\n") if __name__ == "__main__": resolve()
# local search is all you need # 「日付 d とコンテストタイプ q をランダムに選び、d 日目に開催するコンテストをタイプ q に変更する」 # このデメリット→変化させる量が小さすぎるとすぐに行き止まり (局所最適解) に陥ってしまい、逆に、変化させる量が 大きすぎると闇雲に探す状態に近くなって、改善できる確率が低くなってしまう。 # 今回ならば、開催日が全体的に遠すぎず近すぎない感じのlocal minimumに収束する∵d日目のコンテストをi→jに変更したとする。iの開催期間はすごく伸びると2乗でスコアが下がるため、iの開催期間が比較的近いところのiしか選ばれない # →じゃ2点スワップを導入して改善してみよう # あといっぱい回すためにinitやscoreも若干高速化 from time import time t0 = time() import sys sys.setrecursionlimit(1 << 25) read = sys.stdin.readline ra = range enu = enumerate def exit(*argv, **kwarg): print(*argv, **kwarg) sys.exit() def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv)) # 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと def a_int(): return int(read()) def ints(): return list(map(int, read().split())) def read_col(H): '''H is number of rows A列、B列が与えられるようなとき ex1)A,B=read_col(H) ex2) A,=read_col(H) #一列の場合''' ret = [] for _ in range(H): ret.append(list(map(int, read().split()))) return tuple(map(list, zip(*ret))) def read_tuple(H): '''H is number of rows''' ret = [] for _ in range(H): ret.append(tuple(map(int, read().split()))) return ret MOD = 10**9 + 7 INF = 2**31 # 2147483648 > 10**9 # default import from itertools import product, permutations, combinations from bisect import bisect_left, bisect_right # , insort_left, insort_right from functools import reduce from random import randint, random def score(D, C, S, T): '''2~3*D回のループでスコアを計算する''' # last = [-1] * 26 date_by_contest = [[-1] for _ in range(26)] for d, t in enumerate(T): date_by_contest[t].append(d) for i in range(26): date_by_contest[i].append(D) # 番兵 # print(*date_by_contest, sep='\n') score = 0 for d in range(D): score += S[d][T[d]] for c, dates in enu(date_by_contest): for i in range(len(dates) - 1): dd = (dates[i + 1] - dates[i]) # for ddd in range(dd): # score -= C[c] * (ddd) score -= C[c] * (dd - 1) * dd // 2 return score D = a_int() C = ints() S = read_tuple(D) def maximizer(newT, bestT, bestscore): tmpscore = score(D, C, S, newT) if tmpscore > bestscore: return newT, tmpscore else: return bestT, bestscore def ret_init_T(): '''greedyで作ったTを初期値とする。 return ---------- T, score ... 初期のTとそのTで得られるscore ''' def _make_T(n_days): # editorialよりd日目の改善は、改善せずにd+n_days経過したときの関数にしたほうが # 最終的なスコアと相関があるんじゃない? T = [] last = [-1] * 26 for d in range(D): ma = -INF for i in range(26): tmp = S[d][i] dd = d - last[i] tmp += C[i] * (((dd + n_days + dd) * (n_days) // 2)) if tmp > ma: t = i ma = tmp last[t] = d # Tを選んだあとで決める T.append(t) return T T = _make_T(2) sco = score(D, C, S, T) for i in range(3, 16): T, sco = maximizer(_make_T(i), T, sco) return T, sco bestT, bestscore = ret_init_T() def add_noise(T, thre_p, days_near): '''確率的にどちらかの操作を行う 1.日付dとコンテストqをランダムに選びd日目に開催するコンテストのタイプをqに変更する 2.10日以内の点でコンテストを入れ替える thre_pはどちらの行動を行うかを調節、days_nearは近さのパラメータ''' ret = T.copy() if random() < thre_p: d = randint(0, D - 1) q = randint(0, 25) ret[d] = q return ret else: i = randint(0, D - 2) j = randint(i - days_near, i + days_near) j = max(j, 0) j = min(j, D - 1) if i == j: j += 1 ret[i], ret[j] = ret[j], ret[i] return ret while time() - t0 < 1.9: for _ in range(100): bestT, bestscore = maximizer( add_noise(bestT, 0.8, 8), bestT, bestscore) # print(bestscore) # print(score(D, C, S, T)) print(*mina(*bestT, sub=-1), sep='\n')
1
9,679,843,056,580
null
113
113
def main(): h, w, m = map(int, input().split()) hp = [0]*h wp = [0]*w hw = set() for _ in range(m): h1, w1 = map(int, input().split()) hp[h1-1] += 1 wp[w1-1] += 1 hw.add((h1, w1)) h_max = max(hp) w_max = max(wp) hh = [i+1 for i, v in enumerate(hp) if v == h_max] ww = [i+1 for i, v in enumerate(wp) if v == w_max] ans = h_max + w_max for hb in hh: for wb in ww: if (hb, wb) not in hw: print(ans) exit() print(ans-1) if __name__ == '__main__': main()
import sys sys.setrecursionlimit(10**6) h, w, m = map(int, input().split()) # 各行各列に何個ずつあるか hs = [0] * h ws = [0] * w # 座標を記録 s = set() readline = sys.stdin.readline for _ in range(m): r, c = [int(i) for i in readline().split()] r -= 1 c -= 1 hs[r] += 1 ws[c] += 1 s.add((r, c)) # 最大値 mh = 0 mw = 0 for i in range(h): mh = max(mh, hs[i]) for j in range(w): mw = max(mw, ws[j]) # 最大値をとっている行・列 si = [] sj = [] for i in range(h): if mh == hs[i]: si.append(i) for j in range(w): if mw == ws[j]: sj.append(j) # 爆破対象がないマスに爆弾を設置できれば尚良し。そこでmh+mwをansに仮に記録して、1個でも見つかればその座標を出力。見つからなければ、爆破対象があるマスに爆弾を設置するのが最大となるので、ans=mh+mw-1となる ans = mh + mw for i in si: for j in sj: if (i, j) in s: continue print(ans) exit() print(ans-1)
1
4,732,041,637,700
null
89
89
class PrintARectangle: def output(self, H, W): s = "" for w in range(W): s += "#" for h in range(H): print s print if __name__ == "__main__": pr = PrintARectangle() while True: h, w = map(int, raw_input().split()) if h == 0 and w == 0: break pr.output(h, w)
def getN(): return int(input()) def getNM(): return map(int, input().split()) def getList(): return list(map(int, input().split())) from collections import defaultdict, deque from sys import exit import math import copy from bisect import bisect_left, bisect_right from heapq import * import sys # sys.setrecursionlimit(1000000) INF = 10 ** 17 MOD = 1000000007 from fractions import * def inverse(f): # return Fraction(f.denominator,f.numerator) return 1 / f def combmod(n, k, mod=MOD): ret = 1 for i in range(n - k + 1, n + 1): ret *= i ret %= mod for i in range(1, k + 1): ret *= pow(i, mod - 2, mod) ret %= mod return ret MOD = 10 ** 9 + 7 def solve(): n = getN() bit_array = [[0, 0] for i in range(61)] nums = getList() for num in nums: digit = 0 while(digit < 61): if num % 2 == 0: bit_array[digit][0] += 1 else: bit_array[digit][1] += 1 digit += 1 num //= 2 # print(bit_array) ans = 0 for i, tgt in enumerate(bit_array): ans += pow(2, i, MOD) * tgt[0] * tgt[1] ans %= MOD print(ans ) def main(): n = getN() for _ in range(n): solve() if __name__ == "__main__": solve()
0
null
61,932,724,896,562
49
263
A,B = map(int,input().split()) if A - 2*B > 0 : print(A-2*B) else : print("0")
def getN(): return int(input()) def getNM(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getArray(intn): return [int(input()) for i in range(intn)] def input(): return sys.stdin.readline().rstrip() def rand_N(ran1, ran2): return random.randint(ran1, ran2) def rand_List(ran1, ran2, rantime): return [random.randint(ran1, ran2) for i in range(rantime)] def rand_ints_nodup(ran1, ran2, rantime): ns = [] while len(ns) < rantime: n = random.randint(ran1, ran2) if not n in ns: ns.append(n) return sorted(ns) def rand_query(ran1, ran2, rantime): r_query = [] while len(r_query) < rantime: n_q = rand_ints_nodup(ran1, ran2, 2) if not n_q in r_query: r_query.append(n_q) return sorted(r_query) from collections import defaultdict, deque, Counter from sys import exit from decimal import * import heapq import math from fractions import gcd import random import string import copy from itertools import combinations, permutations, product from operator import mul, itemgetter from functools import reduce from bisect import bisect_left, bisect_right import sys sys.setrecursionlimit(1000000000) mod = 10 ** 9 + 7 ############# # Main Code # ############# N = input() N = [int(i) for i in N] # 数え上げ問題なので多分dp dp = [[0, 0] for i in range(len(N))] # ぴったし払うための最小値 dp[0][0] = min(N[0], 11 - N[0]) # お釣りをもらう用の紙幣を1枚余分にとっておく場合の最小値 dp[0][1] = min(N[0] + 1, 10 - N[0]) for i in range(1, len(N)): # dp[i - 1][1] + 10 - N[i]:とっておいた紙幣を使用し、お釣りを10 - N[i]枚もらう dp[i][0] = min(dp[i - 1][0] + N[i], dp[i - 1][1] + 10 - N[i]) # dp[i - 1][1] + 9 - N[i]:お釣りを10 - N[i]枚もらい、そのうち1枚は次のお釣りを # もらう試行のためにとっておく dp[i][1] = min(dp[i - 1][0] + N[i] + 1, dp[i - 1][1] + 9 - N[i]) print(dp[len(N) - 1][0])
0
null
118,519,026,430,658
291
219
import sys from io import StringIO import unittest import os from operator import ixor # 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む) sys.setrecursionlimit(999999999) # 実装を行う関数 def resolve(test_def_name=""): # 数値取得サンプル # 1行1項目 n = int(input()) # 1行2項目 x, y = map(int, input().split()) # 1行N項目 x = [list(map(int, input().split()))] # N行1項目 x = [int(input()) for i in range(n)] # N行N項目 x = [list(map(int, input().split())) for i in range(n)] # 文字取得サンプル # 1行1項目 x = input() # 1行1項目(1文字ずつリストに入れる場合) x = list(input()) n = int(input()) a_s = list(map(int, input().split())) one_counts = [0 for i in range(61)] zero_counts = [0 for i in range(61)] # 以下、XORで便利と思われる処理。 # ------------------------------------------- # ループ回数の取得(最大の数値の桁数分、ループ処理を行うようにする) max_num = max(a_s) max_loop = 0 for i in range(0, 61): if max_num < 1 << i: break max_loop += 1 # ループ処理にて、各桁の0と1の個数を数え上げる。 # 例:入力[1(01),2(10),3(11)] -> one_count=[2,2,0・・]とzero_counts[1,1,0・・]を得る。 for a in a_s: for i in range(0, max_loop): if a & 1 << i: one_counts[i] += 1 else: zero_counts[i] += 1 # ------------------------------------------- # 以上、XORで便利と思われる処理。 # 結果を計算する処理 ans = 0 for i in range(0, max_loop): # 各桁の「0の個数」*「1の個数」*2^(0~桁数-1乗まで) を加算していく ans += (one_counts[i] * zero_counts[i] * pow(2, i)) % (pow(10, 9) + 7) ans = ans % (pow(10, 9) + 7) # 結果を出力 print(ans) # テストクラス class TestClass(unittest.TestCase): def assertIO(self, assert_input, output): stdout, sat_in = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(assert_input) resolve(sys._getframe().f_back.f_code.co_name) sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, sat_in self.assertEqual(out, output) def test_input_1(self): test_input = """3 1 2 3""" output = """6""" self.assertIO(test_input, output) def test_input_2(self): test_input = """10 3 1 4 1 5 9 2 6 5 3""" output = """237""" self.assertIO(test_input, output) def test_input_3(self): test_input = """10 3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820""" output = """103715602""" self.assertIO(test_input, output) # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること def tes_t_1original_1(self): test_input = """データ""" output = """データ""" self.assertIO(test_input, output) # 実装orテストの呼び出し if __name__ == "__main__": if os.environ.get("USERNAME") is None: # AtCoder提出時の場合 resolve() else: # 自PCの場合 unittest.main()
import sys input=sys.stdin.readline n=int(input()) cnt=[] for i in range(n): s=input().rstrip() l=s.count("(") r=s.count(")") minus=0 now=0 for j in range(len(s)): if s[j]=="(": now+=1 else: now-=1 minus=min(minus,now) cnt.append((l,r,s,minus)) cnt.sort(key=lambda x:x[1]-x[0]) plus=[] minus=[] #x[3]は最小値 x[0]-x[1]が合計値 for x in cnt: if x[0]-x[1]>0: plus.append(x) else: minus.append(x) plus.sort(key=lambda x:-x[3])#最小値の少ない(0に近い方から) minus.sort(key=lambda x:-(x[0]-x[1]-x[3])) lr=0#l-r for x in plus+minus: if lr+x[3]<0: print("No") exit() lr+=x[0]-x[1] if lr==0: print("Yes") else: print("No")
0
null
73,487,078,955,290
263
152
x, y = map(int, input().split()) if y/2 != int(y/2): print("No") else: p = 2*x - y/2 q = y/2 - x if p >= 0: if q >= 0: print("Yes") else: print("No") else: print("No")
n = int(input()) cnt = 0 judge = False for i in range(n): a,b = map(int,input().split()) if a == b: cnt += 1 else: cnt = 0 if cnt == 3: judge = True if judge: print("Yes") else: print("No")
0
null
8,190,090,531,896
127
72
x1,y1,x2,y2=map(float,input().split()) d=((x1-x2)**2 + (y1-y2)**2)**(1/2) print(d)
import math data =map(float, raw_input().split(" ")) dx = data[2] - data[0] dy = data[3] - data[1] print math.sqrt(dx**2+dy**2)
1
156,642,495,072
null
29
29
p1 , s1=map(int , input().split()) p2 , s2=map(int , input().split()) t=int(input()) if abs(p1-p2) > t*(s1-s2): print("NO") else: print("YES")
a, v = map(int, input().split()) b, w = map(int, input().split()) t = int(input()) print('YES' if abs(b-a)-(v-w)*t <= 0 else 'NO')
1
15,101,910,880,980
null
131
131
import math a, b, C = map(int, input().split()) C = C / 180 * math.pi S = a * b * math.sin(C) / 2 print(S) print(a + b + math.sqrt(a**2 + b**2 - 2*a*b*math.cos(C))) print(2 * S / a)
# -*- coding: utf-8 -*- import math a, b, C = map(float, input().split()) c = math.sqrt((a * a) + (b * b) - 2 * a * b * math.cos(math.radians(C))) s = (a * b * math.sin(math.radians(C))) / 2 l = a + b + c h = (2 * s) / a print(s) print(l) print(h)
1
175,304,939,040
null
30
30
x = input() split_x = x.split(sep=" ") a = split_x[0] b = split_x[1] if int(a) < int(b): print("a < b") if int(a) == int(b): print("a == b") if int(a) > int(b): print("a > b")
a,b=map(int,input().split());print('a == b'if a==b else'a > b'if a>b else'a < b')
1
362,081,636,522
null
38
38
def resolve(): N = int(input()) print(1000 * ((1000 + (N-1)) // 1000) - N) resolve()
import sys input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().split()) inl = lambda: list(map(int, input().split())) n = ini() ans = n % 1000 print(0 if ans == 0 else 1000 - ans)
1
8,457,009,471,590
null
108
108
#! /usr/bin/env python # -*- coding: utf-8 -*- raw_list = sorted(map(int, raw_input().split())) print("%d %d %d") % (raw_list[0], raw_list[1], raw_list[2])
import itertools n = int(input()) a = list(map(int, input().split())) q = int(input()) M = list(map(int, input().split())) able = [0] * (max(n * max(a) + 1, 2001)) for item in itertools.product([0, 1], repeat=n): total = sum([i * j for i, j in zip(a, item)]) able[total] = 1 for m in M: print('yes' if able[m] else 'no')
0
null
258,225,886,688
40
25
a=int(input()) b,c=input().split() b=int(b) c=int(c) if b%a==0 or c%a==0: print("OK") else: if int(b/a)==int(c/a): print("NG") else: print("OK")
import sys def Ii():return int(sys.stdin.buffer.readline()) def Mi():return map(int,sys.stdin.buffer.readline().split()) def Li():return list(map(int,sys.stdin.buffer.readline().split())) k = Ii() a,b = Mi() for i in range(a,b+1): if i%k == 0: print('OK') exit(0) print('NG')
1
26,513,230,245,852
null
158
158
x = int(input()) a, r = x // 500, x % 500 b = r // 5 print(1000 * a + 5 * b)
X=int(input()) h500=X//500 h5=(X-h500*500)//5 print(h500*1000+h5*5)
1
42,699,362,485,562
null
185
185