Harryxun/stf_run huggingface dataset:
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
m = {}
np = [deque([]) for _ in range(n)]
for _ in range(n-1):
    a, b, c = [int(x) for x in input().strip().split()]
    m[(a, b)] = c
    m[(b, a)] = c
    np[a-1].append(b)
    np[b-1].append(a)

q, k = [int(x) for x in input().strip().split()]
master = [-1 for _ in range(n)]
master[k-1] = 0

def dfs(cur, pre, master):
    #print('cur = {}, pre = {}'.format(cur, pre))
    for p in np[cur-1]:
        if p == pre:
            continue
        master[p-1] = master[cur-1] + m[(cur, p)]
        dfs(p, cur, master)

dfs(k, -1, master)

for _ in range(q):
    x, y = [int(x) for x in input().strip().split()]
    print(master[x-1] + master[y-1])

### FIXED CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
n = int(input())
m = {}
np = [deque([]) for _ in range(n)]
for _ in range(n-1):
    a, b, c = [int(x) for x in input().strip().split()]
    m[(a, b)] = c
    m[(b, a)] = c
    np[a-1].append(b)
    np[b-1].append(a)

q, k = [int(x) for x in input().strip().split()]
master = [-1 for _ in range(n)]
master[k-1] = 0

def dfs(cur, pre, master):
    #print('cur = {}, pre = {}'.format(cur, pre))
    for p in np[cur-1]:
        if p == pre:
            continue
        master[p-1] = master[cur-1] + m[(cur, p)]
        dfs(p, cur, master)

dfs(k, -1, master)

for _ in range(q):
    x, y = [int(x) for x in input().strip().split()]
    print(master[x-1] + master[y-1])
import math

n, d, a = map(int,input().split())
xh = [[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]

num = 0
xh.sort(key=lambda xh:xh[0])

dam = [0]*n
for i in range(n):
    res = xh[i][1]-dam[i]
    pos = xh[i][0]
    if res > 0:
        nat = math.ceil(res/a)
        num += nat
        for j in range(i+1,n):
            if xh[j][0]-d <= pos + d:
                dam[j] += a*nat
            else:
                break
    print(dam)
print(num)


### FIXED CODE ###

h, a = map(int,input().split())

if h%a == 0:
    print(int(h/a))
else:
    print(int(h/a)+1)
N, A, B = map(int, input().split())

if N == 1 :
    if A != B :
        ans = 0
    elif :
        ans = 1



elif A > B :
    ans = 0
elif A == B :
    ans = 1
else:
    ans = (B*(N-1)+A) - (A*(N-1)+B) + 1

print(ans)

### FIXED CODE ###

N, A, B = map(int, input().split())

if N == 1 :
    if A != B :
        ans = 0
    else :
        ans = 1



elif A > B :
    ans = 0
elif A == B :
    ans = 1
else:
    ans = (B*(N-1)+A) - (A*(N-1)+B) + 1

print(ans)
w=input()

for i in range(len(w)):
    if w.count(w[i])%2==0:
        pass
    else:
        break
else:
    print("Yes")
    exit(1)
print("No")

### FIXED CODE ###

w=input()

for i in range(len(w)):
    if w.count(w[i])%2==0:
        pass
    else:
        break
else:
    print("Yes")
    exit(0)
print("No")
N = int(input())
V = list(map(int,input().split()))
D1 = {}
D2 = {}
for i in range(N) :
    if i % 2 == 0:
        if V[i] in D1 :
            D1[V[i]] += 1
        else :
            D1[V[i]] = 1
    else :
        if V[i] in D2 :
            D2[V[i]] += 1
        else :
            D2[V[i]] = 1
m1 = max(D1.values())
m2 = max(D2.values())
if max(D1,key = D1.get) != max(D2,key = D2.get) :
    print(N-m1-m2)
else :
    D1[max(D1,key=D1.get)] = 0
    D2[max(D2,key=D1.get)] = 0
    mm1 = max(D1.values())
    mm2 = max(D2.values())
    if mm1 >= mm2 :
        print(N-m2-mm1)
    else :
        print(N-m1-mm2)


### FIXED CODE ###

#input phase
N = int(input())
V = list(map(int,input().split()))
#make dictionary
D1 = {}
D2 = {}
#append element to dictionary
for i in range(N):
    if i % 2 == 0 :
        if V[i] in D1 :
            D1[V[i]] += 1
        else:
            D1[V[i]] = 1
    else:
        if V[i] in D2 :
            D2[V[i]] += 1
        else:
            D2[V[i]] = 1
#define max-value of each dictionary
maxD1 = max(D1.values())
maxD2 = max(D2.values())
# I haven't appreciated dictionary method well...
if max(D1, key=D1.get) != max(D2, key=D2.get):
    print(N-maxD1-maxD2)
else:
    D1[max(D1, key=D1.get)] = 0
    D2[max(D2, key=D2.get)] = 0
    maxD12 = max(D1.values())
    maxD22 = max(D2.values())
    if maxD12 >= maxD22 :
        if len(D1) > 1 :
            print(N-maxD12-maxD2)
        else :
            print(N-maxD2)
    else :
        if len(D2) > 1 :
            print(N-maxD1-maxD22)
        else :
            print(N-maxD2)
N,A,B = map(int,input().split())

ans = (N//(A+B))*A
tmp = N - (N//(A+B))*(A+B)
if(tmp <= A): ans += tmp
else: ans += tmp
print(ans)

### FIXED CODE ###

S = input()
if((S[0] == S[1])and(S[1] == S[2])):
  print('No')
else:
  print('Yes')
N,M = map(int,input().split())
lsab = []
for i in range(N):
    a,b = map(int,input().split())
    lsab.append([a,b])
lscd = []
for j in range(M):
    c,d= map(int,input().split())
    lscd.append([c,d])

lsans = []
for i in range(N):
    max1 = 2*10**8+100
    for j in range(M):
        dis = abs(lsab[i][0]-lscd[j][0])+abs(lsab[i][1]-lscd[j][1])
        if dis < max1:
            ans = j+1
            max1 = dis
    lsans.append(ans)

for i in range(N):
    print(lsans[i])

### FIXED CODE ###

N,M = map(int,input().split())
lsab = []
for i in range(N):
    a,b = map(int,input().split())
    lsab.append([a,b])
lscd = []
for j in range(M):
    c,d= map(int,input().split())
    lscd.append([c,d])

lsans = []
for i in range(N):
    max1 = 4*10**8+100
    ans = 0
    for j in range(M):
        dis = abs(lsab[i][0]-lscd[j][0])+abs(lsab[i][1]-lscd[j][1])
        if dis < max1:
            ans = j+1
            max1 = dis
    lsans.append(ans)

for i in range(N):
    print(lsans[i])
L, R = list(map(int,input().split()))

minimum = L * R
for i in range(L, min(R, L+2020)):
    for j in range(i+1, min(R+1, i+2020):
        
        minimum = min(minimum, i * j // 2019)
        

print(minimum)

### FIXED CODE ###

L, R = list(map(int,input().split()))

minimum = L * R
for i in range(L, min(R, L+2020)):
    for j in range(i+1, min(R+1, i+2021)):
        
        minimum = min(minimum, i * j % 2019)
        

print(minimum)
# -*- coding: utf-8 -*-
 
if “o” == s[:1] :
   x = 1
    
if “o” == s[1:2] :
   x = x + 1
 
if “o” == s[2:3] :
   x = x + 1
 
y = 700 + x * 100
 
print str(y)

### FIXED CODE ###

# -*- coding: utf-8 -*-
s = input()
a = s.count('o')
y = 700 + 100 * a
print(y)
s = input()
if s == 'ABC':
    ans = 'ARC'
else:
    anse = 'ABC'

print(ans)

### FIXED CODE ###

s = input()
if s == 'ABC':
    ans = 'ARC'
else:
    ans = 'ABC'

print(ans)
n=int(input())
lst=[]
for i in range(n):
  c,s,f=map(int,input().split())
  lst.append([c,s,f])
for j in range(n-1):
  time=lst[j][0]+lst[j][1]
  ind=j
  for k in range(n-j-2):
    if time<=lst[ind+1][1] :
      time=lst[ind+1][0]+lst[ind+1][1]
      ind+=1
    elif (time-lst[ind+1][1])%lst[ind+1][2]==0:
      time+=lst[ind+1][2]
      ind+=1
    else:
      time+=lst[ind+1][2]+lst[ind+1][2]-(time-lst[ind+1][1])%lst[ind+1][2]
      ind+=1
  print(time)
print(0)


### FIXED CODE ###

n=int(input())
lst=[]
for i in range(n-1):
  c,s,f=map(int,input().split())
  lst.append([c,s,f])
for j in range(n-1):
  time=lst[j][0]+lst[j][1]
  ind=j
  for k in range(n-j-2):
    if time<=lst[ind+1][1] :
      time=lst[ind+1][0]+lst[ind+1][1]
      ind+=1
    elif (time-lst[ind+1][1])%lst[ind+1][2]==0:
      time+=lst[ind+1][0]
      ind+=1
    else:
      time+=lst[ind+1][0]+lst[ind+1][2]-(time-lst[ind+1][1])%lst[ind+1][2]
      ind+=1
  print(time)
print(0)
N = int(input())

for i in range(1, N + 1, 1):
  
  if i == 1:
    power = i
  else:
  	power = i * power
  
print(power / (10 ** 9 + 7))

### FIXED CODE ###

N = int(input())
power = 1

for i in range(1, N + 1, 1):
 
  power = (i * power) % (10**9 + 7)
  
print(power)
N , L= map(int,input().split())
lst = list()
Str = ''
for i in range(L):
    lst.append(input())
for k  in range(N):
    for j in range(N-1):
        if lst[j] >= lst[j+1]:
            lst[j],lst[j+1] = lst[j+1] ,lst[j]

for i in lst:
    Str += i
print(Str)


### FIXED CODE ###

N,L=map(int, input().split())
S=[]
for i in range(N):
    S.append(input())
S.sort()
result=''
for j in range(N):
    result+=S[j]     
print(result)
import fractions

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

R = 1 # 最小公倍数
# 最小公倍数を求める
for A in A_list:
    R = R * A // fractions.gcd(R, A)

# Bの合計を求める
ans = 0
for A in A_list:
    ans = ans + (R / A)

print(int(ans%(pow(10,9)+7)))


### FIXED CODE ###

import fractions

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

mod = 10**9+7

R = 1 # 最小公倍数
# 最小公倍数を求める
for A in A_list:
    R *= A // fractions.gcd(R, A)

# Bの合計を求める
ans = 0
for A in A_list:
    ans = ans + R // A # 計算は小数として出してはダメだから//を使う

ans %= mod

print(ans)
N,M = map(int, input().split())

balls_in_box = [0] + [1 for _ in range(1,N+1)]

#赤玉の初回移動までは捨てる
for i in range(M):
    x,y = map(int, input().split())
    if x == 1:
        red_ball_containable_box = set()
        red_ball_containable_box.add(y)
        balls_in_box[x] -= 1
        balls_in_box[y] += 1
        start = i + 1
        break
    else:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1

for j in range(start,M):
    x,y = map(int, input().split())
    if x in red_ball_containable_box:
        red_ball_containable_box.add(y)
        balls_in_box[x] -= 1
        balls_in_box[y] += 1
        if balls_in_box[x] == 0: #赤玉が入っている可能性が100％の箱を全部空にすれば確実に移る
            red_ball_containable_box.remove(x)
    else:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1

print(len(red_ball_containable_box))

### FIXED CODE ###

N,M = map(int, input().split())

balls_in_box = [0] + [1 for _ in range(1,N+1)]
red_ball_containable_box = set()
red_ball_containable_box.add(1)

for i in range(M):
    x,y = map(int, input().split())
    if x in red_ball_containable_box:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1
        red_ball_containable_box.add(y)
        if balls_in_box[x] == 0: #赤玉が入っている可能性が100％の箱を全部空にすれば確実に移る
            red_ball_containable_box.remove(x)
    else:
        balls_in_box[x] -= 1
        balls_in_box[y] += 1

print(len(red_ball_containable_box))
a,b,k=map(int,input().split())
import math
temp=a*b//math.gcd(a,b)
print(temp*k)
  

### FIXED CODE ###

a,b,k=map(int,input().split())
temp=[]
for i in range(1,min(a,b)+1):
  if a%i==0 and b%i==0:
    temp.append(i)
print(temp[len(temp)-k])
N,W=map(int,input().split())
for i in range(n):
    w[i],v[i]=map(int,input().split())
dp=[[0 for i in range(W+1)] for j in range(N+1)]
for i in range(N):
    for j in range(W+1):
        if j<w[i]:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])
print(dp[N][W])


### FIXED CODE ###

N,W=map(int,input().split())
w=[0]*N
v=[0]*N
for i in range(N):
    w[i],v[i]=map(int,input().split())
dp=[0]*(W+1)
for i in range(N):
    j=W
    while j>=w[i]:
        dp[j]=max(dp[j],dp[j-w[i]]+v[i])
        j-=1
print(dp[W])
#!/usr/bin/env python3

a = [ int(input()) for i in range(int(input())) ]

print(max(a)-min(a))


### FIXED CODE ###

#!/usr/bin/env python3
 
N = int(input())
a = list(map(int, input().split()))
 
print(max(a) - min(a))
N, M = map(int, input().split())
id = [list(map(int, input().split())) for _ in range(M)]
ans = 0
for i in range(1, N+1):
    a = id[0]
    b = id[1]
    if a[0] <= i <= a[1] and b[0] <= i <= b[1]:
        ans = ans + 1
print(ans)


### FIXED CODE ###

N, M = map(int, input().split())
id = [list(map(int, input().split())) for _ in range(M)]
L = []
R = []
for i in range(M):
    L.append(id[i][0])
    R.append(id[i][1])
if max(L) <= min(R):
    print(min(R) - max(L) + 1)
else:
    print(0)
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)] 
ans = [0]*(10**5+1)

for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
S = for i in range(maxlen+1):
        S += ans [i]*i
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    S = S + ans[BC[i][1]] *BC[i][1] - ans[BC[i][0]]*BC[i][0]    
    print(S)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)] 
