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

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

# ほとんどは全体のmax?
m = max(a)
p = a.index(m)

for i in range(n):
  if i == p or a[i] == m:
    print(max(a[:i]+a[i+1:]))
  else:
    print(m)

### FIXED CODE ###

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

# ほとんどは全体のmax?
m1 = max(a)

# max値が多い場合用に2nd max
m2 = sorted(a)[n-2]

for i in range(n):
  if a[i] == m1:
    print(m2)
  else:
    print(m1)
### BUGGY CODE ###

N = int(input())
List = list(map(int,input().split()))
ans = 1
for item in List:
  ans *= item
  if ans > 10 **18:
    ans = -1
print(ans)

### FIXED CODE ###

N = int(input())
List = list(map(int,input().split()))
ans = 1
for item in List:
  ans *= item
  if ans > 10 **18 or ans <= -1:
    ans = -1
print(ans)
### BUGGY CODE ###

from bisect import insort
s = input().strip()
k = int(input())

sublist = []
for i in range(len(s)):
    if len(sublist)>k:
        sublist = sublist[:k]
    for j in range(i+1,len(s)+1):
        if s[i:j] not in sublist and (len(sublist)<k or sublist[k-1]>s[i:j]):
            insort(sublist,s[i:j])

print(sublist[k-1])

### FIXED CODE ###

from bisect import insort
s = input().strip()
k = int(input())

sublist = []
for i in range(len(s)):
    if len(sublist)>k:
        sublist = sublist[:k]
    for j in range(i+1,len(s)+1):
        if len(sublist)>=k and sublist[k-1]<s[i:j]:
            break
        if s[i:j] not in sublist and (len(sublist)<k or sublist[k-1]>s[i:j]):
            insort(sublist,s[i:j])

print(sublist[k-1])
### BUGGY CODE ###

from operator import itemgetter
import sys

# スペース区切りの整数入力
n,d,a = map(int, input().split())

#指定数分の2つの整数を順番に受けてリストに入力
e = []
for i in range(n):
    e.append([int(x) for x in input().split()])
e.sort(key=itemgetter(0))

atk_times_sum = 0

atk = []

for i in range(n):
    for j in range(len(atk)-1,-1,-1):
        if e[i][0] <= atk[j][0]:
            e[i][1] -= atk[j][1]
        else:
            break    
    if e[i][1] <= 0:
        continue
    atk_u = e[i][0] + d*2
    if e[i][1] % a == 0:
        atk_times = e[i][1] // a
    else:
        atk_times = e[i][1] // a + 1
    e[i][1] = 0 
    atk_times_sum += atk_times
    atk.append([atk_u,a*atk_times])

print(atk_times_sum)

### FIXED CODE ###

from operator import itemgetter
from collections import deque
import sys

# スペース区切りの整数入力
n,d,a = map(int, input().split())

#指定数分の2つの整数を順番に受けてリストに入力
e = []
for i in range(n):
    e.append([int(x) for x in input().split()])
e.sort(key=itemgetter(0))

atk_times_sum = 0
atk_que = deque()
atk_acm_p = 0

for i in range(n):
    while len(atk_que) > 0:
        if atk_que[0][0] < e[i][0]:
            atk_u,atk_p = atk_que.popleft()
            atk_acm_p -= atk_p
        else:
            break
    e[i][1] -= atk_acm_p
    if e[i][1] <= 0:
        continue
    atk_u = e[i][0] + d*2
    if e[i][1] % a == 0:
        atk_times = e[i][1] // a
    else:
        atk_times = e[i][1] // a + 1
    e[i][1] = 0 
    atk_times_sum += atk_times
    atk_p = a * atk_times
    atk_acm_p += atk_p
    atk_que.append([atk_u,atk_p])

print(atk_times_sum)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
for i in range(Q):
  L, R = map(int, input().split())
  print(S[L-1:R].count("AC"))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
t = [0]*(N)
for i in range(N-1):
  t[i+1] = t[i] + (1 if S[i:i+2] == "AC" else 0) 
