제출 #678926

#제출 시각아이디문제언어결과실행 시간메모리
678926vjudge1K개의 묶음 (IZhO14_blocks)C++14
0 / 100
1 ms340 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define pb push_back #define all(a) a.begin(), a.end() typedef long long ll; typedef pair<int, int> ii; const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, k; int a[N]; ll dp[105][N]; void solve() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 0; i <= k; i++) for(int j = 0; j <= n; j++) dp[i][j] = 1e17; dp[1][0] = 0; for(int i = 1; i <= n; i++) dp[1][i] = max(dp[1][i - 1], 1ll * a[i]); for(int i = 2; i <= k; i++) { stack<int> st; for(int j = i; j <= n; j++) { while(st.size() && a[j] >= a[st.top()]) { dp[i][j] = min(dp[i][j], dp[i - 1][st.top()] + a[j]); st.pop(); } int k = st.size() ? st.top() : i - 1; dp[i][j] = min({dp[i][j], dp[i][k], dp[i - 1][k] + a[j]}); st.push(j); } } cout << dp[k][n]; } signed main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while(t--) solve(); 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...