ans = [0]*(10**5+1)

for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
S = 0
for i in range(maxlen+1):
        S += ans [i]*i
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    S = S + ans[BC[i][0]] *BC[i][1] - ans[BC[i][0]]*BC[i][0]
    ans[BC[i][0]] = 0
    print(S)
# coding: utf-8
#Problem Name: Digit Number
#ID: tabris
#Mail: t123037@kaiyodai.ac.jp

s = []
for i in range(200):
    s.append(raw_input())
    if s[-1] == '':
        s.remove('')
        break
    
for i in s:
    a,b = map(int,i.split(' '))
    print len(str(a+b))

'''
Sample Input
5 7
1 99
1000 999
'''

### FIXED CODE ###

# coding: utf-8
#Problem Name: Digit Number
#ID: tabris
#Mail: t123037@kaiyodai.ac.jp

while True:
    try:
        a,b = map(int,raw_input().split(' '))
        print len(str(a+b))
    except:
        break

'''
Sample Input
5 7
1 99
1000 999
'''
#15rectangle
H,W=(int(i) for i in input().split())
while H!=0 or W!=0:
    for i in range(H-1):
        S=""
        for j in range(W):
            S+="#"
        print(S)
    for j in range(W):
        S+="#"
    print(S, end=??????)
    H,W=(int(i) for i in input().split())
    if H!=0 or W!=0:
        print("\n")

