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≤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≤k,n≤100000 )
The second line has n - 1 integers, the distance of adjacent domino d, 1≤d≤100000
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≤k,n≤100000 )
The second line has n - 1 integers, the distance of adjacent domino d, 1≤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
Sample Output
9
可以想到,对于一堆牌,从左到右推和从右到左推是一样的,所以只要考虑都向右即可,然后一次机会,可以减去一个最长的跨度。
所以只要排个序,把小的加起来就好了
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m, a[N];
LL ans;
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
m = min(n, m);
ans = 0;
rep(i, 1, n - 1) scanf("%d", &a[i]);
sort(a + 1, a + n);
rep(i, 1, n - m) ans += a[i] + 1;
printf("%I64d\n", ans + m);
}
return 0;
}