Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

s = list(input())[::-1]
word = ["dream", "dreamer", "erase", "eraser"]
while True:
    if "".join(s[:5][::-1]) == "dream" or "".join(s[:5][::-1]) == "erase":
        s = s[5:]
        continue
    elif "".join(s[:6][::-1]) == "eraser":
        s = s[6:]
        continue
    elif "".join(s[:7][::-1]) == "dreamer":
        s = s[7:]
        continue
    break
if len(s) == 0:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

s = input()[::-1]
word = ["dream", "dreamer", "erase", "eraser"]
dream = "dream"[::-1]
dreamer = "dreamer"[::-1]
erase = "erase"[::-1]
eraser = "eraser"[::-1]
while True:
    if s[:5] == dream or s[:5] == erase:
        s = s[5:]
        continue
    elif s[:6] == eraser:
        s = s[6:]
        continue
    elif s[:7] == dreamer:
        s = s[7:]
        continue
    break
if len(s) == 0:
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : map(int, stdin.readline().split())

def divi(n):
  res = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      res.append(i)
      if i != n // i:
        res.append(n // i)
  return res

def isprime(n):
  for i in range(2, n+1):
    if i*i <= n:
      if (n % i == 0):
        return False
  return True

def main():
  a, b = sorted(na())
  x = divi(a)
  y = []
  for bb in divi(b):
    if bb in x:
      y.append(bb)
  cnt = 0
  for i, yy in enumerate(y):
    if not isprime(yy):
      continue
    cnt += 1
  print(cnt)
main()

### FIXED CODE ###

import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : map(int, stdin.readline().split())

def divi(n):
  res = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      res.append(i)
      if i != n // i:
        res.append(n // i)
  return res

def isprime(n):
  if n == 2:
    return True
  if n % 2 == 0 or n < 2:
    return False
  for i in range(3, int(n**0.5)+1, 2):
    if (n % i == 0):
      return False
  return True

def main():
  a, b = sorted(na())
  x = divi(a)
  cnt = 1
  for bb in divi(b):
    if bb in x and isprime(bb):
      cnt += 1
  print(cnt)
main()
### BUGGY CODE ###

def init():
    global N, Sp, Sm, total
    N = int(input())
    Sp = []
    Sm = []
    total = 0
    for i in range(N):
      l = input()
      u = 0
      h = 0
      for j in range(len(l)):
        if l[j] == '(':
          h += 1
        else:
          h -= 1
        if h < u:
          u = h
      if h >= 0:
        Sp.append([u, h])
      else:
        Sm.append([u-h, -h])
      total += h
    Sp.sort(reverse=True)
    Sm.sort(reverse=True)

def main():
  init()
  if total != 0:
    print('No')
  else:
    p = check(Sp)
    m = check(Sm)
    if p and m:
      print('Yes')
    else:
      print('No')

def check(S):
  h = 0
  for i in range(len(S)):
    if h + S[i][0] < 0:
      return False
    else:
      h += S[i][1]
  return True

if __name__ == '__main__':
  main()

### FIXED CODE ###

import sys
readline = sys.stdin.readline

def main():
  N = int(readline())
  Sp = []
  Sm = []
  total = 0
  bm = 0
  for i in range(N):
    l = readline().strip()
    b = 0
    h = 0
    for j in range(len(l)):
      if l[j] == '(':
        h += 1
      else:
        h -= 1
      b = min(b, h)
    if h == 0 and b != 0:
      bm = min(bm, b)
    elif h > 0:
      Sp.append([b, h])
    elif h < 0:
      Sm.append([b-h, -h])
    total += h
  Sp.append([bm, 0])
  if total != 0:
    print('No')
    exit()
  Sp.sort(key=lambda x:x[0], reverse=True)
  p = check(Sp)
  if p < 0:
    print('No')
    exit()
  Sm.sort(key=lambda x:x[0], reverse=True)
  m = check(Sm)
  if m < 0:
    print('No')
    exit()
  print('Yes')

def check(S):
  h = 0
  for i in range(len(S)):
    if h + S[i][0] < 0:
      return -1
    else:
      h += S[i][1]
  return h

if __name__ == '__main__':
  main()
### BUGGY CODE ###

import sys

input=sys.stdin.readline

sys.setrecursionlimit(100000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)
mod=10**9+7

parent={i:0 for i in range(1,N+1)}

q=set([1])
sub=set([])
while q:
    while q:
        x=q.pop()
        for p in edge[x]:
            if p!=1 and parent[p]==0:
                parent[p]=x
                sub.add(p)

        if not q:
            q=sub
            sub=set([])
            break


def dfs(num):
    if len(edge[num])==1 and num!=1:
        return (1,1)

    WHITE=1
    BLACK=1
    for i in edge[num]:
        if i!=parent[num]:
            s,t=dfs(i)
            WHITE=(WHITE*(s+t))%mod
            BLACK=(BLACK*s)%mod

    return (WHITE,BLACK)

w,b=dfs(1)
print((w+b)%mod)

### FIXED CODE ###

import sys

input=sys.stdin.readline

sys.setrecursionlimit(100000)

N=int(input())
edge={i:[] for i in range(1,N+1)}
for i in range(0,N-1):
    a,b=map(int,input().split())
    edge[a].append(b)
    edge[b].append(a)
mod=10**9+7

parent={i:0 for i in range(1,N+1)}

q=set([1])
sub=set([])
while q:
    while q:
        x=q.pop()
        for p in edge[x]:
            if p!=1 and parent[p]==0:
                parent[p]=x
                sub.add(p)

        if not q:
            q=sub
            sub=set([])
            break


def dfs(num):
    if len(edge[num])==1 and num!=1:
        return (1,1)

    WHITE=1
    BLACK=1
    for i in edge[num]:
        if i!=parent[num]:
            s,t=dfs(i)
            WHITE=(WHITE*(s+t))%mod
            BLACK=(BLACK*s)%mod

    return (WHITE,BLACK)

w,b=dfs(1)
print((w+b)%mod)
### BUGGY CODE ###

S=input()
cnt=0

while True:
    if S.find('01')!=-1:
        S=S[0:S.find('01')]+S[S.find('01')+2:]
        cnt+=2
    elif S.find('10')!=-1:
        S = S[0:S.find('10')] + S[S.find('10') + 2:]
        cnt+=2
    else:
        break

print(cnt)





### FIXED CODE ###

S=input()
print(2*(min(S.count('1'),S.count('0'))))
### BUGGY CODE ###

def main():
    n = int(input())
    a = list(map(int, input().split()))
    count = 0
    for i in range(n):
        for j in range(i+1,n,1):
            if a[i]+a[j]==abs(i-j): count+=1
    print(count)

main()


### FIXED CODE ###

from collections import Counter

def main():
    # n:人数, a:身長リスト
    n = int(input())
    a = list(map(int, input().split()))
    # Li=i+Ai, Ri=i-Aiを満たすL,Rを作成
    L = [0]*n
    R = [0]*n
    for i,ai in enumerate(a):
        L[i] = i+ai
        R[i] = i-ai
    # Li=X=Rj,i<jを満たす組が存在する時count+=1
    count = 0
    c = Counter(R)
    for Li in L:
        count += c.get(Li,0)

    print(count)

main()
### BUGGY CODE ###

n = int(input())
a = [input() for i in range(n)]
#
from collections import Counter
b = Counter(a)
c = list(b.keys())
d = list(b.values())
#
e = []
for j in range(len(d)):
    if int(d[j]) == max(d):
        e.append(j)
#
#[print(c[e[k]]) for k in range(len(e))]
f =[]
for k in range(len(e)):
    f.append(c[e[k]])
f = sorted(f)
[print(f[l]) for l in range(len(f))]


### FIXED CODE ###

import collections
n = int(input())
s = []
for _ in range(n):
    s.append(input())
ss = collections.Counter(s)
s_most = ss.most_common()

ans = []
#
for i in range(len(s_most)):
    if s_most[i][1] == s_most[0][1]:
        ans.append(s_most[i][0])
    else:
        break
for i in sorted(ans):
    print(i)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
S=0
mod=10**9+7
for i in range(N-1):
  for j in range(i+1,N):
    S+=A[i]^A[j]
print(S%mod)

### FIXED CODE ###

N=int(input())
bin_a="".join(["{:060b}".format(a) for a in map(int, input().split())])
mod=10**9+7
xor=0
for d in range(60):
  ones=bin_a[59-d::60].count('1')
  xor+=2**d*ones*(N-ones)    
print(xor%mod)
### BUGGY CODE ###

s = list(input())
n = int(input())
q = [list(input().split()) for _ in range(n)]

from collections import deque
s = deque(s)

for qi in q:
    if qi[0] == "1":
        s.reverse()
    if len(qi) != 1:
        if qi[1] == '1':
            s.appendleft(qi[2])
        elif qi[1] == "2":
            s.append(qi[2])
print(''.join(s))


### FIXED CODE ###

from collections import deque

s = deque(input())
q = int(input())
flg = 0

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    flg += 1
  else:
    if (Q[1] == '1' and flg%2==0) or (Q[1] == '2' and flg%2==1):
      s.appendleft(Q[2])
    else:
      s.append(Q[2])
if flg%2==1:
  s.reverse()

print('' . join(s))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

def cur(x):
    for p in neighbor[x]:
        par=parent[x]
        if p!=par:
            parent[p]=x
            ans[p]+=ans[x]
            cur(p)

n,q=map(int,input().split())
neighbor=[[]  for _ in range(n+1)]
parent=[-1]*(n+1)
ans=[0]*(n+1)

for _ in range(n-1):
    a,b=map(int,input().split())
    neighbor[a].append(b)
    neighbor[b].append(a)

for _ in range(q):
    p,x=map(int,input().split())
    ans[p]+=x

cur(1)
print(" ".join(map(str,ans[1:])))

### FIXED CODE ###

n,q=map(int,input().split())
neighbor=[[]  for _ in range(n+1)]
ans=[0]*(n+1)

for _ in range(n-1):
    a,b=map(int,input().split())
    neighbor[a].append(b)
    neighbor[b].append(a)


for _ in range(q):
    p,x=map(int,input().split())
    ans[p]+=x

visit=[True]*(n+1)
tmp=[1]
for x in tmp:
    visit[x]=False
    for y in neighbor[x]:
        if visit[y]:
            tmp.append(y)
            ans[y]+=ans[x]

print(*ans[1:])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
n,m=map(int,input().split())
graph=[[] for i in range(3*n)]
for i in range(m):
    a,e=map(int,input().split())
    graph[a-1].append(e+n-1)
    graph[a+n-1].append(e+2*n-1)
    graph[a+2*n-1].append(e-1)
s,t=map(int,input().split())
s-=1;t-=1
visited=[False]*(3*n)
dfs_alpha_memory = [3*n]*(3*n)
dfs_skipped = [set() for _ in range(3*n)]
dfs_whether_precise = [False]*(3*n)

def dfs(checking, alpha=3*n):
    if dfs_alpha_memory[checking] < alpha:
        return dfs_alpha_memory[checking]
    elif dfs_whether_precise[checking]:
        moves = dfs_skipped[checking]
    else:
        moves = graph[checking]
    pr_flag = True
    looped = set()
    visited[checking]=True
    initial_alpha = alpha
    for next in moves:
        if t==next:
            alpha=1
            looped = set()
            dfs_whether_precise[checking] = True
            break
        if visited[next]:
            looped.add(next)
            continue
        alpha=min(alpha, dfs(next, alpha-1)+1)
        pr_flag &= dfs_whether_precise[next]
    else:
        if alpha >= initial_alpha:
            alpha = 3*n
    dfs_skipped[checking] &= looped
    dfs_alpha_memory[checking] = min(alpha, dfs_alpha_memory[checking])
    dfs_whether_precise[checking] &= pr_flag
    visited[checking] = False
    return alpha

ans = dfs(s)//3
if ans >= n:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, M = inpl()
uv = [ set() for _ in range(3*N) ]
vu = [ set() for _ in range(3*N) ]
for _ in range(M):
    u, v = inpl()
    for k in range(3):
        uv[u-1 + k*N].add(v-1 + ((k+1)%3)*N)
        vu[v-1 + ((k+1)%3)*N].add(u-1 + k*N)
S, T = inpl()
S -= 1
T -= 1

cur = set({S})
for i in range(3*N):
    if i % 3 == 0 and T in cur:
        print(i//3)
        break
    elif len(cur) == 0:
        print(-1)
        break
    prev = cur
    cur = set()
    for u in prev:
        for fr in vu[u]:
            uv[fr].remove(u)
        cur |= uv[u]
else:
    print(-1)
### BUGGY CODE ###

N = int(input())
list_A = list(map(int, input().split()))

threshold = 1000000007
ans = 0
for i in range(N-1):
    count = 0
    for j in range(i+1, N):
        count += list_A[j]
    count = count % threshold
    ans += (list_A[i] * count % threshold)
ans = ans % threshold
print(ans)

### FIXED CODE ###

N = int(input())
list_A = list(map(int, input().split()))

threshold = 1000000007
tmp = sum(list_A[1:])
ans = 0
for i in range(N-1):
    if i == 0:
        ans += list_A[i] * tmp % threshold
    else:
        tmp = tmp - list_A[i]
        ans += (list_A[i] * tmp % threshold)
ans = ans % threshold
print(ans)
### BUGGY CODE ###

n, y = map(int, input().split())

ans =[-1, -1, -1]
for i in range(n + 1):
    for j in range(n + 1 - i):
        for k in range(n + 1 - i - j):
            if i * 10000 + j * 5000 + k * 1000 == y and i + j + k == n:
                ans = [i, j, k]
                break
print(' '.join(map(str, ans)))

### FIXED CODE ###

n, y = map(int, input().split())
 
ans =[-1, -1, -1]
for i in range(n + 1):
    for j in range(n + 1 - i):
            if i * 10000 + j * 5000 + (n - i - j) * 1000 == y:
                ans = [i, j, n - i - j]
                break
print(' '.join(map(str, ans)))
### BUGGY CODE ###

n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
dp=[[0]*(w+1) for _ in range(n+1)]
for i in range(1,n+1):
    for j in range(1,w+1):
        if wv[i-1][0]<=j:
            dp[i][j]=max(dp[i-1][j],dp[i-1][max(0,j-wv[i-1][0])]+wv[i-1][1])
        else:
            dp[i][j]=dp[i-1][j]
print(dp[-1][-1])

### FIXED CODE ###

n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
dp=[[0]*(w+1) for _ in range(n+1)]
for i in range(1,n+1):
    for j in range(1,w+1):
        if wv[i-1][0]<=j:
            dp[i][j]=max(dp[i-1][j],dp[i-1][max(0,j-wv[i-1][0])]+wv[i-1][1])
        else:
            dp[i][j]=dp[i-1][j]
print(dp[-1][-1])
### BUGGY CODE ###

import sys
lines = sys.stdin.readlines()

while(True):
    for i in lines:
        a,b = (map(int,i.split()))
        
        if a == 0 and b == 0:
            break
        if a == 1 and b == 1:
            print('#')
            continue
        for j in range(a-1):
            print('#' * b)


### FIXED CODE ###

while(True):
    H,W = (map(int,input().split()))   
    if H == 0 and W == 0:
        break
 
    for i in range(H):
        print('#'*W)
    print('')
### BUGGY CODE ###

N = int(input())
A_list = list(map(int, input().split()))
for i in range(1, N):
    print(A_list.count(i))
print(0)


### FIXED CODE ###

N = int(input())
A_list = list(map(int, input().split()))
ans_list = [0] * (N + 1)
for i in range(N-1):
    ans_list[A_list[i]] += 1
for j in range(N):
    print(ans_list[j + 1])
### BUGGY CODE ###

a, b, x = map(int, input().split())

ans = 0

for i in range(b - a + 1):
    if((a + i) % x) == 0:
        ans = ans + 1


print(ans)


### FIXED CODE ###

a, b, x = map(int, input().split())

def func(n):
    if n == -1:
        return 0

    else:
        return n // x + 1

print(func(b) - func(a - 1))
### BUGGY CODE ###

n,h = (int(i) for i in input().split())
a = []
b = []
cnt = 0
for i in range(n):
  x,y = (int(i) for i in input().split())
  a.append(x)
  b.append(-y)
import heapq
max = max(a)
b.append(-max)
heapq.heapify(b)
while(h>0):
  pop = heapq.heapreplace(b, -max)
  h -= -pop
  cnt += 1
print(cnt)

### FIXED CODE ###

n,h = (int(i) for i in input().split())
a = []
b = []
cnt = 0
for i in range(n):
  x,y = (int(i) for i in input().split())
  a.append(x)
  b.append(y)
max = max(a)
b.sort()
for i in b[::-1]:
  if i >= max:
    h -= i
    cnt += 1
 #   print(i,h,cnt)
  if h <= 0:
    print(cnt)
    exit()
import math
cnt += int(math.ceil(h / max))
print(cnt)
### BUGGY CODE ###

a = []
n = int(input())
for i in range(n):
  a.append(int(input()))
for j in range(n):
  b = [a[i] for i in range(n) if i != j]
  print(max(b))

### FIXED CODE ###

b = []
n = int(input())
m = 0
for i in range(n):
  a = int(input())
  b.append(a)
  if m < a:
    m = a
    c = [i]
  elif m == a:
    c.append(i)

if len(c) != 1:
  for i in range(n):
    print(m)
else:
  del b[c[0]]
  for i in range(n):
    if i == c[0]:
      print(max(b))
    else:
      print(m)
### BUGGY CODE ###

n,m=map(int, input().split())
a=[int(x) for x in input().split()]

while m>0:
    max_value = max(a)
    max_index = a.index(max_value)
    a[max_index]=max_value/2
    m-=1
a=[int(x) for x in a]
print(sum(a))

### FIXED CODE ###

n,m=map(int, input().split())
a=[int(x) for x in input().split()]
a=[-1*x for x in a]

import heapq

heapq.heapify(a)

for i in range(m):
    max_value = heapq.heappop(a) #最大値の取得
    heapq.heappush(a, (max_value/2)) #半額にして-1倍してからキューに戻す

a=[-1*x for x in a]
a=[int(x) for x in a]
print(sum(a))
### BUGGY CODE ###

S = input()
N = len(S)
while True:
  temp_S = S
  temp_S = temp_S.replace('0011', '').replace('1010', '')
  temp_S = temp_S.replace('0101', '').replace('1100', '')
  temp_S = temp_S.replace('01', '').replace('10', '')
  if temp_S == S:
    break
  S = temp_S

print(N - len(S))

### FIXED CODE ###

S = input()
C0, C1 = S.count('0'), S.count('1')
print(2 * min(C0, C1))
### BUGGY CODE ###

N, P = [int(i) for i in input().strip().split(" ")]
S = input()

ans = 0
for i in range(1, N+1):
    for j in range(N-i+1):
        s = int(S[j:j+i])
        if s % P == 0:
            ans += 1
print(ans)

### FIXED CODE ###

N, P = [int(i) for i in input().strip().split(" ")]
S = [i for i in input()]

r = [int(i) % P for i in S]

ret = 0

if P == 2 or P == 5:
    for i in range(N):
        if int(S[i]) % P == 0:
            ret += (i+1)
else:
    from collections import defaultdict
    S.reverse()
    mp = defaultdict(int)
    mp[0] = 1
    cur = 0
    p10 = 1
    for c in S:
        cur = (cur + int(c) * p10) % P
        ret += mp[cur]
        mp[cur] += 1
        p10 *= 10
        p10 %= P

print(ret)
### BUGGY CODE ###

n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if 2*j - i >= n:
            break
        
        if string[i] == string[j]: 
            continue
        rgb = set(['R', 'G', 'B'])
        rgb.remove(string[i])
        rgb.remove(string[j])
        k = rgb.pop()
        if 2*j-i < n and string[2*j-i] == k:
            ans -= 1
print(ans)
        
        


### FIXED CODE ###

n = int(input())
string = input()
r = 0
g = 0
b = 0
for c in string:
    if c == 'R':
        r += 1
    elif c == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        if 2*j - i >= n:
            break
        
        if string[i] != string[j] and string[i] != string[2*j-i] and string[j] != string[2*j-i]:
            ans -= 1
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

#=====================================
class Tree():
  def __init__(self):
    self.root = None
    self.children = set()
    self.counter = 0

  def add_child(self, child):
    self.children.add(child)
  
  def reconstruct(self, root=None):
    if root in self.children:
      self.root = root
      self.children.remove(root)
    for child in self.children:
      child.reconstruct(self)
  
  def add_count(self, x):
    self.counter += x

  def get_count(self):
    res = self.counter
    if self.root:
      res += self.root.get_count()
    return res
      
#=====================================

def solve():
  n,q = [int(i) for i in input().split()]
  trees = [Tree() for i in range(n)]

  for _ in range(n-1):
    a,b = [int(i) for i in input().split()]
    trees[a-1].add_child(trees[b-1])
    trees[b-1].add_child(trees[a-1])

  trees[0].reconstruct()

  for _ in range(q):
    p,x = [int(i) for i in input().split()]
    trees[p-1].add_count(x)

  print(*[trees[i].get_count() for i in range(n)])

if __name__ == '__main__':
  solve()

### FIXED CODE ###

N, Q = map(int, input().split())
graph = [[] for i in range(200010)]
ans = [0 for i in range(200010)]
import sys
#sys.setrecursionlimit(100000)
sys.setrecursionlimit(10**7)
#
readline = sys.stdin.readline


for _ in range(N - 1):
    #a, b = map(int, input().split())
    a, b = map(int, readline().split())
    a -= 1
    b -= 1

    graph[a].append(b)
    graph[b].append(a)

for _ in range(Q):
    #p, x = map(int, input().split())
    p, x = map(int, readline().split())
    p -= 1

    ans[p] += x

def dfs(node, frm=-1):
    for to in graph[node]:
        if to == frm:
            continue
        ans[to] += ans[node]
        dfs(to, node)
dfs(0)

print(*ans[:N])
### BUGGY CODE ###

#abc177_C_3_掛け算の時点で計算を挟む
int_n = int(input())
list_tmp = list(map(int, input().split()))
 
int_sum = 0
int_divide = (10**9) + 7
 
for i in range(0, len(list_tmp) - 1):
  for j in range(i + 1, len(list_tmp)):
    int_sum = (int_sum + ((list_tmp[i]%int_divide) * (list_tmp[j]%int_divide)))%int_divide
    
print(int_sum)

### FIXED CODE ###

#abc177_C_6_retry_累積和を使用する
int_n = int(input())
list_tmp = list(map(int, input().split()))
 
int_sum = 0
int_divide = (10**9) + 7
int_sum_tmp = sum(list_tmp)

for i in range(0, len(list_tmp) - 1):
  int_sum_tmp = int_sum_tmp - list_tmp[i]
  int_sum = int_sum + (int_sum_tmp * list_tmp[i])
print(int_sum % int_divide)
#  for j in range(i + 1, len(list_tmp)):
#    if list_tmp[i] >= int_divide:
#      list_tmp_i = list_tmp[i]%int_divide
#    else:
#      list_tmp_i = list_tmp[i]
#      
#    if list_tmp[j] >= int_divide:
#      list_tmp_j = list_tmp[j]%int_divide
#    else:
#      list_tmp_j = list_tmp[j]    
#    
#    int_sum = int_sum + (list_tmp_i*list_tmp_j)
#    
#    if int_sum >= int_divide:
#      int_sum = int_sum%int_divide
#      
#print(int_sum)
### BUGGY CODE ###

n, k = map(int, input().split())
ls = list(map(int, input().split()))

res = 2 * (abs(ls[0]) + abs(ls[-1]))
for i in range(n - k + 1):
    l = ls[i:i + k]
    if l[-1] <= 0:
        res = min(res, -l[0])
    elif l[0] >= 0:
        res = min(res, l[-1])
    else:
        res = min(res, -2 * l[0] + l[-1], -l[0] + 2 * l[-1])
print(res)

### FIXED CODE ###

n, k = map(int, input().split())
ls = list(map(int, input().split()))

res = 2 * (abs(ls[0]) + abs(ls[-1]))
for i in range(n - k + 1):
    a = ls[i]
    b = ls[i + k - 1]
    if b <= 0:
        res = min(res, -a)
    elif a >= 0:
        res = min(res, b)
    else:
        res = min(res, -2 * a + b, -a + 2 * b)
print(res)
### BUGGY CODE ###

def main():
    n, k = map(lambda x: int(x), input().split())
    w = [int(input()) for _ in range(n)]
    cumulative_sum = [0]
    for i in w:
        cumulative_sum.append(cumulative_sum[-1] + i)
    p = max(max(w), sum(w)//k)
    while True:
        left_track = k
        capacity = 0
        for weight in w:
            if capacity + weight <= p:
                capacity += weight
            else:
                left_track -= 1
                capacity = weight
                if left_track == 0:
                    p += 1
                    break
        else:
            break
    print(p)


if __name__ == '__main__':
    main()



### FIXED CODE ###

import bisect


def main():
    n, k = map(lambda x: int(x), input().split())
    w = [int(input()) for _ in range(n)]

    cumulative_sum = [0]
    for weight in w:
        cumulative_sum.append(cumulative_sum[-1] + weight)
    p_min, p_max = max(w), cumulative_sum[-1]

    while p_min != p_max:
        p_mid = (p_min + p_max) // 2
        left_track = k
        index = 1
        while left_track > 0:
            left_track -= 1
            index = bisect.bisect_right(cumulative_sum, cumulative_sum[index-1] + p_mid)
            if index == len(cumulative_sum):
                break
        else:
            p_min = p_mid+1
            continue
        p_max = p_mid

    print(p_min)


if __name__ == '__main__':
    main()