Submission #736400

#TimeUsernameProblemLanguageResultExecution timeMemory
736400cdjs1432K blocks (IZhO14_blocks)C++17
0 / 100
20 ms40288 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int a[100005]; int dp[100005][102]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp,0x3f,sizeof(dp)); int n, k; cin >> n >> k; int maxx = 0; for (int i=1; i<=n; i++) { cin >> a[i]; dp[i][1] = maxx = max(a[i], maxx); } for (int i=2; i<=k; i++) { stack<pair<int, int>> s; for (int j=1; j<=n; j++) { int best = dp[j-1][i-1]; while (!s.empty() && a[s.top().second] <= a[j]) { best = min(best, s.top().first); s.pop(); } if (s.empty()) dp[j][i] = best + a[j]; else dp[j][i] = min(best + a[j], dp[s.top().second][i-1]); s.push({best, j}); } } cout << dp[n][k]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...