Submission #1083585

#TimeUsernameProblemLanguageResultExecution timeMemory
1083585f0rizenK blocks (IZhO14_blocks)C++17
100 / 100
192 ms41988 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll infll = 1e18; template<typename T> istream &operator>>(istream &is, vector<T> &a) { for (auto &i : a) { is >> i; } return is; } int32_t main() { #ifdef LOCAL freopen("/tmp/input.txt", "r", stdin); #else ios::sync_with_stdio(false); cin.tie(nullptr); #endif int n, k; cin >> n >> k; vector<int> a(n); cin >> a; vector<vector<int>> dp(k + 1, vector<int>(n + 1, inf)); dp[0][0] = 0; for (int j = 1; j <= k; ++j) { vector<int> st, mn, pref; for (int i = 1; i <= n; ++i) { int cur = dp[j - 1][i - 1]; while (!st.empty() && a[st.back()] <= a[i - 1]) { cur = min(cur, mn.back()); st.pop_back(); mn.pop_back(); pref.pop_back(); } st.push_back(i - 1); mn.push_back(cur); if (pref.empty()) { pref.push_back(cur + a[i - 1]); } else { pref.push_back(min(pref.back(), cur + a[i - 1])); } dp[j][i] = pref.back(); } } cout << dp[k][n] << "\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...