제출 #882274

#제출 시각아이디문제언어결과실행 시간메모리
882274OAleksaK개의 묶음 (IZhO14_blocks)C++14
100 / 100
281 ms89860 KiB
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int maxn = 1e5 + 69;
const int inf = 1e18;
int n, k, a[maxn], dp[maxn][110];
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
   	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] = inf;
   	}
   	int mx = 0;
   	for (int i = 1;i <= n;i++) {
   		mx = max(mx, a[i]);
   		dp[i][1] = mx;
   	}
   	for (int j = 2;j <= k;j++) {
   		vector<pair<int, int>> st;
   		for (int i = 1;i <= n;i++) {
	   		int mn = dp[i - 1][j - 1];
	   		while (!st.empty() && a[st.back().f] < a[i]) {
	   			mn = min(mn, st.back().s);
	   			st.pop_back();
	   		}
	   		if (st.empty() || a[i] + mn < a[st.back().f] + st.back().s)
	   			st.push_back({i, mn});
	   		if (i >= j)
	   			dp[i][j] = a[st.back().f] + st.back().s;
   		}
   	}
   	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...