for i in range(Q):
  L, R = map(int, input().split())
  print(t[R-1]-t[L-1])
### BUGGY CODE ###

def bin_search(itr, v, ret=None):
  l, r = 0, len(itr) - 1
  while l <= r:
    m = l + (r - l) // 2
    if itr[m] == v:
      if ret == 'smaller':
        r = m - 1
        continue
      if ret == 'bigger':
        l = m + 1
        continue
      return m
    elif itr[m] > v:
      r = m - 1
    else:
      l = m + 1
  if ret == 'smaller':
    return min(l, r)
  if ret == 'bigger':
    return max(l, r)
  return -1
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
r = 0
for i in range(N):
  ai = bin_search(A, B[i], 'smaller')
  ci = bin_search(C, B[i], 'bigger')
  r += len(A[:ai+1]) * len(C[ci:])
print(r)

### FIXED CODE ###

from bisect import bisect_left
from bisect import bisect_right
N = int(input())
A = sorted(map(int, input().split()))
B = list(map(int, input().split()))
C = sorted(map(int, input().split()))
r = 0
for i in range(N):
  ai = bisect_left(A, B[i])
  ci = bisect_right(C, B[i])
  r += ai * (N - ci)
print(r)
### BUGGY CODE ###

N = int(input())
S = input().strip()

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

for i in reversed(range(N)):
    for j in reversed(range(i+1, N)):
        if S[i] != S[j]:
            dp[i][j] = 0
        if S[i] == S[j]:
            dp[i][j] = dp[i+1][j+1] + 1

ans = 0
for i in range(N):
    for j in range(i+1, N):
        now = min([dp[i][j], j-i])
        ans = max(ans, now)

print(ans)


### FIXED CODE ###

N = int(input())
S = input().strip()

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

for i in reversed(range(N)):
    for j in reversed(range(N)):
        if i >= j:
            continue
        if S[i] != S[j]:
            dp[i][j] = 0
        if S[i] == S[j]:
            dp[i][j] = dp[i+1][j+1] + 1

ans = 0
for i in range(N):
    for j in range(N):
        if i >= j:
            continue
        now = min([dp[i][j], j-i])
        ans = max(ans, now)

print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
P = tuple(map(lambda x:int(x)-1, input().split()))
swaps = [set() for _ in range(n)]
for i in range(m):
    x, y = tuple(map(lambda x:int(x)-1, input().split()))
    swaps[x].add(y)
    swaps[y].add(x)

c = 0
from collections import deque
for idx in range(n):
    q = deque([idx])
    visited = {idx}
    while q:
        i = q.pop()
        if idx == P[i]:
            c += 1
            break
        for s in swaps[i]:
            if not s in visited:
                visited.add(s)
                q.append(s)
    
print(c)


### FIXED CODE ###

n, m = map(int, input().split())
P = tuple(map(lambda x: int(x)-1, input().split()))
par = list(range(n))
size = [1] * n
def find(x):
    """ 根のノードの値を見つけて返す """
    if x != par[x]:
        par[x] = find(par[x])
    return par[x]

def unite(x, y):
    """xとyを結合する"""
    rootx = find(x)
    rooty = find(y)
    if rootx == rooty:
        return False
    else:
        if size[rootx] < size[rooty]:
            par[rootx] = rooty
            size[rooty] += size[rootx]
        else:
            par[rooty] = rootx
            size[rootx] += size[rooty]
        return True

def same(x, y):
    """ xとyが同じ集合に属しているか """
    return find(x) == find(y)

for _ in range(m):
    x, y = map(lambda x: int(x)-1, input().split())
    unite(x, y)
groups = {}
for idx, p in enumerate(par):
    p = find(p)
    groups.setdefault(p, set())
    groups[p].add(idx)

cnt = 0
for k, g in groups.items():
    for gi in g:
        pi = P[gi]
        if pi in g:
            cnt += 1
print(cnt)
### BUGGY CODE ###

s = input()
if(len(s) < 5):
    print(0)
else:
    p = 0
    for i in range(5,len(s)+1):
        for j in range(len(s)-i+1):
            if(int(s[j:j+i])%2019 == 0):
                p += 1
    print(p)

