## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Chopping Carrots (Hard Version) Codeforces Solution

D2. Chopping Carrots (Hard Version)
time limit per test
4 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The only difference between the versions is the constraints on $n$$k$${a}_{i}$, and the sum of $n$ over all test cases. You can make hacks only if both versions of the problem are solved.

Note the unusual memory limit.

You are given an array of integers ${a}_{1},{a}_{2},\dots ,{a}_{n}$ of length $n$, and an integer $k$.

The cost of

Input

The first line contains a single integer $t$ ($1\le t\le 100$) — the number of test cases.

The first line of each test case contains two integers $n$ and $k$ ($1\le n,k\le {10}^{5}$).

The second line

Another String Minimization Problem Codeforces Solution

Making Towers Codeforces Solution

Qpwoeirut And The City Codeforces Solution

Chopping Carrots (Easy Version) Codeforces Solution

Chopping Carrots (Hard Version) Codeforces Solution

contains $n$ integers ${a}_{1},{a}_{2},\dots ,{a}_{n}$ ($1\le {a}_{1}\le {a}_{2}\le \dots \le {a}_{n}\le {10}^{5}$).

It is guaranteed that the sum of $n$ over all test cases does not exceed ${10}^{5}$.

Output

For each test case, print a single integer — the minimum possible cost of an array $p$ satisfying the condition above.