### FIXED CODE ###

#15rectangle
H,W=(int(i) for i in input().split())
while H!=0 or W!=0:
    for i in range(H-1):
        S=""
        for j in range(W):
            S+="#"
        print(S)
    S=""
    for j in range(W):
        S+="#"
    print(S, end="")
    H,W=(int(i) for i in input().split())
  #  if H!=0 or W!=0:
    print("\n")
N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = 0
for i in a:
    x -= i
    if x => 0:
        b += 1
    else:
        break
if x == 0:
    print(b)
else:
    print(b-1)

### FIXED CODE ###

N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = 0
for i in a:
    x -= i
    if x >= 0:
        b += 1
        N -= 1
    else:
        break
if x == 0:
    print(b)
else:
    if N == 0:
        print(b-1)
    else:
        print(b)
a=input().split()
N=int(a[0])
K=int(a[1])
if K > (N-1)*(N-2)/2 or K < N-2:
  print(-1)
else:
  p = [0]*N
  p[2] = (N-1)*(N-2)/2
  for i in range(2,N-1):
    p[i+1] = p[i] + i - N + 1
  t = 0
  for i in range(2,N):
    if p[i] == K:
      l = i
      break
    else:
      t = t + 1
  if t == N - 2:
    print(-1)
  else:
    print(N-1)
    for j in range(l-1):
      print(j+1,j+2)
    for j in range(l+1,N+1):
      print(l,j)

### FIXED CODE ###

n,k = map(int,input().split())
if k > (n-1)*(n-2)/2:
  print(-1)
else:
  p = (n-1)*(n-2)/2 - k
  print(n-1+int(p))
  for i in range(2,n+1):
    print(1,i)
  x = 2
  y = 3
  for _ in range(int(p)):
    print(x,y)
    y = y + 1
    if y == n + 1:
      x = x + 1
      y = x + 1
n,a,b,c,d=(int(i) for i in input().split())
s=input()
r="Yes"
for i in(a,d):
  if s[i]=="#":
    if s[i+1]=="#":
      r="No"
      break
if r=="Yes" and c>d:
  r="No"
  for i in range(d-b+1):
    if s[i+b-2]==".":
      if s[i+b-1]==".":
        if s[i+b]==".":
          r="Yes"
          break
print(r)

### FIXED CODE ###

n,a,b,c,d=(int(i) for i in input().split())
s=input()
r="Yes"
for i in range(d-a+1):
  if s[i+a-1]=="#":
    if s[i+a]=="#":
      r="No"
      break
if r=="Yes" and c>d:
  r="No"
  for i in range(d-b+1):
    if s[i+b-2]==".":
      if s[i+b-1]==".":
        if s[i+b]==".":
          r="Yes"
          break
print(r)
N,M=map(int,input().split())
L=list(map(int,input().split()))
X=0
for i in range(M):
  X=X+L[i]
if N-X<0:
  print(-1)
else:
  print(N-X)

### FIXED CODE ###

R=int(input())
print(R*2*3.14)
from bisect import bisect
from itertools import accumulate
from functools import reduce
MOD = 998244353

def solve(robots):
    N = len(robots)
    robots.sort()

    parent = [None]*(N+1)

    stack = [(float('inf'),0)]
    for i,(x,d) in enumerate(robots,start=1):
        d += x
        while stack[-1][0] <= x:
            stack.pop()
        parent[i] = stack[-1][1]
        while stack[-1][0] <= d:
            stack.pop()
        stack.append((d,i))

    adj = [list() for _ in range(N+1)]
    for i,p in enumerate(parent[1:],start=1):
        adj[p].append(i)

    rec = lambda i: reduce(lambda a,b:a*b%MOD, (rec(j)+1 for j in adj[i]), 1)

    return rec(0)

if __name__ == '__main__':
    N = int(input())
    robots = [tuple(map(int,input().split())) for _ in range(N)]
    print(solve(robots))

### FIXED CODE ###

from bisect import bisect
from itertools import accumulate
from functools import reduce
from sys import setrecursionlimit

setrecursionlimit(10000000)

MOD = 998244353

def solve(robots):
    N = len(robots)
    robots.sort()

    parent = [None]*(N+1)

    stack = [(float('inf'),0)]
    for i,(x,d) in enumerate(robots,start=1):
        d += x
        while stack[-1][0] <= x:
            stack.pop()
        parent[i] = stack[-1][1]
        while stack[-1][0] <= d:
            stack.pop()
        stack.append((d,i))

    dp = [1]*(N+1)
    for i in reversed(range(1,N+1)):
        p = parent[i]
        dp[p] *= dp[i]+1
        dp[p] %= MOD
    return dp[0]

if __name__ == '__main__':
    N = int(input())
    robots = [tuple(map(int,input().split())) for _ in range(N)]
    print(solve(robots))
import math

n=int(input())
x = list(map(int, input().split()))
ans=0
mod=10**9+7


for i in range(1,n):
    ans+=(x[n-1]-x[i-1])/i
    ans%=mod

ans*=math.factorial(n-1)
ans%=mod

print(int(ans))

### FIXED CODE ###

import math

n = int(input())
x = list(map(int, input().split()))
ans = 0
mod = 10 ** 9 + 7
f = 1
for i in range(1,n):
  f *= i
  f %= mod

for i in range(n-1):
    ans+=(x[n-1]-x[i])*pow(i+1,mod-2,mod)

ans*=f
ans%=mod


print(ans)
N=int(input())

if N<1000:
    print(“ABC”)
else:
    print(“ABD”)

### FIXED CODE ###

N=int(input())
if N<1000:
    print("ABC")
else:
    print("ABD")
n=int(input())

for i in (1,n):
  if 2**i>n
    print(2**(i-1))

### FIXED CODE ###

n=int(input())

for i in range(1,8):
  if 2**i>n:
    n=2**(i-1)
    break
  if 1==n:
    break
  if 2**i==n:
    break
    
print(n)
import sys
input = sys.stdin.readline
def main():
    N, P = map( int ,input().split())
    S = [ int(s) for s in list(input())][::-1]
    if P == 2 or P == 5:
        ans = 0
        for i in range(N):
            if S[i]%P == 0:
                ans += N-i
        print(ans)
        return
    L = [0]*P
    L[0] = 1
    t = 0
    s = 1
    for i in range(N):
        t = (S[i]*s+t)%P
        L[t] += 1
        s = s*10%P
    ans = 0
    for l in L:
        ans += l*(l-1)//2
    print(ans)
    