### FIXED CODE ###

s = input()
if(len(s) < 5):
    print(0)
else:
    t = [0]*2019
    t[0] = 1
    p = 0
    temp = 0
    temp2 = 1
    for i in range(1,len(s)+1):
        temp2 *= 10
        temp2 %= 2019
        t[(temp + temp2*int(s[-i]))%2019] += 1
        temp += temp2*int(s[-i])
    for i in range(2019):
        p += (t[i]*(t[i]-1))//2
    print(p)
### BUGGY CODE ###

N,M=map(int,input().split())
nekotyann=[]
wanntyann=[]
ans="IMPOSSIBLE"
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        if b in wanntyann:
            ans="POSSIBLE"
            break
        else:
            nekotyann.append(b)
    if b==N:
        if a in nekotyann:
            ans="POSSIBLE"
            break
        wanntyann.append(a)
print(ans)


### FIXED CODE ###

N,M=map(int,input().split())
nekotyann=[0]*N
wanntyann=[0]*N
ans="IMPOSSIBLE"
for i in range(M):
    a,b=map(int,input().split())
    if a==1:
        nekotyann[b-1]+=1
    if b==N:
        wanntyann[a-1]+=1
for k in range(N):
    if nekotyann[k]!=0 and wanntyann[k]!=0:
        ans="POSSIBLE"
        break
print(ans)
### BUGGY CODE ###

from collections import deque
INF = 10**9

n, x, y = map(int, input().split())
x, y = x-1, y-1
ans = [0] * n

for sv in range(n):
  dist = [INF] * n
  q = deque()

  def push(v, d):
    if dist[v] != INF: return
    dist[v] = d
    q.append(v)

  push(sv, 0)

  while q:
    v = q.popleft()
    d = dist[v]
    if v-1 >= 0: push(v-1, d+1)
    if v+1 < n: push(v+1, d+1)
    if v == x: push(y, d+1)
    if v == y: push(x, d+1)

  for i in range(n):
    ans[dist[i]] += 1

for i in range(n):
  ans[i] //= 2

for i in range(1, n):
  print(ans[i])

### FIXED CODE ###

from collections import deque
INF = 10**9

n, x, y = map(int, input().split())
x, y = x-1, y-1
ans = [0] * n

for sv in range(n):
  dist = [INF] * n
  q = deque()

  def push(v, d):
    if dist[v] != INF: return
    dist[v] = d
    q.append(v)

  push(sv, 0)

  while q:
    v = q.popleft()
    d = dist[v]
    if v-1 >= 0: push(v-1, d+1)
    if v+1 < n: push(v+1, d+1)
    if v == x: push(y, d+1)
    if v == y: push(x, d+1)

  for i in range(n):
    ans[dist[i]] += 1

for i in range(n):
  ans[i] //= 2

for i in range(1, n):
  print(ans[i])
### BUGGY CODE ###

import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 2019

    S = input()[::-1]
    N = len(S)
    nums = [0] * N
    for i in range(N):
        nums[i] = int(S[i])
    if N < 4:
        print(0)
        return

    R = [0] * 2019
    R[0] = 1 # '' の分
    t = 0
    b = 1
    for i in range(N):
        t += nums[i] * b
        t %= 2019
        R[t] += 1
        b *= 10
    ans = 0
    for r in R:
        ans += r*(r-1)//2
    print(ans)
    




    

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 2019

    S = input()[::-1]
    N = len(S)

    if N < 4:
        print(0)
        return

    R = [0] * 2019
    R[0] = 1 # '' の分
    t = 0
    b = 1
    for c in S:
        t += int(c) * b
        t %= 2019
        R[t] += 1
        b *= 10
        b %= 2019
    ans = 0
    for r in R:
        ans += r*(r-1)//2
    print(ans)
    




    

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

n = int(input())
a = list(map(int,input().split()))
 
output = 1
if a[0] != 0:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, max(a)+1):
        output *= pow(a.count(i-1),a.count(i),998244353)
        output %= 998244353
    print(output)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
