Problem:


Problem E:   カントリーロード

  

    ある過疎地域での話である．
    この地域ではカントリーロードと呼ばれるまっすぐな道に沿って，
    家がまばらに建っている．
    今までこの地域には電気が通っていなかったのだが，
    今回政府からいくつかの発電機が与えられることになった．
    発電機は好きなところに設置できるが，
    家に電気が供給されるにはどれかの発電機に電線を介してつながっていなければならず，
    電線には長さに比例するコストが発生する．
    地域で唯一の技術系公務員であるあなたの仕事は，
    すべての家に電気が供給されるという条件の下で，
    できるだけ電線の長さの総計が短くなるような発電機
    および電線の配置を求めることである．
    なお，発電機の容量は十分に大きいので，
    いくらでも多くの家に電気を供給することができるものとする．
  

  

    サンプル入力の1番目のデータセットを図2に示す．
    この問題に対する最適な配置を与えるには，
    図のように x = 20 と x = 80 の位置に発電機を配置し，
    それぞれ図中のグレーで示した位置に電線を引けばよい．
  

    

	

	  
	    
	  
	

	

	  図2: 発電機と電線の配置の例（入力例の最初のデータセット）．
	

    

  
Input

  

    入力の1行目にはデータセットの個数 t
    (0 < t ≤ 50) が与えられる．
  

  

    引き続き t 個のデータセットが与えられる．
    データセットはそれぞれ次のような形式で2行で与えられる．
  

  

    n k
    x1 x2 ... xn
  

  

    n は家の戸数，k は発電機の個数である．
    x1, x2, ...,
    xn
    はそれぞれ家の位置を表す一次元座標である．
    これらの値はすべて整数であり，
    0 < n ≤ 100000,
    0 < k ≤ 100000,
    0 ≤ x1 < x2 <
    ... < xn ≤ 1000000
    を満たす．
  

  

    さらに，データセットのうち 90% は，
    0 < n ≤ 100, 0 < k ≤ 100 を満たしている．
  

  
Output

  

    各データセットに対し，
    必要な電線の長さの総計の最小値を1行に出力せよ．
  

  
Sample Input

  

6
5 2
10 30 40 70 100
7 3
3 6 10 17 21 26 28
1 1
100
2 1
0 1000000
3 5
30 70 150
6 4
0 10 20 30 40 50

  
Output for the Sample Input

  

60
13
0
1000000
0
20



Sample input:
6
5 2
10 30 40 70 100
7 3
3 6 10 17 21 26 28
1 1
100
2 1
0 1000000
3 5
30 70 150
6 4
0 10 20 30 40 50


Sample output:
60
13
0
1000000
0
20

Submission status: Wrong Answer

Solution code:
def f(n,k,x):
    dis = [x[i+1]-x[i] for i in range(n-1)]#隣り合う家の距離を調べる
    dis.sort()#電線の数-1だけ隣り合う家同士の電線を配置しなくて良い
    return sum(dis[:n-k])#隣り合う家の距離が大きい順にkー1個は電線を配置しない
    
t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    print(f(n,k,x))

Submission status: Wrong Answer

Solution code:
def f(n,k,x):
    dis = [x[i+1]-x[i] for i in range(n-1)]
    dis.sort()
    return sum(dis[:n-k])
    
t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    print(f(n,k,x))

Submission status: Wrong Answer

Solution code:
t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    dis = [x[i+1]-x[i] for i in range(n-1)]
    dis.sort()
    ans = sum(dis[:n-k])
    print(ans)

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

Solution code:
t = int(raw_input())

for i in xrange(t):
    n, k = [int(_) for _ in raw_input().split()]

    x = [int(_) for _ in raw_input().split()]
    d = [x[j + 1] - x[j] for j in xrange(n - 1)]
    d.sort(reverse=True)
    print(x[-1] - x[0] - sum(d[:k]))

