# GitBook

domino

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 131072/131072 K (Java/Others) Problem Description

Little White plays a game.There are n pieces of dominoes on the table in a row. He can choose a domino which hasn’t fall down for at most k times, let it fall to the left or right. When a domino is toppled, it will knock down the erect domino. On the assumption that all of the tiles are fallen in the end, he can set the height of all dominoes, but he wants to minimize the sum of all dominoes height. The height of every domino is an integer and at least 1.

Input

The first line of input is an integer T ( 1 \leq T \leq 101≤T≤10) There are two lines of each test case. The first line has two integer n and k, respectively domino number and the number of opportunities.( 2\leq k, n \leq 1000002≤k,n≤100000) The second line has n – 1 integers, the distance of adjacent domino d, 1 \leq d \leq 1000001≤d≤100000

Output

For each testcase, output of a line, the smallest sum of all dominoes height

Sample Input

1 4 2 2 3 4 1 2 3 1 4 2 2 3 4 Sample Output

9 1 9 中文题意：

PS：注意数据范围 int 存不下sum

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b) {
return a>b;
}
__int64 a[100200];
int main() {
int T;
scanf("%d",&T);
while(T--) {
int n,k;
scanf("%d %d",&n,&k);
n--;
__int64 sum=0;
for(int i=0; i<n; i++) {
scanf("%I64d",&a[i]);
sum+=a[i]+1;
}
sum++;
sort(a,a+n,cmp);
for(int i=0; i<k-1&&i<n; i++)
sum=sum-a[i];
printf("%I64d\n",sum);
}
return 0;
}