제출 #671411

#제출 시각아이디문제언어결과실행 시간메모리
671411hct_2so1K개의 묶음 (IZhO14_blocks)C++14
100 / 100
166 ms7232 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 5; const ll INF = 1e18 + 7; ll dp[N][3], n, k, a[N], st[4 * N]; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k; for (int i = 1; i<=n; i++) cin>>a[i], dp[i][1] = max(dp[i - 1][1], a[i]); for (int i = 1; i<=4*n; i++) st[i] = INF; for (int j = 2; j<=k; j++) { stack<pair<int, ll>> s; int cur = j & 1; for (int i = j; i<=n; i++) { ll minF = dp[i - 1][1 - cur]; while (s.size() && a[s.top().first] <= a[i]) minF = min(minF, s.top().second), s.pop(); if (s.empty()) dp[i][cur] = minF + a[i]; else dp[i][cur] = min(dp[s.top().first][cur], minF + a[i]); s.push({i, minF}); } } cout<<dp[n][k & 1]; 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...