cnt = [0]*n
for i in a:
    cnt[i] += 1

output = 1
if a[0] != 0 or cnt[0] != 1:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, n):
        output *= cnt[i-1]**cnt[i]
        output %= 998244353
    print(output)
### BUGGY CODE ###

def is_ok(mid, gen=None, diff=0):
    print(mid, flush=True)
    res = input()
    if res == "Vacant":
        exit()
    if gen is None:
        return res
    if diff % 2 == 0:
        return gen == res
    else:
        return gen != res


def meguru_bisect_kai(ng, ok, gen):
    while abs(ok - ng) >= 2:
        mid = (ok + ng) // 2
        if is_ok(mid, gen, ok - mid):
            ok = mid
        else:
            ng = mid
    return (ok + ng) // 2


n = int(input())
gen = is_ok(n - 1)
ret = meguru_bisect_kai(0, n - 1, gen)
is_ok(ret)

### FIXED CODE ###

n = int(input())

print(0, flush=True)
res = input()
if res == "Vacant":
    exit()

left = 0
right = n
gen = res


while True:
    mid = (left + right) // 2
    print(mid, flush=True)
    res = input()

    if res == "Vacant":
        exit()

    if gen == res:
        if (mid - left) % 2:
            right = mid
        else:
            left = mid
    else:
        if (mid - left) % 2:
            left = mid
            gen = res
        else:
            right = mid
### BUGGY CODE ###

N,M = map(int, input().split())
LR = [list(map(int,input().split())) for _ in range(M)]
gate_list = []
ans = set(range(LR[0][0], LR[0][1]+1))
for i in range(M):
    gate_list = set(range(LR[i][0], LR[i][1]+1))
    ans = gate_list & ans
print(len(ans))

### FIXED CODE ###

N,M = map(int, input().split())
LR = [list(map(int,input().split())) for _ in range(M)]
sta = [LR[i][0] for i in range(M)]
gol = [LR[i][1] for i in range(M)]

ans = min(gol) -max(sta) 
print(ans+1 if ans >= 0 else 0)
### BUGGY CODE ###

s = input()
ls = list(map(int,list(s)))
lt = []
for i in range(len(s)-1):
  lt.append(ls[i]+ls[i+1])

ans = 0
while 1 in lt:
  flg = 1
  n = len(ls)
  for i in range(1,n):
    if flg == 1 and lt[n-i-1] == 1:
      ls.pop(n-i)
      ls.pop(n-i-1)
      ans += 2
      flg = 0
    elif flg == 0:
      flg = 1
  lt = []
  for i in range(len(ls)-1):
  	lt.append(ls[i]+ls[i+1])
print(ans)

### FIXED CODE ###

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

S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
dp[0][0] = 1
MOD = 10 ** 9 + 7
mul = 1
for i in range(l):
    if S[-(i+1)] == '?':
        for j in range(10):
            for k in range(13):
                dp[i+1][(j * mul + k) % 13] += dp[i][k]
                dp[i+1][(j * mul + k) % 13] %= MOD
    else:
        j = int(S[-(i+1)])
        for k in range(13):
            dp[i+1][(j * mul + k) % 13] += dp[i][k]
            dp[i+1][(j * mul + k) % 13] %= MOD
    mul = mul * 10 % 13
print(dp[l][5])

### FIXED CODE ###

S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
dp[0][0] = 1
MOD = 10 ** 9 + 7
mul = 1
for i in range(l):
    if S[-(i+1)] == '?':
        for j in range(10):
            for k in range(13):
                dp[i+1][(j * mul + k) % 13] += dp[i][k]
                dp[i+1][(j * mul + k) % 13] %= MOD
    else:
        j = int(S[-(i+1)])
        for k in range(13):
            dp[i+1][(j * mul + k) % 13] += dp[i][k]
            dp[i+1][(j * mul + k) % 13] %= MOD
    mul = mul * 10 % 13
print(dp[l][5])
### BUGGY CODE ###

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