Submission status: Time Limit Exceeded

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
    if N < K:
        K = N
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = Z - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Time Limit Exceeded

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
    if N < K:
        K = N
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = Z - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104
"""
import sys
from sys import stdin
input = stdin.readline


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

        if k >= n:              #  ????????¨?????????????????¨?????§??????
            print(0)
            continue

        diff = []
        prev = houses[0]
        for h in houses[1:]:
            diff.append(h-prev)
            prev = h

        points = []
        for x in range(k-1):
            pos = diff.index(max(diff))
            points.append(pos)
            diff[pos] = -1


        cable_total = 0
        prev = 0
        for p in points:
            cable_total += houses[p] - houses[prev]
            prev = p + 1
        if prev != (n - 1):
            cable_total += houses[-1] - houses[prev]

        print(cable_total)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104
TLE
"""
import sys
from sys import stdin
input = stdin.readline


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

        if k >= n:              #  ????????¨?????????????????¨?????§??????
            print(0)
            continue

        diff = []
        prev = houses[0]
        for h in houses[1:]:
            temp = h - prev
            diff.append(temp)
            prev = h



        points = []
        s_diff = sorted(diff)
        for x in range(k-1):
            pos = diff.index(s_diff.pop())
            points.append(pos)
            diff[pos] = -1


        cable_total = 0
        prev = 0
        for p in points:
            cable_total += houses[p] - houses[prev]
            prev = p + 1
        if prev != (n - 1):
            cable_total += houses[-1] - houses[prev]

        print(cable_total)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
t = int(input())
for i in range(t):
    n,k = map(int,input().strip().split(' '))#入力
    x = []
    x = list(map(int,input().strip().split(' ')))
    x.sort()

    I = []
    for j in range(n-1):
        I.append(x[j+1]-x[j])
    I.sort()
    # print(I)

    L = x[n-1] - x[0]
    if k != 0:
        if k <= n:
            for j in range(k-1):
                L = L - I[n-2-j]
                del I[n-2-j]
        else:
            L = 0
    print(L)

Submission status: Accepted

Solution code:
t = int(input())
for t in range(t):
    N,K = map(int, input().strip().split(" "))
    X = list(map(int,input().strip().split(" ")))
    L = [X[i+1] - X[i] for i in range(0,N-1)]
    L.sort()
    s = 0
    for i in range(0,N-K):
        s = s + L[i]
    print(s)

Submission status: Accepted

Solution code:
t = int(input()) 
for t in range(t):
    N,K = map(int, input().strip().split(" "))
    X = list(map(int,input().strip().split(" ")))
    L = [X[i+1] - X[i] for i in range(0,N-1)]
    L.sort()
    s = 0
    for i in range(0,N-K):
        s = s + L[i]
    print(s)

Submission status: Accepted

Solution code:

# coding: utf-8

# In[23]:


#ｎは戸数
#ｋは発電機の個数
#ｘは家の座標

t = int(input()) 
for t in range(t):
    N,K = map(int, input().strip().split(" "))
    X = list(map(int,input().strip().split(" ")))
    L = [X[i+1] - X[i] for i in range(0,N-1)]
    L.sort()
    s = 0
    for i in range(0,N-K):
        s = s + L[i]
    print(s)
    
#Country Road
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104&lang=jp
#右中央

Submission status: Accepted

Solution code:
t=int(input())
for j in range(t):
  n,k=map(int,input().split())
  l=list(map(int,input().split()))
  l1=[]
  for i in range(n-1):
      l1.append(l[i+1]-l[i])
  l1=sorted(l1)
  if n<=k:
    print(0)
  else:
    for i in range(k-1):
      del l1[-1]
    s=0
    for i in l1:
        s+=i
    print(s)

Submission status: Accepted

Solution code:
q = int(input())
for i in range(q):
    n,k = map(int,input().split())
    s = list(map(int,input().split()))
    sa = [0]
    for j in range(1,n):
      sa.append(s[j]-s[j-1]) 

    sa.sort(reverse = True)
    print(sum(sa[k-1:]))

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
    n,k = map(int,input().split())
    L = list(map(int,input().split()))
    if n <= k:
        print(0)
    else:
        A = []
        for j in range(1,n):
            A.append(L[j]-L[j-1])
        A.sort()
        A.reverse()
        S = L[n-1]-L[0]
        for l in range(k-1):
            S -= A[l]
        print(S)

Submission status: Accepted

Solution code:
def croad():
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    y = []
    for i in range(n-1):
        y.append(x[i+1]-x[i])
    y.sort()
    y.reverse()
    while k>1 and len(y)>0:
        y.pop(0)
        k -= 1
    print(sum(y))

