input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop
def sRaw():
return input().rstrip("\r")
def iRaw():
return int(eval(input()))
def ssRaw():
return input().split()
def isRaw():
return list(map(int, ssRaw()))
INF = 1 << 2... | import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop
def sRaw():
return input().rstrip("\r")
def iRaw():
return int(eval(input()))
def ssRaw():
return input().split()
def isRaw():
return list(map(int, ssRaw()))
INF = 1 << 2... | p02609 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
in_n = lambda: int(readline())
in_nn = lambda: list(map(int, readline().split()))
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 =... | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
N = int(readline())
X = readline().rstrip().decode('utf-8')
nx = int(X, 2)
ans = [0] * N
oc = X.count('1')
if oc - 1 != 0:
mx = nx % (oc ... | p02609 |
# coding: utf-8
from collections import defaultdict, deque
def popcount(x):
assert x.bit_length() <= 32
x -= (x >> 1) & 0x55555555
x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
x = (x + (x >> 4)) & 0x0f0f0f0f
x += x >> 8
x += x >> 16
return x & 0x3f
def solve(*args: str) -... | # coding: utf-8
def popcount(x):
assert x.bit_length() <= 32
x -= (x >> 1) & 0x55555555
x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
x = (x + (x >> 4)) & 0x0f0f0f0f
x += x >> 8
x += x >> 16
return x & 0x3f
def solve(*args: str) -> str:
n = int(args[0])
x = args[1]
... | p02609 |
N = int(input())
S = str(input())
X = [];Bit = 0
for i in range(N):
if S[i] == "1":
Bit += 1
X.append(int(S[i]))
#print(X)
MAX = 2*pow(10,5)+10
small = [-1 for _ in range(MAX)]
small[0] = 0; small[1] = 1; small[2] = 1; small[3] = 2
for i in range(4,MAX):
ret = 0; target = i*1
while i > 0:
... | N = int(input())
S = str(input())
X = [];Bit = 0;Num = 0
for i in range(N):
if S[i] == "1":
Bit += 1
Num += pow(2,N-1-i)
X.append(int(S[i]))
#print(X)
#print(Num)
MAX = 2*pow(10,5)+10
small = [-1 for _ in range(MAX)]
small[0] = 0; small[1] = 1; small[2] = 1; small[3] = 2
for i in range(4,MAX):
... | p02609 |
def popcount(x):
return bin(x).count("1")
N, X = open(0).read().split()
N = int(N)
XS = list(X)
X = int(X, 2)
p = popcount(X)
if p == 0:
xi = xd = 0
elif p == 1:
xi = X % 2
xd = 0
else:
xi = X % (p + 1)
xd = X % (p - 1)
for i in range(0, N):
if XS[i] == "1":
if... | def popcount(x):
return bin(x).count("1")
N, X = open(0).read().split()
N = int(N)
XS = list(X)
X = int(X, 2)
p = popcount(X)
xi = X % (p + 1)
if p == 1:
xd = 0
else:
xd = X % (p - 1)
for i in range(N):
if XS[i] == "1":
if p == 1:
print((0))
continue... | p02609 |
import sys
input = sys.stdin.readline
import math
def read():
N = int(input().strip())
X = input().strip()
return N, X
def strdiv(X, k, d):
P = 0
q = 0
for i in range(len(X)):
q <<= 1
if i == k:
q |= 1-int(X[i])
else:
q |= in... | import sys
input = sys.stdin.readline
import math
def read():
N = int(input().strip())
X = input().strip()
return N, X
def bitdiv(N:int, X:str, d:int):
q = 0
for i in range(N):
q <<= 1
q |= int(X[i])
p, q = divmod(q, d)
return q
def solve(N, X):
... | p02609 |
import sys
input = sys.stdin.readline
import math
def read():
N = int(input().strip())
X = input().strip()
return N, X
def bitdiv(N:int, X:str, d:int):
q = 0
for i in range(N):
q <<= 1
q |= int(X[i])
p, q = divmod(q, d)
return q
def solve(N, X):
... | import sys
input = sys.stdin.readline
import math
def read():
N = int(input().strip())
X = input().strip()
return N, X
def bitdiv(N:int, X:str, d:int):
q = 0
for i in range(N):
q <<= 1
q |= int(X[i])
p, q = divmod(q, d)
return q
def solve(N, X):
... | p02609 |
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
... | 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
... | p02609 |
import sys
def popcount(n):
return '{:b}'.format(n).count('1')
def f(n, firstpopcount):
if not n:
return 0
i = 1
n %= firstpopcount
# print(f'{n=}')
while n:
i += 1
n %= popcount(n)
return i
def resolve(in_):
n = int(in_.readline())
... | import sys
def popcount(n):
c = 0
while n:
if n & 1:
c += 1
n >>= 1
return c
def f(n, c=0):
while pc := popcount(n):
n %= pc
c += 1
return c
def _resolve(n, x_bytes):
one_i = b'1'[0]
pc_x = x_bytes.count(b'1')
... | p02609 |
# -*- coding: utf-8 -*-
from functools import lru_cache
import sys
sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_int_n():
return l... | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_int_n():
return list(map(int, buff_readline().split(... | p02609 |
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_str():
return readline().strip()
def slv(N, X... | # -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_str():
return readline().strip()
def slv(N, X... | p02609 |
def popcount(n):
return bin(n).count("1")
cache = [-1] * (2 * 10 ** 5 + 1)
def f(n):
if cache[X] != -1:
return cache[X]
return f(n % popcount(n)) + 1
N = int(input())
X = int(input(), 2)
result = []
for i in range(N):
n = X ^ (1 << (N - 1 - i))
t = 0
while n !=... | def popcount(n):
return bin(n).count("1")
def f(n):
if n == 0:
return 0
return 1 + f(n % popcount(n))
N = int(input())
X = input()
c = X.count('1')
t1 = [0] * N
t1[0] = 1 % (c + 1)
for i in range(1, N):
t1[i] = (t1[i - 1] << 1) % (c + 1)
x1 = 0
for i in range(N):
if ... | p02609 |
#!python3
import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from functools import lru_cache
@lru_cache(maxsize=2*10**5)
def f2(x):
if x == 0: return 0
y = bin(x).count("1")
return 1 + f2(x % y)
def resolve():
N = int(input())
W = 16
S = input()
cnt... | #!python3
import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from functools import lru_cache
@lru_cache(maxsize=2*10**5)
def f2(x):
if x == 0: return 0
y = bin(x).count("1")
return 1 + f2(x % y)
def resolve():
N = int(input())
S = input()
cnt0 = S.count(... | p02609 |
#!python3
import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from functools import lru_cache
@lru_cache(maxsize=2*10**5)
def f2(x):
if x == 0: return 0
y = bin(x).count("1")
return 1 + f2(x % y)
def resolve():
N = int(input())
S = input()
cnt0 = S.count(... | #!python3
import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
def resolve():
N = int(input())
S = input()
cnt0 = S.count("1")
if cnt0 == 0:
print(*[1]*N, sep="\n")
return
X = int(S, 2)
def f2():
cache = [-1] * max(9, cnt0+1)
... | p02609 |
n, X = open(0).read().split()
n = int(n)
x = int(X, 2)
X = list(map(int, X))
def popcount(q):
c = 0
while q:
q, r = divmod(q, 2)
c += r
return c
ma = 2 * 10**5 + 1
dp = [0] * ma
for i in range(1, ma):
dp[i] = dp[i % popcount(i)] + 1
c = popcount(x)
a = x % (c+1)
if ... | n, X = open(0).read().split()
n = int(n)
x = int(X, 2)
X = list(map(int, X))
def popcount(n):
return bin(n).count('1')
ma = 2 * 10**5 + 1
dp = [0] * ma
for i in range(1, ma):
dp[i] = dp[i % popcount(i)] + 1
c = popcount(x)
a = x % (c+1)
if c == 1:
for i in range(n):
if X[i]:
... | p02609 |
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from co... | import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from co... | p02609 |
"""
NTC here
"""
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
profile = 0
pypy = 1
def iin(): return int(input())
def lin(): return list(map(int, input().split()))
def main():
T = 1
while T:
T -= 1
dp = {}
n = iin()
... | """
NTC here
"""
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
profile = 0
pypy = 1
def iin(): return int(input())
def lin(): return list(map(int, input().split()))
def main():
T = 1
while T:
T -= 1
n = iin()
a = list(map(in... | p02609 |
def popcount(n):
bit_count = bin(n).count('1')
n = n % bit_count
return n
if __name__ == "__main__":
import sys
readline = sys.stdin.readline
N = int(eval(input()))
X = readline()
bit_count = X.count('1')
X_int = int(X, 2)
for i in range(N):
# ビット... | def popcount(n):
bit_count = bin(n).count('1')
n = n % bit_count
return n
if __name__ == "__main__":
import sys
readline = sys.stdin.readline
N = int(eval(input()))
X = readline()
bit_count = X.count('1')
mod1 = int(X, 2) % (bit_count + 1)
mod2 = int(X, 2) % (... | p02609 |
N = int(eval(input()))
X = input().strip()
def popcount(n):
cnt = 0
while n > 0:
cnt += n & 1
n //= 2
return cnt
def f(n):
cnt = 0
while n != 0:
n = n % popcount(n)
cnt += 1
return cnt
nX = int(X, 2)
pcnt = X.count('1')
MOD1 = pcnt + 1
MOD2 ... | import sys
N = int(sys.stdin.readline().strip())
X = sys.stdin.readline().strip()
def popcount(n):
cnt = 0
while n > 0:
cnt += n & 1
n //= 2
return cnt
def f(n):
cnt = 0
while n != 0:
n = n % popcount(n)
cnt += 1
return cnt
#nX = int(X, 2)
... | p02609 |
import sys
from collections import defaultdict
sys.setrecursionlimit(300000)
memp = {}
def popcount(n):
if n == 0:
return 0
global memp
if n not in memp:
memp[n] = (n & 1) + popcount(n >> 1)
return memp[n]
memf = {}
def f(x):
if x == 0:
return 0
g... | import sys
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(300000)
memp = {}
def popcount(n):
if n == 0:
return 0
global memp
if n not in memp:
memp[n] = (n & 1) + popcount(n >> 1)
return memp[n]
memf = {}
def f(x):
if x... | p02609 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return l... | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return l... | p02609 |
def solve():
N = int(input())
X = input()
bl = X.count('1')
bitp = 0
for i in range(N):
bitp += int(X[i])*pow(2,N-1-i)
bitp %= bl+1
bitm = 0
if bl>1:
for i in range(N):
bitm += int(X[i])*pow(2,N-1-i)
bitm %= bl-1
ans = [0]*N
dp = [0]*(N+1)
for i in range(1,... | def solve():
N = int(input())
X = input()
bl = X.count('1')
bitp = 0
for i in range(N):
bitp += int(X[i])*pow(2,N-1-i,bl+1)
bitp %= bl+1
bitm = 0
if bl>1:
for i in range(N):
bitm += int(X[i])*pow(2,N-1-i,bl-1)
bitm %= bl-1
ans = [0]*N
dp = [0]*(N+1)
for i i... | p02609 |
### ----------------
### ここから
### ----------------
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(20000000)
def yn(b):
print(("Yes" if b==1 else "No"))
return
def count_ones_by_bin(num):
bin_num = bin(num)[2:]
count = 0
for i in bin_num:
co... | ### ----------------
### ここから
### ----------------
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(20000000)
def yn(b):
print(("Yes" if b==1 else "No"))
return
def count_ones_by_bin(num):
bin_num = bin(num)[2:]
count = 0
for i in bin_num:
co... | p02609 |
from functools import lru_cache
def popcnt(x):
return bin(x).count("1")
@lru_cache(maxsize=None)
def rec(n):
if n == 0:
return 0
else:
return rec(n % popcnt(n)) + 1
ret = [rec(x) for x in range(2 * 10**5 + 1)]
n = int(input())
arr = input()
ALL_ARR = int(arr, 2)... | from functools import lru_cache
def popcnt(x):
return bin(x).count("1")
@lru_cache(maxsize=None)
def rec(n):
if n == 0:
return 0
else:
return rec(n % popcnt(n)) + 1
ret = [rec(x) for x in range(2 * 10**5 + 1)]
n = int(input())
arr = input()
ALL_ARR = int(arr, 2)... | p02609 |
from functools import lru_cache
def popcnt(x):
return bin(x).count("1")
@lru_cache(maxsize=None)
def rec(n):
if n == 0:
return 0
else:
return rec(n % popcnt(n)) + 1
ret = [rec(x) for x in range(2 * 10**5 + 1)]
n = int(input())
arr = input()
ALL_ARR = int(arr, 2)... | from functools import lru_cache
def popcnt(x):
return bin(x).count("1")
@lru_cache(maxsize=None)
def rec(n):
if n == 0:
return 0
else:
return rec(n % popcnt(n)) + 1
n = int(input())
arr = input()
ALL_ARR = int(arr, 2)
cnt = popcnt(int(arr, 2))
init_big = ALL_ARR ... | p02609 |
def main():
import sys
input=sys.stdin.readline
n=int(eval(input()))
x=input().rstrip()
p=x.count("1")
minus=0
plus=0
numlst=[0]*n
for i in range(n):
number=2**i
numlst[-i-1]=number
if x[-i-1]=="1":
plus+=number
plus%=(p+1... | import sys
input=sys.stdin.readline
n=int(eval(input()))
x=input().rstrip()
x10=int(x,2)
pcx=x.count("1")
plus=x10%(pcx+1)
if pcx>=2:
minus=x10%(pcx-1)
def popcnt(x):
cnt=1
while x>=1:
x%=(bin(x).count("1"))
cnt+=1
return cnt
for i in range(n):
if x[i]=="0":
... | p02609 |
import sys
readline = sys.stdin.readline
N = int(readline())
X = readline().rstrip()
X0 = int(X, 2)
count1 = X.count("1")
X0mod0to1 = X0 % (count1 + 1) # 0->1に変化した際の、X0を割った余り
if count1 > 1:
X0mod1to0 = X0 % (count1 - 1) # 1->0に変化した際の、X0を割った余り
else:
X0mod1to0 = 0
from collections import defaultdic... | import sys
readline = sys.stdin.readline
N = int(readline())
X = readline().rstrip()
X0 = int(X, 2)
count1 = X.count("1")
X0mod0to1 = X0 % (count1 + 1) # 0->1に変化した際の、X0を割った余り
if count1 > 1:
X0mod1to0 = X0 % (count1 - 1) # 1->0に変化した際の、X0を割った余り
else:
X0mod1to0 = 0
from collections import defaultdic... | p02609 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_chee... | import sys
sys.setrecursionlimit(100000000)
moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def search(dataset):
min_time = 0
mouse = 1
gx, gy = get_start(dataset)
for i in range(N):
gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)
mouse += 1
min_time += min_tim... | p00481 |
def getStart(field):
search_field = field[1:]
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
gotten_cheese... |
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,start_y,goal_N):
direction ... | p00481 |
from collections import deque
def getStart(field):
search_field = field[1:] # cut the first [H,W,N] for min_path matrix
for y in range(len(search_field)):
for x in range(len(search_field[0])):
if search_field[y][x] == "S":
return x,y+1
def bfs(field,start_x,s... |
def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in directi... | p00481 |
def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in directi... | moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def search(dataset):
min_time = 0
mouse = 1
gx, gy = get_start(dataset)
for i in range(N):
gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)
mouse += 1
min_time += min_time_tmp
print(min_time)
def bfs(dataset... | p00481 |
def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in directi... | def bfs(field,H,W,start_x,start_y,tmp_N):
direction = [[-1,0],[1,0],[0,-1],[0,1]]
que = []
que.append([start_x,start_y])
INF = 1000000
min_path = [[INF] * W for i in range(H)]
min_path[start_y][start_x] = 0
while len(que) != 0:
current = que.pop(0)
for d in direction... | p00481 |
import sys
file_input = sys.stdin
H, W, N = list(map(int, file_input.readline().split()))
town_map = file_input.read()
start_index = town_map.index('S')
mouse = [start_index, 0]
# position, elapsed time
from collections import deque
for goal in range(1, N + 1):
q = deque()
unvisited = [T... | import sys
file_input = sys.stdin
H, W, N = list(map(int, file_input.readline().split()))
town_map = 'X' * (W + 2)
for line in file_input:
town_map += 'X' + line.rstrip() + 'X'
town_map += 'X' * (W + 2)
move = (1, -1, W + 2, -W - 2)
from collections import deque
def search(start_pos, goal_lot):... | p00481 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys
import re
def check_out_of_map(pos,H,W):
H = len(Map)
W = len(Map[0])
if pos[0] < 0 or pos[1] < 0:
return False
if pos[0] >= H or pos[1] >= W:
return False
return True
def bfs(s,g,Map,H,W):
... | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys
def check_out_of_map(pos,H,W):
H = len(Map)
W = len(Map[0])
if pos[0] < 0 or pos[1] < 0:
return False
if pos[0] >= H or pos[1] >= W:
return False
return True
def bfs(s,g,Map,H,W):
queue = [... | p00481 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys
def check_out_of_map(pos,H,W):
H = len(Map)
W = len(Map[0])
if pos[0] < 0 or pos[1] < 0:
return False
if pos[0] >= H or pos[1] >= W:
return False
return True
def bfs(s,g,Map,H,W):
queue = [... | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys
def check_out_of_map(pos,H,W):
if pos[0] < 0 or pos[1] < 0:
return False
if pos[0] >= H or pos[1] >= W:
return False
return True
def bfs(s,g,Map,H,W):
queue = []
queue.append(s)
dist = [[-1 f... | p00481 |
def f(s):
s=list(map(int,s.split(':')))
return (s[0]*60+s[1])*60+s[2]
a=86401
while 1:
t=[0]*a
n=int(eval(input()))
if n==0:break
for _ in range(n):
b,c=input().split()
t[f(b)]+=1;t[f(c)]-=1
d=0
for i in range(1,a):
t[i]+=t[i-1]
d=max(d,t[i])... | def f(s):
return (s[0]*60+s[1])*60+s[2]
a=86400
while 1:
t=[0]*a
n=int(eval(input()))
if n==0:break
for _ in range(n):
b,c = [list(map(int,x.split(':'))) for x in input().split()]
t[f(b)]+=1;t[f(c)]-=1
d=0
for i in range(1,a):
t[i]+=t[i-1]
if d<t[... | p01138 |
m=24*60*60
while True:
n=int(eval(input()))
if n==0:break
dp=[0]*(m+1)
for _ in range(n):
a,d=input().split()
a,b,c=list(map(int,a.split(':')));
d,e,f=list(map(int,d.split(':')));
dp[a*3600+b*60+c]+=1
dp[d*3600+e*60+f]-=1
for i in range(m):
dp[i+1]+=dp[i]
print((max(dp))) | m=24*60*60
while True:
n=int(input())
if n==0:break
dp=[0]*(m+1)
for _ in range(n):
a,d=input().split()
a,b,c=list(map(int,a.split(':')));
d,e,f=list(map(int,d.split(':')));
dp[a*3600+b*60+c]+=1
dp[d*3600+e*60+f]-=1
for i in range(m):
dp[i+1]+=dp[i]
print((max(dp))) | p01138 |
def tosec(t):
return int(t[:2])*60*60 + int(t[3:5])*60 + int(t[6:])
while True:
n = int(eval(input()))
if n == 0: break
time = [0] * (60*60*24 + 1)
for _ in range(n):
s, f = input().split()
time[tosec(s)] += 1
time[tosec(f)] -= 1
ans = time[0]
for i i... | while True:
n = eval(input())
if not n: break
time = [0]*86400
for i in range(n):
s, t = input().split()
ssec = int(s[0:2])*3600 + int(s[3:5])*60 + int(s[6:8])
tsec = int(t[0:2])*3600 + int(t[3:5])*60 + int(t[6:8])
time[ssec] += 1
time[tsec] -= 1
an... | p01138 |
import re
while True:
n = int(input())
if n == 0:
break
l = [0] * (24*60*60+1)
for _ in range(n):
h1,m1,s1,h2,m2,s2 = list(map(int, re.split(':| ', input())))
l[h1*60*60 + m1*60 + s1] += 1
l[h2*60*60 + m2*60 + s2] -= 1
r = 0
for i in range(24*60*60):
... | while True:
n = int(input())
if n == 0:
break
l = []
for _ in range(n):
hms1, hms2 = input().split()
l.append((hms1, True))
l.append((hms2, False))
l.sort()
r, t = 0, 0
for i in range(len(l)):
if l[i][1]:
t += 1
else:
... | p01138 |
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)... | import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)... | p03274 |
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)... | import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
d... | p03274 |
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
dis = 300000000
for i in range(N-K+1):
y = list(x[i:i+K])
disl = abs(x[i]) + abs(x[i+K-1]-x[i])
disr = abs(x[i+K-1]) + abs(x[i+K-1]-x[i])
dis = min(dis, disl, disr)
# if (y[0]*y[-1]<0):
# disy = y[-1]-y... | N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
dis = 300000000
for i in range(N-K+1):
disl = abs(x[i]) + abs(x[i+K-1]-x[i])
disr = abs(x[i+K-1]) + abs(x[i+K-1]-x[i])
dis = min(dis, disl, disr)
# if (y[0]*y[-1]<0):
# disy = y[-1]-y[0]+min(abs(y[-1]), abs(y[... | p03274 |
def main():
# editorial読んだ.
N, K = list(map(int, input().split(" ")))
coordinates = list(map(int, input().split(" ")))
result = 10 ** 9 + 7
for i in range(N - K + 1):
result = min(result, abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i]),
abs(coor... | def main():
N, K = list(map(int, input().split(" ")))
X = list(map(int, input().split(" ")))
answer = 10 ** 9 + 7
for i in range(0, N - K + 1):
time = X[i + K - 1] - X[i] + min(abs(X[i]), abs(X[i + K - 1]))
answer = min(answer, time)
print(answer)
if __name__ == "__main__":
main() | p03274 |
def main():
n, k = list(map(int, input().split()))
x = [int(x) for x in input().split()]
answer = float("inf")
if 1 < n:
for i in range(n - k + 1):
if x[i + k - 1] < 0:
answer = min(answer, -x[i])
elif 0 < x[i]:
answer = min(answer... | def main():
n, k = list(map(int, input().split()))
x = [int(i) for i in input().split()]
x.sort()
answer = float("inf")
if n == k:
if n == 1:
answer = abs(x[0])
if 0 <= x[0]:
answer = x[-1]
elif x[-1] <= 0:
answer = abs(x[0])
... | p03274 |
N,K = list(map(int,input().split()))
ary = [0]+list(map(int,input().split()))
dp = [[0]*(N+1) for _ in range(N+1)]
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N+1):
for j in range(N+1):
dp[i][j] = abs(ary[i]-ary[j])
for i in range(N+1):
if i == a:continue
... | N, K = list(map(int, input().split()))
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
if i == a: continue
k = abs(i - a)
d = abs(ary[i])
if k >= K and ans > d:
ans = d
if i < a:
k = K - abs(i - a)... | p03274 |
N, K = list(map(int, input().split()))
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
b = N - i - 1
d = abs(ary[i])
if a[i] >= K:
ans = min(ans, d)
ans = ... | N, K = list(map(int, input().split()))
ary = list(map(int, input().split()))
ary2 = sorted(ary+[0])
zero = ary2.index(0)
k = [abs(i - zero) if i < zero else ((i + 1) - zero)for i in range(N)]
ans = float("Inf")
for i in range(N - K + 1):
if k[i] <= K:
if ans > abs(ary[i]) + abs(ary[i] - ary[i + (K -... | p03274 |
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
tmp = [0] * k
ans = float("inf")
for i in range(n-k+1):
tmp = a[i:i+k]
if (tmp[0] <= 0 and tmp[-1] <= 0) or (tmp[0] >= 0 and tmp[-1] >= 0):
cost = max(abs(tmp[0]), abs(tmp[-1]))
else:
cost = min(abs(tmp[0]), abs(tmp[-1])... | n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = float("inf")
for i in range(n-k+1):
#tmpの左端と右端の符号が同じなら、端から移動していく
if (a[i] <= 0 and a[i+k-1] <= 0) or (a[i] >= 0 and a[i+k-1] >= 0):
cost = max(abs(a[i]), abs(a[i+k-1]))
#選んだろうそくが正負にまたがる場合、左端と右端の近いほうに移動してから
else:
... | p03274 |
import sys, os, bisect
f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
sys.stdin = open('./input.txt', 'r')
def solve():
n , k = f()
a = f()
ans = int(1e10)
for i in range(n):
if i+k-1<n:
cur_ans = 0
if a[i]* a[i+k-1]>=0:
... | import sys, os, bisect
f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
sys.stdin = open('./input.txt', 'r')
def solve():
n , k = f()
a = f()
ans = int(1e10)
for i in range(n):
if i+k-1<n:
ans = min(ans, abs(a[i]) + abs(a[i] - a[i+k-1]), abs(a... | p03274 |
def solve():
N, K = [int(i) for i in input().split()]
xlist = list(map(int, input().split()))
cost = float("inf")
for i in range(N-K+1):
j = i+K-1
cost = min(
abs(xlist[i])+abs(xlist[j]-xlist[i]),
abs(xlist[j])+abs(xlist[i]-xlist[j]),
co... | def solve():
N, K = [int(i) for i in input().split()]
xlist = [int(i) for i in input().split()]
cost = float("inf")
for i in range(N-K+1):
j = i+K-1
cost = min(
cost,
abs(xlist[i])+abs(xlist[j]-xlist[i]),
abs(xlist[j])+abs(xlist[i]-xlist... | p03274 |
N,k = list(map(int,input().split()))
a = list(map(int,input().split()))
i = 0
while i < N:
if a[i] >= 0:
break
i += 1
x = []
y = []
for j in range(i-k,i+1):
if j>=0 and j+k-1<N:
x = a[j:j+k]
if x[0] < 0 and x[k-1] > 0:
y.append(min(-x[0],x[k-1])*2 + max(-x[0]... | N, K = list(map(int,input().split()))
x = list(map(int,input().split()))
if 0 in x:
K -= 1
cand = []
hasleft = x[0] < 0
hasright = x[N-1] > 0
if K == 0:
cand = [0]
else:
for i in range(N-K+1):
left = x[i]
right = x[i+K-1]
if left < 0 and right > 0:
cand.... | p03274 |
# ABC107C - Candles (ARC101C)
def main():
n, k = tuple(map(int, input().rstrip().split()))
x = tuple(map(int, input().rstrip().split()))
ans = float("inf")
for i in range(n - k + 1):
a, b = x[i], x[i + k - 1]
if a < b < 0 or 0 < a < b:
ans = min(ans, max(abs(a), abs(b... | # ABC107C - Candles (ARC101C)
def main():
_, k = list(map(int, input().split()))
x = tuple(map(int, input().split()))
print((min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :]))))
if __name__ == "__main__":
main() | p03274 |
# ABC107C - Candles (ARC101C)
def main():
N, K, *X = list(map(int, open(0).read().split()))
ans = min(r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :]))
print(ans)
if __name__ == "__main__":
main() | # ABC107C - Candles (ARC101C)
def main():
N, K, *X = list(map(int, open(0).read().split()))
cand = [r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :])]
ans = min(cand)
print(ans)
if __name__ == "__main__":
main()
| p03274 |
n, k = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
INF = 10 ** 9
def sign(x):
return int(x >= 0)
ans = INF
for i in range(n - k + 1):
p = k + i - 1
if sign(x[i]) == sign(x[p]):
t = max(abs(x[i]), abs(x[p]))
else:
t = min(-x[i] * 2 + x[p], -x[i] ... | n, k = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
INF = 10 ** 9
ans = INF
for li in range(n - k + 1):
ri = k + li - 1
t = min(abs(x[li]) + abs(x[ri] - x[li]), abs(x[ri]) + abs(x[li] - x[ri]))
ans = min(t, ans)
print(ans) | p03274 |
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
temp = a[i:i+k]
l = temp[0]
r = temp[-1]
dist_l = abs(l) + abs(l-r)
dist_r = abs(r) + abs(l-r)
dist = min(dist_l, dist_r)
b.append(dist)
print((min(b))) | n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
dist = a[i+k-1] - a[i] + min(abs(a[i]), abs(a[i+k-1]))
b.append(dist)
print((min(b))) | p03274 |
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
dist = a[i+k-1] - a[i] + min(abs(a[i]), abs(a[i+k-1]))
b.append(dist)
print((min(b))) | n, k = list(map(int, input().split()))
x = [int(i) for i in input().split()]
d = []
for i in range(n-k+1):
l, r = x[i], x[i+k-1]
d.append(abs(l-r) + min(abs(l), abs(r)))
print((min(d))) | p03274 |
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
ans=10**9
for i in range(n-k+1):
d=x[i+k-1]-x[i]
d+=min(abs(x[i]),abs(x[i+k-1]))
ans=min(ans,d)
print(ans) | n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
ans=10**9
for i in range(n-k+1):
x_max=x[i+k-1]
x_min=x[i]
t_ans=x_max-x_min
t_ans+=min(abs(x_max),abs(x_min))
ans=min(ans,t_ans)
print(ans) | p03274 |
import sys
input = sys.stdin.readline
def main():
N, K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
plus = [0]
j = 0
for i in range(N):
if x[i] >= 0:
plus.append(x[i])
j += 1
j = 0
minus = [0]
for i in rang... | import sys
input = sys.stdin.readline
def main():
N, K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
ans = float("inf")
for i in range(N + 1 - K):
l = x[i]
r = x[i + K - 1]
if l < 0 < r:
ans = min(ans, abs(l) + abs(r) + min(... | p03274 |
n, k = list(map(int,input().split()))
x = list(map(int,input().split()))
time = 10**9
for i in range(n - k + 1):
x_k = x[i:i+k]
#print(x_k)
if x_k[0] >= 0:
t = x_k[-1]
elif x_k[-1] <= 0:
t = -x_k[0]
else:
t = min(-x_k[0] + x_k[-1] *2,-x_... | n, k = list(map(int,input().split()))
x = list(map(int,input().split()))
time = 10**9
x_k = [0, 0]
for i in range(n - k + 1):
x_k[0] = x[i]
x_k[1] = x[i+k-1]
#print(x_k)
if x_k[0] >= 0:
t = x_k[1]
elif x_k[1] <= 0:
t = -x_k[0]
else:
... | p03274 |
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
tl = l[i:i + k]
min_l = min(0, tl[0])
max_l = max(0, tl[-1])
min_abs = min(abs(min_l), abs(max_l))
max_abs = max(abs(min_l), abs(max_l))
ans = min(ans, min_abs ... | n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
min_l = min(0, l[i])
max_l = max(0, l[i+k-1])
min_abs = min(abs(min_l), abs(max_l))
max_abs = max(abs(min_l), abs(max_l))
ans = min(ans, min_abs * 2 + max_abs)
prin... | p03274 |
def main():
n, k, *x = list(map(int, open(0).read().split()))
f = lambda i: x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i]))
l = f(0)
for i in range(n - k + 1):
y = f(i)
if y < l:
l = y
print(l)
if __name__ == '__main__':
main()
| def main():
n, k, *x = list(map(int, open(0).read().split()))
l = x[0 + k - 1] - x[0] + min(abs(x[0 + k - 1]), abs(x[0]))
for i in range(n - k + 1):
y = x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i]))
if y < l:
l = y
print(l)
if __name__ == '__main__':... | p03274 |
n,k,*x=list(map(int,open(0).read().split()))
if x[0]>=0:
ans=x[k-1]
elif x[-1]<=0:
ans=abs(x[~(k-1)])
elif n==k:
l=abs(x[0])
r=abs(x[-1])
ans=min(l+l+r,r+r+l)
else:
ans=10**10
for i in range(n-k+1):
l=x[i]
r=x[i+k-1]
if l<=0<=r:
ll=abs(l)
rr=abs(r)
tmp=mi... | n,k,*x=list(map(int,open(0).read().split()))
if x[0]>=0:
ans=x[k-1]
elif x[-1]<=0:
ans=abs(x[~(k-1)])
else:
ans=10**10
for i in range(n-k+1):
l=x[i]
r=x[i+k-1]
if l<=0<=r:
ll=abs(l)
rr=abs(r)
tmp=min(ll+2*rr,2*ll+rr)
ans=min(ans,tmp)
print(ans)
| p03274 |
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
dp = [float("inf")] * (K + 2)
s = bl(x, 0)
l = 0
r = K - 1
if r + 1 < s:
l = s - 1 - r
r = s - 1
def calc(l, r):
if x[l] * x[r] < 0:
return min(ab... | import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
if a[i] * a[i + K - 1] < 0:
mn = min(abs(a[i]), abs(a[i + K - 1]))
mx = max(abs(a[i]), abs(a[i + K - 1]))
res = min(res, mn * 2 + mx)
e... | p03274 |
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
if a[i] * a[i + K - 1] < 0:
mn = min(abs(a[i]), abs(a[i + K - 1]))
mx = max(abs(a[i]), abs(a[i + K - 1]))
res = min(res, mn * 2 + mx)
e... | import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
z = -1
res = float("inf")
for r in range(N):
if a[r] < 0:
z = r
continue
if r < K - 1: continue
x = r - z
y = a[z - (K - x) + 1]
if K == x: y = 0
t = min(abs(a[r]), abs(y))
... | p03274 |
n,k=list(map(int,input().split()))
candles=list(map(int,input().split()))
rows=[]
minimum=[]
for i in range(n-k+1):
new=candles[i:i+k]
l_r=abs(new[0])+abs(new[-1]-new[0])
r_l=abs(new[-1])+abs(new[-1]-new[0])
minimum.append(min(l_r,r_l))
print((min(minimum))) | N, K, *x = list(map(int, open(0).read().split()))
ans = 1e9
for i in range(N - K + 1):
l = x[i]
r = x[i + K - 1]
ans = min(ans, abs(l) + (r - l), abs(r) + (r - l))
print(ans) | p03274 |
###############################################################################
from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
mod = 1
def intin():
input_tuple = input().split()
if len(input_tuple) <= 1:
return int(input_tuple[0])
ret... | ###############################################################################
from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
mod = 1
def intin():
input_tuple = input().split()
if len(input_tuple) <= 1:
return int(input_tuple[0])
ret... | p03274 |
import sys
input = sys.stdin.readline
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ans = 10**9
for i in range(N-K+1):
#print(X[i:i+K])
if min(X[i:i+K]) >= 0:
#print('a')
ans = min(ans,max(X[i:i+K]))
elif max(X[i:i+K]) <= 0:
#print('b')
a... | N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ans = 10**9
for i in range(N-K+1):
ans = min(ans,X[i+K-1]-X[i]+min(abs(X[i]),abs(X[i+K-1])))
print(ans) | p03274 |
n,k = list(map(int,input().split()))
x = [int(x) for x in input().split()]
mini = 10**9
for i in range(n-k+1):
ls = x[i:i+k]
flg1 = abs(ls[0])+abs(ls[0]-ls[k-1])
flg2 = abs(ls[k-1])+abs(ls[0]-ls[k-1])
flg = min(flg1,flg2)
mini = min(flg,mini)
print(mini) | n,k = list(map(int,input().split()))
ls = [int(x) for x in input().split()]
mini = 10**9
for i in range(n-k+1):
flg1 = abs(ls[i])+abs(ls[i]-ls[i+k-1])
flg2 = abs(ls[i+k-1])+abs(ls[i]-ls[i+k-1])
mini = min(flg1,flg2,mini)
print(mini) | p03274 |
def solve():
n, k = list(map(int, input().split()))
xs = list(map(int, input().split()))
min_dist = 10 ** 9
for i in range(n - k + 1):
part = xs[i:i + k]
length = abs(part[0] - part[-1])
dist = min(abs(part[0]) + length, abs(part[-1]) + length)
if dist < min_dist:... | def solve():
n, k = list(map(int, input().split()))
xs = list(map(int, input().split()))
min_dist = 10 ** 9
for i in range(n - k + 1):
length = abs(xs[i] - xs[i + k - 1])
dist = min(abs(xs[i]) + length, abs(xs[i + k - 1]) + length)
if dist < min_dist:
min_dist... | p03274 |
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()
def main():
n, k = na()
xs = na()
m = float('inf')
for i in range(n-k+1):
... | import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()
def main():
n, k = na()
xs = na()
m = float('inf')
for i in range(n-k+1):
... | p03274 |
n,k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = float("inf")
for i in range(n-k+1):
# 0の位置から移動し、i〜i+kまでのろうそくに火をつけた場合の移動時間について左右で確認し、最小を求める
l = abs(x[i]) + abs(x[i]-x[i+k-1])
r = abs(x[i+k-1]) + abs(x[i] - x[i+k-1])
ans = min(ans, l, r)
print(ans) | n,k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = float("inf")
for i in range(n-k+1):
l = abs(x[i]) + abs(x[i]-x[i+k-1])
r = abs(x[n-1-i]) + (x[n-1-i]-x[n-k-i])
ans = min(ans,l,r)
print(ans) | p03274 |
# -*- coding: utf-8 -*-
N, K = list(map(int, input().split()))
xi = list(map(int, input().split()))
kouho = []
ans_list = []
for i in range(N-K+1):
li = xi[i:i+K]
if li[0]*li[-1] < 0:
a = abs(li[0])*2 + abs(li[-1])
b = abs(li[-1])*2 + abs(li[0])
if a > b :
ans_... | # -*- coding: utf-8 -*-
N, K = list(map(int, input().split()))
xi = list(map(int, input().split()))
ans_list = []
for i in range(N-K+1):
l = xi[i]
r = xi[i+K-1]
if l*r < 0:
a = abs(l)*2 + abs(r)
b = abs(r)*2 + abs(l)
if a > b :
ans_list.append(b)
e... | p03274 |
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
if N == 1:
print((abs(X[0])))
exit()
flg = False
for x in X:
if x < 0:
flg = True
if flg is False:
print((X[K - 1]))
exit()
flag2 = False
for x in X:
if x > 0:
flag2 = True
... | N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
lft, rgt = 0, K - 1
ans = float('inf')
while True:
if rgt == N:
break
ans = min(ans, min(abs(X[lft]) + abs(X[lft] - X[rgt]),
abs(X[rgt]) + abs(X[rgt] - X[lft])))
lft += 1
rgt += 1
pri... | p03274 |
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
assert len(x) == N
m = min(x[i] - x[i-K+1] + min(abs(x[i]), abs(x[i-K+1])) for i in range(K-1, N))
print(m) | N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
assert len(x) == N
m = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))
print(m) | p03274 |
N, K = list(map(int, input().split()))
cans = list(map(int, input().split()))
d = []
for n in range(N-K+1):
l = abs(cans[n]) + abs(cans[n + (K-1)] - cans[n])
r = abs(cans[n + (K-1)]) + abs(cans[n + (K-1)] - cans[n])
d.append(min(l,r))
print((min(d))) | N, K = list(map(int, input().split()))
cans = list(map(int, input().split()))
print((min([min(abs(cans[n]), abs(cans[n + (K-1)])) + abs(cans[n + (K-1)] - cans[n]) for n in range(N-K+1)]))) | p03274 |
n, k, *a = list(map(int, open(0).read().split()))
ans = 1e11
for i in range(n-k+1):
ans = min(ans, a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i])))
print(ans) | n,k,*a=list(map(int,open(0).read().split()));print((min([a[i+k-1]-a[i]+min(abs(a[i+k-1]),abs(a[i]))for i in range(n-k+1)]))) | p03274 |
import sys
def input(): return sys.stdin.readline().rstrip()
def solve(l, m):
if l < m:
return l * 2 + m
else:
return l + m * 2
def main():
N, K = list(map(int, input().split()))
x = tuple(map(int, input().split()))
right_x = [i for i in x if i >= 0][:K]
left_x = [... | import sys
def input(): return sys.stdin.readline().rstrip()
def solve(l, r):
lv = abs(l)
rv = abs(r)
if (l < 0) ^ (r < 0):
return lv + rv + min(lv, rv)
else:
return max(lv, rv)
def main():
N, K = list(map(int, input().split()))
x = tuple(map(int, input().split()))... | p03274 |
import sys
n, k, *x = list(map(int, sys.stdin.read().split()))
def main():
l = 0; r = k - 1
ans = float('inf')
while r < n:
if x[r] <= 0:
res = abs(x[l])
elif x[l] < 0:
res = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
else:
res ... | import sys
n, k, *x = list(map(int, sys.stdin.read().split()))
def main():
res = float('inf')
for i in range(n - k + 1):
j = i + k - 1
if x[i] < 0:
d = -x[i] if x[j] <= 0 else min(-x[i], x[j]) * 2 + max(-x[i], x[j])
else:
d = x[j]
res = min(r... | p03274 |
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
cost = []
for i in range(0, N-K+1):
window = X[i:i+K]
cost.append(window[-1]-window[0] + min(abs(window[-1]), abs(window[0])))
print((min(cost))) | N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
cost = []
for i in range(0, N-K+1):
#window = X[i:i+K]
#cost.append(window[-1]-window[0] + min(abs(window[-1]), abs(window[0])))
cost.append(X[i+K-1]-X[i] + min(abs(X[i+K-1]), abs(X[i])))
print((min(cost))) | p03274 |
import sys
import math
import collections
import itertools
import array
import inspect
# Set max recursion limit
sys.setrecursionlimit(1000000)
# Debug output
def chkprint(*args):
names = {
id(v): k
for k, v in list(inspect.currentframe().f_back.f_locals.items())
}
print(... | import sys
import math
import collections
import itertools
import array
import inspect
# Set max recursion limit
sys.setrecursionlimit(1000000)
# Debug output
def chkprint(*args):
names = {
id(v): k
for k, v in list(inspect.currentframe().f_back.f_locals.items())
}
print(... | p03274 |
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
nowx=x[i:i+k]
if nowx[0]>=0 and nowx[-1]>=0:
ans=min(ans, nowx[-1])
elif nowx[0]<=0 and nowx[-1]<=0:
ans=min(ans, abs(nowx[0]))
elif nowx[0]<0 and nowx[-1]>=0:
... | n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
nowx=(x[i],x[i+k-1])
if nowx[0]>=0 and nowx[-1]>=0:
ans=min(ans, nowx[-1])
elif nowx[0]<=0 and nowx[-1]<=0:
ans=min(ans, abs(nowx[0]))
elif nowx[0]<0 and nowx[-1]>=0:
... | p03274 |
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
for i in range(n-k+1):
xtmp = x[i:i+k]
if i==0:
MIN = abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0])
MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[0]))
MIN = min(MIN, abs(xtmp[0]-xtmp[-1]) + abs(xtmp[-1]))
print(MIN... | # -*- coding: utf-8 -*-
n, k = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = 10**10
for i in range(n-k+1):
left = X[i]
right = X[i+k-1]
ans = min(ans, abs(left)+abs(left-right), abs(right)+abs(right-left))
print(ans)
| p03274 |
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
arr = []
for i in range(n-k+1):
arr.append(min((abs(x[i])+abs(x[i]-x[i+k-1])), abs(x[i+k-1])+abs(x[i]-x[i+k-1])))
print((min(arr)))
| import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()
N, K = i_l()
x = i_l()
ans = 1 << 31
for i in range(N-K+1):
if x[i] >= 0:
ans = min(ans, x[i+K-1])
elif x[... | p03274 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digi... | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digi... | p03274 |
import bisect
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
ans, tmp = 10**9, 10**9
#print(bisect.bisect(x, 0))
for i in range(len(x) - K + 1):
if x[i] <= 0 and x[i+K-1] >= 0:
tmp = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
elif x[i] < 0 and x[i+K-1] <= 0:
t... | N, K = list(map(int, input().split()))
x = [int(i) for i in input().split()]
ans = 10**9
for l in range(N-K+1):
r = l + K - 1
tmp = x[r]-x[l]
if x[r] < 0:
tmp += abs(x[r])
elif x[l] < 0 and 0 <= x[r]:
tmp += min(abs(x[l]), x[r])
else:
tmp += x[l]
ans = min(ans, tmp)
print(ans) | p03274 |
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
answer = []
for i in range(N):
if i + K > N:
break
Y = X[i:i+K]
if Y[0] > 0:
answer.append(Y[-1])
elif Y[-1] < 0:
answer.append(-Y[0])
else:
answer.append(min(Y[0]*(-2)+Y[-1], Y[-1]*2-Y[0]))
print((min(... | N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
answer = []
for i in range(N):
if i + K > N:
break
Ys, Yt = X[i], X[i+K-1]
if Ys > 0:
answer.append(Yt)
elif Yt < 0:
answer.append(-Ys)
else:
answer.append(min(Ys*(-2)+Yt, Yt*2-Ys))
print((min(answer))) | p03274 |
def solve(N, K, Xs):
plus = sorted([x for x in Xs if x >= 0])
minus = sorted([abs(x) for x in Xs if x < 0])
ans = 10 ** 9
for i in range(0, max(K, len(plus)) + 1):
ps = plus[:i]
ms = minus[:K - i]
if len(ps) == K:
ans = min(ans, ps[-1])
continue
... | def solve(N, K, Xs):
plus = sorted([x for x in Xs if x >= 0])
minus = sorted([abs(x) for x in Xs if x < 0])
ans = 10 ** 9
for i in range(0, K + 1):
j = K - i
if i <= len(plus) and i == K:
p = plus[i-1]
ans = min(ans, p)
elif j <= len(minus) and j ... | p03274 |
N,K = list(map(int, input().split()))
X = []
idx = 0
for i,x in enumerate(input().split()):
x = int(x)
X.append(x)
if x < 0:
idx = i + 1
if idx == N:
idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
tmp = 0
if i <= idx <= i+K-1 or i >= idx or i+K-1 <= ... | N,K = list(map(int, input().split()))
X = []
idx = 0
for i,x in enumerate(input().split()):
x = int(x)
X.append(x)
if x < 0:
idx = i + 1
if idx == N:
idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
ans = min(ans, X[i+K-1] - X[i] + min(abs(X[i]), abs(X[i+K-1]... | p03274 |
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
first = True
for l, r in zip(list(range(0, n - k + 1)), list(range(k - 1, n))):
if 0 >= x[r]:
if first:
min_time = -x[l]
first = False
else:
time =-x[l]
if time < min... | n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
min_time = 10 ** 10
for l, r in zip(list(range(0, n - k + 1)), list(range(k - 1, n))):
time = x[r] if x[l] >= 0 else (-x[l] if x[r] <= 0 else (x[r] - x[l]) + min(-x[l], x[r]))
if time < min_time:
min_time = time
print(min_... | p03274 |
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**18
for i in range(N-K+1):
y = x[i:i+K]
y0 = x[i]
yk = x[i+K-1]
if yk < 0:
ans = min(ans, abs(y0))
elif y0 <= 0 and 0 <= yk:
ans = min(ans, yk-2*y0, 2*yk-y0)
else:
ans = min(... | N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**18
for i in range(N-K+1):
y0 = x[i]
yk = x[i+K-1]
if yk < 0:
ans = min(ans, abs(y0))
elif y0 <= 0 and 0 <= yk:
ans = min(ans, yk-2*y0, 2*yk-y0)
else:
ans = min(ans, yk)
print(... | p03274 |
def main():
N, K = (int(i)for i in input().split())
x = [int(i) for i in input().split()]
ans = 3 * (10**8)
st_val = min(x,key=abs)
st_idx = x.index(st_val)
for i in range(K+1):
dist = 3 * (10**8)
if 0 <= st_idx-K+1+i and st_idx+1+i <= N:
if (x[st_idx-K... | def main():
N, K = (int(i)for i in input().split())
x = [int(i) for i in input().split()]
ans = 3 * (10**8)
for i in range(N-K+1):
dist = min(abs(x[i]),abs(x[i+K-1])) + abs(x[i+K-1] - x[i])
ans = min(ans,dist)
print(ans)
if __name__ == "__main__":
main() | p03274 |
from itertools import combinations
n, k = list(map(int, input().split()))
x = [int(i) for i in input().split()]
c = combinations(x, k)
# print(list(c))
ans = 10 ** 10
for g in c:
a = max(0, max(g))
b = min(0, min(g))
# print(a, b)
if (a * b) < 0:
tmp = min(2 * abs(a) + abs(b... | n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
# c = combinations(x, k)
c = []
for i in range(n - k + 1):
l = x[i]
r = x[i + k - 1]
c.append([l, r])
# print(list(c))
ans = 10 ** 10
for l, r in c:
tmp = min(abs(l) + abs(r - l), abs(r) + abs(r - l))
an... | p03274 |
N,K = list(map(int,input().split()))
X_all = list(map(int,input().split()))
m = float("inf")
for i in range(N-K+1):
X = X_all[i:i+K]
if X[0] > 0:
d = X[-1]
elif X[-1] < 0:
d = -X[0]
else:
d = X[-1]-X[0]+min(-X[0],X[-1])
m = min(m,d)
print(m) | N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
m = 2*(abs(X[-1])+abs(X[0]))
for i in range(N-K+1):
if X[i] > 0:
d = X[i+K-1]
elif X[i+K-1] < 0:
d = -X[i]
else:
d = X[i+K-1]-X[i]+min(-X[i],X[i+K-1])
m = min(m,d)
print(m) | p03274 |
import bisect
N,K=list(map(int,input().split()))
X=list(map(int,input().split()))
if 0 in X:
K=K-1
ans=[]
Right=[]
Left=[]
for x in X:
if x>0:
Right.append(x)
else:
Left=[abs(x)]+Left
Right=[0]+Right
Left=[0]+Left
for i in range(K+1):
if i<=len(Right)-1 and K-i<=len(Left)... | N,K=list(map(int,input().split()))
X=list(map(int,input().split()))
ans=[]
for i in range(N-K+1):
xl=X[i]
xr=X[i+K-1]
ans.append(min(abs(xl)+abs(xr-xl),abs(xr)+abs(xl-xr)))
print((min(ans))) | p03274 |
import sys
input = sys.stdin.readline
def func(a):
global alist
cnt = 1
for i in range(len(alist)):
if abs(alist[i][0]) > abs(int(a)):
alist[i][1] += 1
else:cnt += 1
alist.append([int(a),cnt])
return int(a)
alist = []
n,k = list(map(int,input().split()... | n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = float('inf')
for i in range(n-k+1):
left = i
right = i+k-1
ans = min(ans, min(abs(x[left])+abs(x[right]-x[left]),abs(x[right])+abs(x[left]-x[right])))
print(ans)
| p03274 |
import sys
input = sys.stdin.readline
def readstr():
return input().strip()
def readint():
return int(eval(input()))
def readnums():
return list(map(int, input().split()))
def readstrs():
return input().split()
def main():
N, K = readnums()
x = list(readnums())
... | import sys
input = sys.stdin.readline
def readstr():
return input().strip()
def readint():
return int(eval(input()))
def readnums():
return list(map(int, input().split()))
def readstrs():
return input().split()
def main():
N, K = readnums()
x = list(readnums())
... | p03274 |
import itertools
import collections
n,k=list(map(int,input().split()))
candle=list(map(int,input().split()))
com=list(itertools.product(["1","0"], repeat=n))
ans=200000000
for i in range(2**n):
count=collections.Counter(com[i])
if count["1"]==k:
comtemp="".join(com[i])
start=candle[comtemp.find("1... | n,k=list(map(int,input().split()))
candle=list(map(int,input().split()))
ans=2000000000
for i in range(n-k+1):
if candle[i]<0 and candle[i+k-1]>0:
temp=min(abs(candle[i]), candle[i+k-1])*2+max(abs(candle[i]), candle[i+k-1])
elif candle[i+k-1]<=0:
temp=abs(candle[i])
elif candle[i]>=0:
temp=ca... | p03274 |
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = pow(10, 10)
for i in range(N-K+1):
temp_l = abs(x[i]) + abs(x[i]-x[i+K-1])
temp_r = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
ans = min(ans, temp_l, temp_r)
print(ans)
| N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K + 1):
l = a[i]
r = a[i+K-1]
ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans) | p03274 |
n, k = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float('inf')
for i in range(n-k+1):
l, r = X[i:i+k][0], X[i:i+k][-1]
if r < 0:
ans = min(abs(l), ans)
elif 0 < l:
ans = min(abs(r), ans)
else:
ans = min(min(abs(l), abs(r))*2+max(abs(l), ... | n, k = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float('inf')
for i in range(n-k+1):
l, r = X[i], X[i+k-1]
if r <= 0:
ans = min(abs(l), ans)
elif 0 <= l:
ans = min(abs(r), ans)
else:
dist = min(abs(l), abs(r))*2+max(abs(l), abs(r))
... | p03274 |
N,K=list(map(int,input().split()))
*X,=list(map(int,input().split()))
A=[]
B=[]
for x in X:
if x>=0:
A.append(x)
else:
B.append(abs(x))
B.sort()
i=min(K,len(A))
j=K-i
ans=[]
while i>=0 and j<=len(B):
if i==0:
ans.append(B[j-1])
elif j==0:
ans.append(A[i-1... | N,K=list(map(int,input().split()))
*X,=list(map(int,input().split()))
i=0
ans=[]
while i+K-1<N:
a,b=X[i],X[i+K-1]
if b<0:
ans.append(abs(a))
elif a>0:
ans.append(b)
else:
a=abs(a)
ans.append(a+b+min(a,b))
i+=1
print((min(ans))) | p03274 |
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float('inf')
for left in range(N - K + 1) :
if X[left] < 0 and X[left + K - 1] >= 0 :
l = -X[left] * 2 + X[left + K - 1]
r = -X[left] + X[left + K - 1] * 2
ans = min(ans, l, r)
elif X[left + K - ... | N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = 10**18
for l in range(N - K + 1):
D = X[l + K - 1] - X[l]
if X[l] <= 0 <= X[l + K - 1]:
D += min(-X[l], X[l + K - 1])
elif X[l + K - 1] <= 0:
D += -X[l + K - 1]
elif X[l] >= 0:
D += X[l]
... | p03274 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.