def E(x):
    ans = 0
    for i in range(1,x+1):
        ans += (1/x)*i
    return ans
ans = 0
for i in range(len(P)):
    P[i] = round(E(P[i]),2)

max_v = tmp = sum(P[:k])
l = 0
r = k
for i in range(1,len(P)-k+1):
    tmp = tmp-P[i-1]+P[r]
    max_v = max(max_v, tmp)
    r += 1
print(max_v)

### FIXED CODE ###

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



def E(x):
    # ans = 0
    tmp = (x*(x+1))/2 if x % 2 == 0 else x+(((x-1)*(x))/2)
    return tmp/x
ans = 0
for i in range(len(P)):
    P[i] = round(E(P[i]),1)
max_v = tmp = sum(P[:k])
l = 0
r = k
for i in range(1,len(P)-k+1):
    tmp = tmp-P[i-1]+P[r]
    max_v = max(max_v, tmp)
    r += 1
print(max_v)
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
Q = int(input())
Bs = [[int(j) for j in input().split()] for i in range(Q)]
X = [0]
for i in range(1, 100001):
  X.append(A.count(i))
S = sum(A)
for B in Bs:
  S += X[B[0]]*(B[1]-B[0])
  X[B[1]] += X[B[0]]
  X[B[0]] = 0
  print(S)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
S = sum(A)
D = {c:0 for c in range(1, 10**5+1)}
for a in A:
  D[a] += 1
for _ in range(int(input())):
  B, C = map(int, input().split())
  S += (C-B)*D[B]
  D[C] += D[B]
  D[B] = 0
  print(S)
### BUGGY CODE ###

def BinarySearch(A, n, key):
    left = 0
    right = n

    while left < right:
        mid = (left+right)/2
	if A[mid] == key:
            return mid
	elif key < A[mid]:
            right = mid
	elif key > A[mid]:
            left = mid
    return "NOT_FOUND"


if __name__ == "__main__":
    n1 = int(raw_input())
    x1 = map(int, raw_input().split())
    n2 = int(raw_input())
    x2 = map(int, raw_input().split())

    C_num = 0

    for i in range(n2):
	if BinarySearch(x1, n1, x2[i]) != "NOT_FOUND":
            C_num += 1

    print(C_num)

### FIXED CODE ###

def BinarySearch(A, n, key):
    #left:left end index                                      
    #right:right end index + 1                                
    left = 0
    right = n

    while left < right:
        mid = (left+right)/2
        if A[mid] == key:
            return mid
        elif key < A[mid]:
            right = mid
        elif key > A[mid]:
            left = mid + 1

    return "NOT_FOUND"


if __name__ == "__main__":
    n1 = int(raw_input())
    x1 = map(int, raw_input().split())
    n2 = int(raw_input())
    x2 = map(int, raw_input().split())

    C_num = 0

    for i in range(n2):
	if BinarySearch(x1, n1, x2[i]) != "NOT_FOUND":
            C_num += 1

    print(C_num)
### BUGGY CODE ###

a,b,k=[int(i) for i in input().split()]
c=[]
for i in range(a,b+1):
    c.append(i)
head=c[:k]
tail=c[::-1][:k]
d=head+tail
e=list(set(d))
e.sort()
for i in e:
    print(i)

### FIXED CODE ###

a,b,k=[int(i) for i in input().split()]

if b-a+1>=2*k:
    for i in range(a,a+k):
        print(i)
    for i in range(b-k+1,b+1):
        print(i)
else:
    for i in range(a,b+1):
        print(i)
### BUGGY CODE ###

import math
N=int(input())
ans=list()
for i in range(1,N+1):
    tmp=0
    tgt=int(math.sqrt(i/2)+1)
    for j in range(1,tgt):
        for k in range(j,tgt):
            for h in range(k,tgt):
                if h**2+j**2+k**2+h*j+j*k+k*h==i:
                    if j==k==h:
                        tmp+=1
                    elif j==k or k ==h:
                        tmp+=3
                    else:
                        tmp+=6
    ans.append(tmp)
for i in ans:
    print(i)

### FIXED CODE ###