a = int(input())
for i in range(a):
    croad()

Submission status: Accepted

Solution code:
def croad():
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    y = []
    for i in range(n-1):
        y.append(x[i+1]-x[i])
    y.sort()
    y.reverse()
    while k>1 and len(y)>0:
        y.pop(0)
        k -= 1
    print(sum(y))

a = int(input())
for i in range(a):
    croad()

Submission status: Accepted

Solution code:
t = int(input())
for _ in range(t):
    n,k = map(int,input().split())
    houses=list(map(int,input().split()))
    if n<=k:
        print(0)
        continue
    sa = [houses[i+1] - houses[i] for i in range(n-1)]
    sa.sort()
    res = houses[-1] - houses[0]
    for i in range(k-1):
        res -= sa.pop()
    print(res)

Submission status: Accepted

Solution code:
def f(n,k,x):
    if n <= k-1:
        return 0
    dis = [x[i+1]-x[i] for i in range(n-1)]#隣り合う家の距離を調べる
    dis.sort()#電線の数-1だけ隣り合う家同士の電線を配置しなくて良い
    return sum(dis[:n-k])#隣り合う家の距離が大きい順にkー1個は電線を配置しない
    
t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    print(f(n,k,x))

Submission status: Accepted

Solution code:
#カントリーロード
t=int(input())
for i in range(t):
    N,K=map(int, input().split())
    X=list(map(int,input().strip().split()))
    a=[0]
    for j in range(N-1):
        a.append(X[j+1]-X[j])
    b=sorted(a, reverse=True)
    if K>1:
        del b[0:K-1]

    print(sum(b))

Submission status: Accepted

Solution code:
trial = int(input())
for t in range(trial):
    housecnt,elec = (int(n) for n in input().split(" "))
    house = [int(n) for n in input().split(" ")]
    maxlist = []
    for h in range(housecnt-1):
        maxlist.append(house[h+1]-house[h])
    maxlist.sort()
    for p in range(min(housecnt-1,elec-1)):
        maxlist.pop()
    print(sum(maxlist))

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    rs = sorted([xs[i + 1] - xs[i]for i in range(n - 1)])
    if k >= n:
        print(0)
    else:
        print(sum(rs[:n - k]))

Submission status: Accepted

Solution code:
t=int(raw_input())
for tt in range(t):
    n,k=map(int,raw_input().split())
    k=min(n,k)
    A=map(int,raw_input().split())
    B=[]
    for i in range(1,n):
        B.append(A[i]-A[i-1])
    B.sort()
    for i in range(1,k):
        B[-i]=0
    print sum(B)

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    a,b=map(int,input().split())
    c=list(map(int,input().split()))
    c=sorted([c[i+1]-c[i] for i in range(a-1)])
    print([0,sum(c[:a-b])][b<a])

Submission status: Accepted

Solution code:
t=int(input())
for i in range(t):
        n,k=[int(j) for j in input().split(" ")]
        x=[int(j) for j in input().split(" ")]
        if n<=k:
                print(0)
        else:
                distance=x[-1]-x[0]
                dis_list=[]
                for j in range(n-1):
                        dis_list.append(x[j+1]-x[j])
                dis_list.sort()
                dis_list.reverse()
                for j in range(k-1):
                        distance-=dis_list[j]
                print(distance)

Submission status: Accepted

Solution code:
t = int(raw_input())

for i in xrange(t):
    n, k = [int(_) for _ in raw_input().split()]

    x = [int(_) for _ in raw_input().split()]
    d = [x[j + 1] - x[j] for j in xrange(n - 1)]
    d.sort(reverse=True)
    print(x[-1] - x[0] - sum(d[:k - 1]))

Submission status: Accepted

Solution code:
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    position_array = list(map(int, input().split()))

    # ??????????????????????????£????£?????????¢??????????????????
    distance_array = []
    for j in range(n-1):
        distance_array.append(position_array[j+1] - position_array[j])

    # ??????????????????
    distance_array.sort(reverse=True)
    print(sum(distance_array[k-1:])) # ?????§??????k-1????????????????¨?

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104

