Submission #13515

#TimeUsernameProblemLanguageResultExecution timeMemory
13515tncks0121K blocks (IZhO14_blocks)C++14
100 / 100
247 ms43448 KiB
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h> 
#include <time.h>

using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;

const int N_ = 100500;
const int K_ = 105;
int N, K, A[N_];

int tb[K_][N_];
stack<pii> stk;

int main() {
	scanf("%d%d", &N, &K);
	for (int i = 1; i <= N; i++) scanf("%d", A + i);

	for (int i = 1; i <= N; i++) tb[0][i] = (int)2e8;
	for (int k = 1; k <= K; k++) {
		while(!stk.empty()) stk.pop();

		tb[k][k] = tb[k - 1][k - 1] + A[k];
		stk.push(pii(A[k], tb[k - 1][k - 1]));

		for (int i = k+1; i <= N; i++) {
			int c = tb[k - 1][i - 1];
			while (!stk.empty() && stk.top().first <= A[i]) {
				c = min(c, stk.top().second);
				stk.pop();
			}
			if (stk.empty() || stk.top().first + stk.top().second > A[i] + c)
				stk.push(pii(A[i], c));
			
			tb[k][i] = stk.top().first + stk.top().second;
		}
	}

	printf("%d\n", tb[K][N]);
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...