Submission #1125937

#TimeUsernameProblemLanguageResultExecution timeMemory
1125937ChinguunK blocks (IZhO14_blocks)C++20
0 / 100
1 ms584 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define pb push_back
#define meta int tm = (tl + tr) / 2, x = i * 2 + 1, y = x + 1

const int N = 2e5 + 7;
const int oo = 1e9;
const int mod = 1e9 + 7;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

int n, k, a[N], mn[N][107], dp[N][107], s;
stack <pii> st;
int p[N];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];

	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= k; j++) {
			dp[i][j] = oo;
			mn[i][j] = oo;
		}
	}
	for (int i = 0; i <= n; i++) mn[i][0] = 0;
	for (int i = 0; i <= k; i++) mn[0][i] = 0;
	dp[0][0] = 0;
	
	for (int i = 1; i <= n; i++) {
		while (!st.empty ()) {
			if (a[i] > st.top ().ff) st.pop ();
			else break;
		}
		if (!st.empty ()) s = st.top ().ss;
		for (int j = 1; j <= min(i, k); j++) {
			if (st.empty () || j - 1 > s) dp[i][j] = mn[i - 1][j - 1] + a[i];
			else dp[i][j] = min (a[i] + dp[s][j - 1], dp[s][j]);
			
			if (i == 1) mn[i][j] = dp[i][j];
			else mn[i][j] = min (mn[i - 1][j], dp[i][j]);
		}
		st.push ({a[i], i});
	}
	cout << dp[n][k] << ' ';
	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...