"""
import sys
from sys import stdin
from bisect import bisect_left
input = stdin.readline


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

        if k >= n:              #  ????????¨?????????????????¨?????§??????
            print(0)
            continue


        diff = []
        prev = houses[0]
        for i, h in enumerate(houses[1:]):
            diff.append((h - prev, i))
            prev = h


        diff.sort(reverse=True)
        points = diff[:k-1]


        cable_total = 0
        prev = 0
        for _, p in points:
            cable_total += houses[p] - houses[prev]
            prev = p + 1
        if prev != (n - 1):
            cable_total += houses[-1] - houses[prev]

        print(cable_total)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2104
AC
"""
import sys
from sys import stdin
input = stdin.readline


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

        if k >= n:              #  ????????¨?????????????????¨?????§????????´???
            print(0)
            continue

        # ??¶??????????????¢????¨????????????????????????¨????????????????????????
        gaps = []
        prev = houses[0]
        for i, h in enumerate(houses[1:]):
            gaps.append((h - prev, i))
            prev = h

        # gap?????§????????¨??????????????????????????????????????´????????????
        gaps.sort(reverse=True)
        gaps = gaps[:k-1]


        # ???????????????????????±????????????????¨??????????
        cable_total = 0
        prev = 0
        for _, p in gaps:
            cable_total += houses[p] - houses[prev]
            prev = p + 1
        if prev != (n - 1):
            cable_total += houses[-1] - houses[prev]

        print(cable_total)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    houseNum, genNum = map(int,input().split())
    location = list(map(int, input().split()))
    distance = []
    for j in range(1, houseNum):
        distance.append(location[j] - location[j-1])
    distance.sort(reverse=True)
    print(location[-1] - location[0] - sum(distance[:genNum-1]))

Submission status: Accepted

Solution code:
import sys

file_input = sys.stdin

t = int(file_input.readline())

for i in range(t):
    n, k = map(int, file_input.readline().split())
    if n <= k:
        file_input.readline()
        print(0)
    else:
        x_list = list(map(int, file_input.readline().split()))            
        distance = [d2 - d1 for d1, d2 in zip(x_list, x_list[1:])]
        if k == 1:
            print(sum(distance))
        else:
            distance.sort()
            print(sum(distance[:1-k]))

Submission status: Accepted

Solution code:

def cr():
    #データの読み込み
    NK=input().split()

    n=int(NK[0])
    k=int(NK[1])


    x=list(map(int,input().split()))
    distance=[]

    for i in range(len(x)-1):
        distance.append(x[i+1]-x[i])

    distance.sort()

    if n>k:
        for i in range(k-1):
            distance.pop()

        sum=0
        for i in range(n-k):
            sum+=distance[i]

        print(sum)

    else:
        print(0)

S=int(input())


for i in range(S):
    cr()

Submission status: Accepted

Solution code:
t=int(input())

for count in range(t):
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    
    diff=[] 
    if n<=k:
        print(0)
        continue
    
    for i in range(n-1):
        diff.append(x[i+1]-x[i])
        
    diff.sort()
    ans=0
    for i in range(max(0,n-k)):
        ans+=diff[i]
    
    print(ans)

Submission status: Accepted

Solution code:
t = int(input())
for _ in range(t):
    n, k = map(int, input().split())
    x = [int(y) for y in input().split()]
    d = []
    for i in range(n-1):
        d.append(x[i+1] - x[i])
    d.sort(reverse=True)
    ans = x[n-1] - x[0] - sum(d[:k-1])
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 # 宇都宮涼
 # J5-170029

T = int(input())
for i in range(T):
    n,k = map(int, input().strip().split(' '))
    a = list(input().split(' '))
    if n <= k:
        print(0)
    else:
        for m in range(len(a)):
            a[m] = int(a[m])
        b = []
        for j in range(len(a)-1):
            c = a[j+1]-a[j]
            b.append(c)
        b.sort()
        answer = sum(b)
        for l in range(k-1):
            answer = answer - b[len(b)-l-1]
        print(answer)

Submission status: Accepted

Solution code:
for i in range(int(input())):
    t, k = map(int, input().split())
    d=list(map(int,input().split()))
    d=sorted([d[i+1]-d[i] for i in range(t-1)])
    print(sum(d[:t-k]) if t>k else 0)

