제출 #119735

#제출 시각아이디문제언어결과실행 시간메모리
119735dolphingarlicK개의 묶음 (IZhO14_blocks)C++14
100 / 100
721 ms80664 KiB
#include <bits/stdc++.h> using namespace std; stack<int> s; int main() { int n, k; cin >> n >> k; int arr[n + 1]; for (int i = 1; i <= n; ++i) cin >> arr[i]; int dp[n + 1][k + 1]; int chosen[n + 1][k + 1]; dp[0][1] = 0; for (int i = 1; i <= n; ++i) dp[i][1] = max(arr[i], dp[i - 1][1]), chosen[i][1] = dp[i][1]; for (int j = 2; j <= k; ++j) { while (s.size() > 0) s.pop(); for (int i = j; i <= n; ++i) { dp[i][j] = dp[i - 1][j - 1] + arr[i]; chosen[i][j] = arr[i]; while (s.size() > 0) { int idx = s.top(); if (arr[idx] <= arr[i]) { if (dp[idx][j] - chosen[idx][j] + max(chosen[idx][j], arr[i]) < dp[i][j]) { dp[i][j] = dp[idx][j] - chosen[idx][j] + max(chosen[idx][j], arr[i]); } s.pop(); } else break; } if (s.size() > 0) { int idx = s.top(); if (dp[idx][j] < dp[i][j]) { dp[i][j] = dp[idx][j]; chosen[i][j] = chosen[idx][j]; } } s.push(i); } } cout << dp[n][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...