Submission #102773

#TimeUsernameProblemLanguageResultExecution timeMemory
102773Mahmoud_AdelK blocks (IZhO14_blocks)C++14
0 / 100
237 ms79864 KiB
#include <bits/stdc++.h> using namespace std; #define s second #define f first typedef long long ll; const int N = 100005, M = 105; int n, k, a[N], dp[M][N], ch[M][N]; int main() { cin >> n >> k; for(int i=1; i<=n; i++) cin >> a[i]; dp[0][0] = 0; for(int i=1; i<=k; i++) { stack<int> s; for(int j=i; j<=n; j++) { dp[i][j] = dp[i-1][j-1]+ a[j], ch[i][j] = a[j]; while(!s.empty()) { int u = s.top(); if(a[u] <= a[j]) { if(dp[i][j] > dp[i][u] - ch[i][u] + max(ch[i][u], a[j])) dp[i][j] = dp[i][u] - ch[i][u] + max(ch[i][u], a[j]), ch[i][j] = max(ch[i][u], a[j]); } else break; s.pop(); } if(!s.empty()) { int u = s.top(); if(dp[i][u] < dp[i][j]) { dp[i][j] = dp[i][u]; ch[i][j] = ch[i][u]; } } s.push(j); } } cout << dp[k][n] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...