Submission #340183

#TimeUsernameProblemLanguageResultExecution timeMemory
340183_aniK blocks (IZhO14_blocks)C++17
53 / 100
1093 ms8684 KiB
#define BUGO(x) cerr << #x << " = " << (x) << '\n'; #include <iostream> #include <algorithm> using namespace std; using ll = long long; ll dp[100'002][102], a[100'002]; ll mx[4 * 100'002]; void Build(int v, int vl, int vr) { if (vl == vr) { mx[v] = a[vl]; return; } int m = (vl + vr) / 2; Build(v * 2, vl, m); Build(v * 2 + 1, m + 1, vr); mx[v] = max(mx[v * 2], mx[v * 2 + 1]); } ll Mx(int v, int vl, int vr, int l, int r) { if (vl == vr) return mx[v]; ll res = 0; int m = (vl + vr) / 2; if (l <= m) res = max(res, Mx(v * 2, vl, m, l, min(r, m))); if (r > m)res = max(res, Mx(v * 2 + 1, m + 1, vr, max(l, m + 1), r)); return res; } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; Build(1, 1, n); for (int i = 1; i <= n; i++) for (int curk = 1; curk <= k; curk++) dp[i][curk] = 1'000'000'000'000'000'000; if (k != 1 || (k == 1 && n == 1)) dp[1][1] = a[1]; for (int i = 2; i <= n; i++) for (int curk = max(1, k - n + i); curk <= min(i, k - ((n - i) ? 1 : 0)); curk++) for (int sz = ((curk == 1) ? i : 1); sz <= i - curk + 1; sz++) { /*if (i == 5) { BUGO(curk); BUGO(sz); BUGO(Mx(1, 1, n, i - sz + 1, i)); }*/ dp[i][curk] = min(dp[i][curk], dp[i - sz][curk - 1] + Mx(1, 1, n, i - sz + 1, i)); } /*for (int i = 1; i <= n; i++) { for (int curk = 0; curk <= k; curk++) cerr << dp[i][curk] << ' '; cerr << '\n'; }*/ 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...