제출 #1124226

#제출 시각아이디문제언어결과실행 시간메모리
1124226ChinguunK개의 묶음 (IZhO14_blocks)C++20
0 / 100
1 ms324 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;
int p[N];
stack <int> st;

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++) {
		dp[i][0] = 0;
		mn[i][0] = 0;
	}

	st.push(0);
	a[0] = 1e6 + 1;
	for (int i = 1; i <= n; i++) {
		while (a[st.top()] < a[i]) st.pop ();
		p[i] = st.top();
		st.push(i);
	}
	
	for (int i = 1; i <= n; i++) {
		int s = p[i];
		for (int j = 1; j <= min(i, k); j++) {
			if (s == 0) dp[i][j] = mn[i - 1][j - 1] + a[i];
			dp[i][j] = min(dp[i][j], min(a[i] + dp[s][j - 1], dp[s][j]));
			mn[i][j] = min(mn[i - 1][j], dp[i][j]);
		}
	}
	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...