Submission status: Accepted

Solution code:


NUM=int(input())

for sect in range(NUM):
    n,k=list(map(int,input().strip().split()))
    x=list(map(int,input().strip().split()))
    x.sort()
    diff=[]
    
    for i in range(1,len(x)):
        diff.append(x[i]-x[i-1])
    diff.sort(reverse=True)

    s=sum(diff[:k-1])

    print(max(x)-min(x)-s)

Submission status: Accepted

Solution code:
for t in range(int(input())):
    n, k = map(int, input().split())
    X = list(map(int, input().split()))
    mid = [X[i] - X[i - 1] for i in range(1, n)]
    mid.sort()
    ans = [0, sum(mid[:n-k])][k<n]
    print(ans)

Submission status: Accepted

Solution code:
t = int(input())
for i in range(t):
    n, k  = map(int, input().split()) # n = len(xlist)
    xlist = list(map(int, input().split()))
    if (n <= k):
        print(0)
    else:
        betweenx = [xlist[i+1] - xlist[i] for i in range(n-1)]
        betweenx.sort()
        print(sum(betweenx[:n-k]))

Submission status: Accepted

Solution code:
t = int(input())
for _ in range(t):
  n, k = map(int, input().split())
  xlst = list(map(int, input().split()))
  diff = [j - i for i, j in zip(xlst, xlst[1:])]
  diff.sort(reverse=True)
  print(xlst[-1] - xlst[0] - sum(diff[:min(n - 1, k - 1)]))

Submission status: Accepted

Solution code:
for _ in range(input()):
	n,k = map(int,raw_input().split())
	x = map(int,raw_input().split())
	d = [x[i+1]-x[i] for i in range(n-1)]
	print x[-1] - x[0] - sum(sorted(d)[::-1][:k-1])

Submission status: Accepted

Solution code:
#演習２－３
a=int(input())

for i in range(a):
        n,k=[int(j) for j in input().split()]
        x=[int(j) for j in input().split()]
        if n<=k:
                print(0)
        else:
                distance=x[-1]-x[0]
                dist_list=[]
                for j in range(n-1):
                        dist_list.append(x[j+1]-x[j])
                dist_list.sort()
                dist_list.reverse()
                for j in range(k-1):
                        distance-=dist_list[j]
                print(distance)

Submission status: Accepted

Solution code:
for i in range(int(input())):
    n,k=map(int,input().split())
    l=[int(i) for i in input().split()]
    dif=sorted([l[i+1]-l[i] for i in range(n-1)])
    if k==1:
        print(sum(dif))
    else:
        print(max(0,sum(dif[:-k+1])))

Submission status: Accepted

Solution code:
t=int(input())
for k in range(t):
    n,k=map(int,input().split())
    house=list(map(int,input().split()))
    div=[]
    for i in range(n-1):
        div.append(house[i+1]-house[i])
    div.sort()
    answer=sum(div[:n-k])
    if(n>k):
        print(answer)
    else:
        print(0)

Submission status: Accepted

Solution code:
c = int(input())
def solve(n,k,x):
    diff = []
    for i in range(n-1):
        diff.append(x[i+1]-x[i])
    diff.sort()
    ans  = x[-1] - x[0]
    for i in range(min(k-1,len(diff))):
        ans -= diff.pop()
    return print(ans)
for j in range(c):
    n, k = map(int,input().split())
    x = list(map(int,input().split()))
    solve(n,k,x)

Submission status: Accepted

Solution code:
def solve(n,k,x):
  d = [x[i+1]-x[i] for i in range(n-1)]
  d.sort(key=lambda e: -e)
  if k >= n:
    s = 0
  else:
    s = sum(d[k-1:n])
  return s
hoge = int(input())
for j in range (hoge):
  n,k = map(int, input().split( ))
  x = list(map(int, input().split( )))
  print(solve(n,k,x))

Submission status: Accepted

Solution code:
t=int(input())
for _ in range(0,t):
    n,k= map(int,input().strip().split())
    x= list(map(int,input().split()))
    y=[]
    for i in range(1,n):
        y.append(x[i]-x[i-1])
    y.sort()
    count=0
    if k<n :
        for i in range(0,n-k):
            count+=y[i]
    else:
        count=0
    print(count)

Submission status: Accepted

Solution code:
t = int(input())
for i in range(t):
    n, k = map(int, input().split())
    X = list(map(int, input().split()))
    li = []
    for j in range(n-1):
        li.append(X[j+1]-X[j])
    li.sort(reverse=True)
    print(sum(li[(k-1):]))

Submission status: Accepted

Solution code:
t = int(input())

ans = [0 for i in range(t)]

for _ in range(t):
    n, k = map(int, input().split())
    
    li = list(map(int, input().split()))

    if n <= k:
        continue
    dis = []
    for i in range(n-1):
        dis.append(li[i+1]-li[i])
    dis.sort()
    ans[_] = (sum(dis[:n-k]))

for a in ans:
    print(a)

Submission status: Accepted

Solution code:

num = int(input())
for j in range(num):
    house,gen = map(int,input().split())
    
    point = list(map(int,input().split()))
    
    if house ==1 and gen >=1:
        print(0)
    elif house == 2 and gen == 1:
        print(point[-1])
    elif house == 2 and gen >= 2:
        print(0)
    elif house <= gen:
        print(0)
    elif house >=3 and gen ==1:
        print(max(point)-min(point))
    
    elif house >= 3:

        length = []
        for i in range(1,house):
            length.append(point[i]-point[i-1])
        length.sort()

        print(sum(length[:-1*(gen-1)]))

Submission status: Accepted

Solution code:
def cuntry(N,K,X):
    if N <= K:
        return 0
    XX=[]
    for a in range(1,N):
        XX.append(X[a]-X[a-1])
    XX.sort(reverse=True)
    del XX[0:K-1]
    return sum(XX)
n= int(input())  
for j in range(n):
    N, K = map(int,input().strip().split())
    X = list(map(int,input().strip().split()))
    if N==0:
        break;
    print(cuntry(N,K,X))

Submission status: Accepted

Solution code:
def solve(X, N, K):
    if N <= K:
        return 0

    output = []
    for i in range(1, N):
        output.append(X[i] - X[i-1])
    output.sort(reverse=True)
    del output[0:K-1]
    return sum(output)

n = int(input())
for j in range(n):
    N, K = map(int,input().strip().split())
    X = list(map(int, input().strip().split()))
    if N==0:
        break;
    print(solve(X, N, K))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;

int x[1000000];
int distance[1000000];

int main() {
  int all_length;
  cin >> n >> k;
  for (int i=0; i<n; ++i) {
    cin >> x[i];
  }
  if (n==1) {
    cout << 0;
  }
  else {
    all_length = x[n-1] - x[0];
    for (int i=0; i<n-1; ++i) {
      distance[i] = x[i+1] - x[i];
    }
    sort(distance, distance+n);
    for (int i=0; i<k-1; ++i) {
      all_length = all_length - distance[n-1-i];
    }
  }
  cout << all_length;
}

Submission status: Runtime Error

Solution code:
while True:
    N,K = map(int, input().strip().split(" "))
    X = list(map(int,input().strip().split(" ")))
    L = [X[i+1] - X[i] for i in range(0,N-1)]
    L.sort()
    print(L)
    s = 0
    for i in range(0,N-K):
        s = s + L[i]
    print(s)

Submission status: Runtime Error

Solution code:
while True:
    N,K = map(int, input().strip().split(" "))
    X = list(map(int,input().strip().split(" ")))
    L = [X[i+1] - X[i] for i in range(0,N-1)]
    L.sort()
    s = 0
    for i in range(0,N-K):
        s = s + L[i]
    print(s)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
l1=[]
for i in range(n-1):
    l1.append(l[i+1]-l[i])
l1=sorted(l1)
for i in range(k-1):
    del l1[-1]
s=0
for i in l1:
    s+=i
print(s)

Submission status: Runtime Error

Solution code:
t=int(input())
for j in range(t):
  n,k=map(int,input().split())
  l=list(map(int,input().split()))
  l1=[]
  for i in range(n-1):
      l1.append(l[i+1]-l[i])
  l1=sorted(l1)
  for i in range(k-1):
    del l1[-1]
  s=0
  for i in l1:
      s+=i
  print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
