Submission #845807

#TimeUsernameProblemLanguageResultExecution timeMemory
845807BamshooTK blocks (IZhO14_blocks)C++14
0 / 100
7 ms45144 KiB
#include <iostream> #include <cstring> #include <vector> #include <stack> #define ll long long #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define fi first #define se second #define inf 1e9 using namespace std; typedef pair <int, int> ii; const int N = 1e5 + 9; int n, k, a[N], dp[N][109], st[N << 2], L[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("nhap.inp", "r", stdin); // freopen("xuat.out", "w", stdout); cin >> n >> k; FOR(i, 1, n) cin >> a[i]; memset(dp, 0x3f, sizeof dp); dp[1][1] = a[1]; FOR(i, 2, n) dp[i][1] = max(dp[i-1][1], a[i]); for (int t = 2; t <= k; ++t){ stack <ii> st; for (int i = t; i <= n; ++i) { int minF = dp[i-1][t-1]; while (!st.empty() && a[st.top().se] <= a[i]){ minF = min(minF, st.top().fi); st.pop(); } dp[i][t] = (dp[i][st.empty() ? 0 : st.top().se], minF + a[i]); st.push({minF, 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...