Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.

Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.

Input

In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.

Output

Print the minimum value of $P$ in a line.

Constraints

$1 \leq n \leq 100,000$

$1 \leq k \leq 100,000$

$1 \leq w_i \leq 10,000$

Sample Input 1

5 3
8
1
7
3
9

Sample Output 1

10

If the first truck loads two packages of $\{8, 1\}$, the second truck loads two packages of $\{7, 3\}$ and the third truck loads a package of $\{9\}$, then the minimum value of the maximum load $P$ shall be 10.

Sample Input 2

4 2
1
2
2
6

Sample Output 2

6

If the first truck loads three packages of $\{1, 2, 2\}$ and the second truck loads a package of $\{6\}$, then the minimum value of the maximum load $P$ shall be 6.

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define TRUE (1)
#define FALSE (0)

/* DataType */
typedef uint32_t Weight_t;
typedef uint8_t Bool;
typedef struct tagConfigure_t Configure_t;
typedef struct tagSolver_t Solver_t;

struct tagConfigure_t {
	uint32_t truckMaxCount;
	uint32_t loadageMaxCount;
};

struct tagSolver_t {
	Weight_t* loadage;
	Weight_t  maxLoadingCapacity;
	Configure_t config;
};

/* Public Methods */
void     Solver_Initialize(Solver_t* self);
void     Solver_Free(Solver_t* self);
void     Solver_GetProblemConfigure(Solver_t* self);
void     Solver_Solve(Solver_t* self);
Weight_t Solver_GetMaxLoadingCapacity(Solver_t* self);

/* Private Methods */
static Weight_t Solver_Sum(Weight_t* weights, uint32_t count);
static Bool     Solver_CanLoaded(Weight_t* weights, uint32_t maxLoadingCapacity,
																 uint32_t loadageMaxCount, uint32_t truckMaxCount);

int main(void)
{
	Solver_t solver;

	Solver_Initialize(&solver);

	Solver_GetProblemConfigure(&solver);

	Solver_Solve(&solver);

	printf("%d\n", Solver_GetMaxLoadingCapacity(&solver));

	Solver_Free(&solver);
}

void Solver_Initialize(Solver_t* self)
{
	self->loadage                  = NULL;
	self->maxLoadingCapacity       = 0;
	(self->config).truckMaxCount   = 0;
	(self->config).loadageMaxCount = 0;
}

void Solver_GetProblemConfigure(Solver_t* self)
{
	char* numStr;
	char readLine[256];
	char buf[256];

	if (scanf("%255[^\n]%*[^\n]", readLine) != 1) {
		exit(0);
	}

	numStr = strtok(readLine, " ");
	(self->config).loadageMaxCount = atoi(numStr);
	
	numStr = strtok(NULL, " ");
	(self->config).truckMaxCount   = atoi(numStr);

	self->loadage = (Weight_t*)malloc(((self->config).loadageMaxCount)*sizeof(Weight_t));

	for (uint32_t i = 0;i < (self->config).loadageMaxCount;i++) {
		char*    weightStr;
		Weight_t weight;

		scanf("%*c", buf);

		if (scanf("%255[^\n]%*[^\n]", readLine) != 1) {
			exit(0);
		}

		weightStr = strtok(readLine, " ");
		weight    = atoi(weightStr);

		self->loadage[i] = weight;
	}
}

void Solver_Solve(Solver_t* self)
{
	Weight_t upperBound, lowerBound, middle;

	upperBound = Solver_Sum(self->loadage, (self->config).loadageMaxCount);
	lowerBound = Solver_Sum(self->loadage, (self->config).loadageMaxCount) / (self->config).truckMaxCount;

	while (upperBound > lowerBound) {
		middle = (upperBound + lowerBound) / 2;
		if (Solver_CanLoaded(self->loadage, middle, (self->config).loadageMaxCount,
												 (self->config).truckMaxCount)) {
			upperBound = middle;
		} else {
			lowerBound = middle + 1;
		}
	}

	self->maxLoadingCapacity = lowerBound;
}

void Solver_Free(Solver_t* self)
{
	free(self->loadage);
}

Weight_t Solver_GetMaxLoadingCapacity(Solver_t* self)
{
	return self->maxLoadingCapacity;
}

static Weight_t Solver_Sum(Weight_t* weights, uint32_t count)
{
	Weight_t sum = 0;

	for (uint32_t i=0;i < count;i++) {
		sum += weights[i];
	}

	return sum;
}

static Bool Solver_CanLoaded(Weight_t* weights, uint32_t maxLoadingCapacity, 
														 uint32_t loadageMaxCount, uint32_t truckMaxCount)
{
	Weight_t weightsPerTruck = 0;
	uint32_t numOfTrucks = 0;

	for (uint32_t i = 0;i < loadageMaxCount;i++) {
		weightsPerTruck += weights[i];

		if (weightsPerTruck > maxLoadingCapacity) {
			numOfTrucks++;
			weightsPerTruck = weights[i];
		}

		if (numOfTrucks == truckMaxCount || weightsPerTruck > maxLoadingCapacity) {
			return FALSE;
		}
	}

	return TRUE;
}