for i in range(N):
    n,k = map(int,input().split())
    L = list(map(int,input().split()))
    if n <= k:
        print(0)
    else:
        A = []
        for j in range(1,N):
            A.append(L[i]-L[i-1])
        A.sort()
        A.reverse()
        S = L[N-1]-L[0]
        for l in range(k-1):
            S -= A[l]
        print(S)

Submission status: Runtime Error

Solution code:
N = int(input())
for i in range(N):
    n,k = map(int,input().split())
    L = list(map(int,input().split()))
    if n <= k:
        print(0)
    else:
        A = []
        for j in range(1,n):
            A.append(L[i]-L[i-1])
        A.sort()
        A.reverse()
        S = L[n-1]-L[0]
        for l in range(k-1):
            S -= A[l]
        print(S)

Submission status: Runtime Error

Solution code:
def croad():
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    y = []
    for i in range(n-1):
        y.append(x[i+1]-x[i])
    y.sort()
    y.reverse()
    while k>1:
        y.pop(0)
        k -= 1
    print(sum(y))

a = int(input())
for i in range(a):
    croad()

Submission status: Runtime Error

Solution code:
def croad():
    n,k = map(int,input().split())
	if n == 1:
	    print(0)
	else:
        x = list(map(int,input().split()))
        y = []
        for i in range(n-1):
                y.append(x[i+1]-x[i])
        y.sort()
        y.reverse()
        while k>1 and len(y)>0:
            y.pop(0)
            k -= 1
        print(sum(y))

a = int(input())
for i in range(a):
    croad()

Submission status: Runtime Error

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
   
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = a - Y[N-1-i-1]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
   
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = a - Y[N-1-i]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
N,K=map(int, input().strip().split())
X=list(map(int, input().strip().split()))
X.sort(reverse=True)
print(X)
Y=[]
for i in range(len(X)-1):
    Y.append(X[i]-X[i+1])
Y.sort(reverse=True)
del Y[:K-1]
print(Y)
a=0
for i in range(len(Y)):
    a=a+Y[i]
print(a)

Submission status: Runtime Error

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
   
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = a - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
   
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = Z - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
o = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
    if N =< K:
        K = N
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = Z - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
def f(n,k,x):
    dis = [x[i+1]-x[i] for i in range(n-1)]#隣り合う家の距離を調べる
    dis.sort()#電線の数-1だけ隣り合う家同士の電線を配置しなくて良い
    return sum(dis[:n-k])#隣り合う家の距離が大きい順にkー1個は電線を配置しない
    
t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    x = list(map(int,input().split()))
    print(ans)

Submission status: Runtime Error

Solution code:
 = int(input())
x = 0
while x < o+1: #最初の数字は集合のの数
    N,K = map(int,input().strip().split(' '))
    X = list(map(int,input().strip().split(' ')))#リストを作る
    if N < K:
        K = N
    for i in range(N):
        X.sort()
        a = X[N-1] - X[0] #全体の長さ
        Y = [0]*(N-1)     #各家の間の距離の集合
        for j in range(N-1):
            Y[j] = X[j+1] - X[j]
        Y.sort()
    Z = a
    for i in range(K-1):  #一番遠い（大きい）のをK-1回外す
        Z = Z - Y[N-1-1-i]
    print(Z)
    x = x+1

Submission status: Runtime Error

Solution code:
trial = int(input())
for t in range(trial):
    housecnt,elec = (int(n) for n in input().split(" "))
    house = [int(n) for n in input().split(" ")]
    maxlist = []
    for h in range(housecnt-1):
        maxlist.append(house[h+1]-house[h])
    for p in range(elec-1):
        maxlist.pop()
    print(sum(maxlist))

Submission status: Runtime Error

Solution code:
trial = int(input())
for t in range(trial):
    housecnt,elec = (int(n) for n in input().split(" "))
    house = [int(n) for n in input().split(" ")]
    maxlist = []
    for h in range(housecnt-1):
        maxlist.append(house[h+1]-house[h])
    maxlist.sort()
    for p in range(min(housecnt,elec-1)):
        maxlist.pop()
    print(sum(maxlist))