제출 #778397

#제출 시각아이디문제언어결과실행 시간메모리
778397BamshooTK개의 묶음 (IZhO14_blocks)C++14
100 / 100
152 ms80648 KiB
#include <iostream> #include <algorithm> #include <stack> #define ll long long #define inf 1e18 #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define fi first #define se second using namespace std; const int N = 1e5 + 9; int n, k; ll a[N], dp[109][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("nhap.inp", "r", stdin); // freopen("xuat.out", "w", stdout); cin >> n >> k; FOR(i, 1, n) cin >> a[i]; FOR(i, 1, k) FOR(j, 0, n) dp[i][j] = inf; dp[1][0] = 0; FOR(i, 1, n) dp[1][i] = max(dp[1][i-1], a[i]); FOR(i, 2, k) { stack < pair<ll, int>> st; FOR(j, i, n){ ll minF = dp[i-1][j - 1]; while (!st.empty() && a[st.top().se] <= a[j]) { minF = min(minF, st.top().fi); st.pop(); } dp[i][j] = min(dp[i][(!st.empty() ? st.top().se : 0)], minF + a[j]); st.push({minF, j}); } } cout << dp[k][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...