if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    N, P = map( int ,input().split())
    S = [ int(s) for s in list(input())][::-1]
    if P == 2 or P == 5:
        ans = 0
        for i in range(N):
            if S[i]%P == 0:
                ans += N-i
        print(ans)
        return
    L = [0]*P
    L[0] = 1
    t = 0
    for i in range(N):
        t = (S[i]*pow(10,i,P)+t)%P
        L[t] += 1
    ans = 0
    for l in L:
        ans += l*(l-1)//2
    print(ans)
    
if __name__ == '__main__':
    main()
import numpy as np
import concurrent.futures as confu

def cou(i):
		if i % c != 0:
			if i % d != 0:
				return 1
		return 0
		
		
a = input()
a, b, c, d = [int(i) for i in a.split(" ")]
count = 0
with confu.ProcessPoolExecutor() as ex:
	th = ex.map(cou, range(a,b))
	for i in th:
		count += i
print(count)

### FIXED CODE ###

import fractions

def lcm(x, y):
	return (x * y) // fractions.gcd(x, y)

a = input()
a, b, c, d = [int(i) for i in a.split(" ")]
dc = (b // c) - ((a-1) // c)
dd = (b // d) - ((a-1)  // d)
dcd = (b // lcm(c, d)) - ((a-1) // lcm(c, d))
print((b - a) + 1 - (dc + dd - dcd))
from collections import deque

N = int,input()
c = input()

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
print(count)

### FIXED CODE ###

from collections import deque

N = int(input())
c = input()

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
  if(len(rd) == 0 or len(wd) == 0):
    break
print(count)
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

min = min(a)+min(b)

for i in range(M):
    c =list(map(int,input().split()))
    s = a[c[0]-1]+b[c[1]-1]-c[2]
    min = min(s,min)

print(min)


### FIXED CODE ###

A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

min = min(a)+min(b)

for i in range(M):
    c =list(map(int,input().split()))
    s = a[c[0]-1]+b[c[1]-1]-c[2]
    min = (s if s < min else min)

print(min)
import copy

N,M = map(int,input().split())

A = list(map(int,input().split()))

B =[]
C =[]

tA = []
ans = sum(A)

for i in range(M):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)

for i in range(N):
    for j in range(M):
        tA = copy.copy(A)
        for k in range(B[j]):
            if tA[i] < C[j]:
                tA[i] = C[j]
        if sum(tA) > ans:
            ans = sum(tA)

print (ans)


### FIXED CODE ###



A,B = map(int,input().split())

if A >= 13 :
    print(B)
elif A >= 6 :
    print(B//2)
else :
    print(0)
N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for i in range(N)]

if M == 0:
    print(0)
    quit()
else:
    s1 = sorted(XYZ, key=lambda c: c[0] + c[1] + c[2])
    s1max1 = sum(s1[-M:])
    s1max2 = sum(s1[:M])
    s2 = sorted(XYZ, key=lambda c: c[0] + c[1] - c[2])
    s2max1 = sum(s2[-M:])
    s2max2 = sum(s2[:M])
    s3 = sorted(XYZ, key=lambda c: c[0] - c[1] + c[2])
    s3max1 = sum(s3[-M:])
    s3max2 = sum(s3[:M])
    print(max(s1max1, s1max2, s2max1, s2max2, s3max1, s3max2))

### FIXED CODE ###

N, M = map(int, input().split())
cakes = []
for i in range(N):
    x, y, z = map(int, input().split())
    cakes.append([x, y, z])


if M == 0:
    print(0)
    quit()
score = [0]*8
s = sorted(map(lambda c: c[0]+c[1]+c[2], cakes))
score[0] = sum(s[-M:])
score[1] = -sum(s[:M])
s = sorted(map(lambda c: c[0]+c[1]-c[2], cakes))
score[2] = sum(s[-M:])
score[3] = -sum(s[:M])
s = sorted(map(lambda c: c[0]-c[1]+c[2], cakes))
score[4] = sum(s[-M:])
score[5] = -sum(s[:M])
s = sorted(map(lambda c: c[0]-c[1]-c[2], cakes))
score[6] = sum(s[-M:])
score[7] = -sum(s[:M])
print(max(score))