Submission #854808

#TimeUsernameProblemLanguageResultExecution timeMemory
854808NeroZeinK개의 묶음 (IZhO14_blocks)C++17
100 / 100
137 ms80156 KiB
#include "bits/stdc++.h" using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int> a(n + 1); vector<vector<long long>> dp(k, vector<long long> (n + 1)); for (int i = 1; i <= n; ++i) { cin >> a[i]; dp[0][i] = max(a[i], (int) dp[0][i - 1]); } for (int j = 1; j < k; ++j) { stack<pair<int, long long>> stk; for (int i = j + 1; i <= n; ++i) { long long x = dp[j - 1][i - 1]; while (!stk.empty() && stk.top().first <= a[i]) { x = min(x, stk.top().second); stk.pop(); } if (stk.empty() || (stk.top().first + stk.top().second > a[i] + x)) { stk.push({a[i], x}); } dp[j][i] = stk.top().first + stk.top().second; } } cout << dp[k - 1][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...