N=int(input())
a=[0]*10**4
for j in range(1,101):
    for k in range(1,101):
        for h in range(1,101):
            tmp=h**2+j**2+k**2+h*j+j*k+k*h
            if tmp<=10000:
                a[tmp-1]+=1
for i in range(N):
    print(a[i])
### BUGGY CODE ###

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

MOD = 998244353

dp = [[0] * (S + 1) for _ in range(N + 1)]
dp[0][0] = 1

for i in range(N):
    ai = A[i]
    for j in range(S + 1):
        here = dp[i][j]
        # aiがTに含まれない
        dp[i + 1][j] += here
        # aiがTに含まれるが, Uに含まれない
        dp[i + 1][j] += here

        dp[i + 1][j] %= MOD

        # aiがUに含まれる
        if j + ai <= S:
            dp[i + 1][j + ai] += dp[i][j]
            dp[i + 1][j + ai] %= MOD

print(dp[N][S])


### FIXED CODE ###

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

MOD = 998244353

dp = [[0] * (S + 1) for _ in range(N + 1)]
dp[0][0] = 1

for i in range(N):
    ai = A[i]
    for j in range(S + 1):
        here = dp[i][j]
        # aiがTに含まれない
        dp[i + 1][j] += here
        # aiがTに含まれるが, Uに含まれない
        dp[i + 1][j] += here

        dp[i + 1][j] %= MOD

        # aiがUに含まれる
        if j + ai <= S:
            dp[i + 1][j + ai] += dp[i][j]
            dp[i + 1][j + ai] %= MOD

print(dp[N][S])
### BUGGY CODE ###

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
  
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
  
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(n):
  p = a[i]*comb(n-1-i,k-1,mod)
  q = a[i]*comb(i,k-1,mod)
  if n-i < k:
    p = 0
  if i < k-1:
    q = 0
  #print(p,q)
  ans += q
  ans -= p
  ans %= mod
print(ans)

### FIXED CODE ###

def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)
### BUGGY CODE ###

from sys import stdin

if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    memo = set()
    cnt = 0
    for i in range(len(S)):
        for j in range(i+3,len(S)):
            if j == i+3:
                mod = int(S[len(S)-j-1:len(S)-i])%3
            else:
                mod = (mod+int(S[len(S)-j-1]))%3
            if mod == 0:
                _ = int(S[len(S)-j-1:len(S)-i])
                if _ in memo:
                    cnt += 1
                elif _%2019==0:
                    cnt += 1
                    memo.add(_)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


### FIXED CODE ###

from sys import stdin


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    len_S = len(S)
    cnt = 0
    for i in range(len_S):
        for j in range(i+3,len_S):
            if j == i+3:
                mod = int(S[len_S-j-1:len_S-i])%3
            else:
                mod = (mod+int(S[len_S-j-1]))%3
            if mod == 0:
                _ = int(S[len_S-j-1:len_S-i])
                if _%673==0:
                    cnt += 1
                    break
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


if __name__ == "__main__":
    #main()

    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    mod_arr = [0] * 2019
    mod_arr[0] += 1

    num, dec = 0, 1
    for s in S[::-1]:
        num += int(s)*dec
        num %= 2019
        dec *= 10
        dec %= 2019
        mod_arr[num] += 1

    for i in range(2019):
        if mod_arr[i] > 0:
            cnt += mod_arr[i]*(mod_arr[i]-1)//2
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
a.sort()

visited = [False] * (10**6+1)
ans = 0
for i in a:
    j = 1
    flg = True
    if a.count(i) >= 2:
        flg = False
    elif visited[i]:
        flg = False
    if flg:
        ans += 1        
    # print(i, flg)
    while i*j <= 10**6:
        if visited[i*j]:
            flg = False
        visited[i*j] = True
        j += 1
print(ans)

### FIXED CODE ###

import numpy as np

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

m = np.zeros(max(a)+1, dtype=int)
cnt = 0

for i in a:
    m[::i] += 1
for i in a:
    if m[i] == 1:
        cnt += 1
print(cnt)