# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101502 | MThanCCVA | K blocks (IZhO14_blocks) | C++14 | 2 ms | 4492 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 1e4 + 5;
int n, k;
int dp[MAXN][105], a[MAXN];
int main() {
freopen("blocks.inp", "r", stdin);
freopen("blocks.out", "w", stdout);
cin >> n >> k;
memset(dp, 0x3f, sizeof(dp));
int mx = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
dp[i][1] = mx;
}
for (int j = 2; j <= k; j++) {
stack<pii> s;
for (int i = 1; i <= n; i++) {
int tmp = dp[i - 1][j - 1];
while (!s.empty() && s.top().first < a[i]) {
tmp = min(tmp, s.top().second);
s.pop();
}
if (s.empty() || a[i] + tmp < s.top().first + s.top().second)
s.push({a[i], tmp});
if (i >= j) dp[i][j] = s.top().first + s.top().second;
}
}
cout << dp[n][k];
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |