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 <stdio.h>
#include <string.h>
#define N 100000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int main() {
static int aa[N], dp[N + 1], dp_[N + 1], dq[N + 1], dr[N + 1], qu[N + 1];
int n, cnt, k, i, q;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
memset(dp, 0x3f, (n + 1) * sizeof *dp), dp[0] = 0;
while (k--) {
cnt = 0;
dp_[0] = INF;
for (i = 0; i < n; i++) {
q = dp[i];
while (cnt && aa[qu[cnt - 1]] <= aa[i])
q = min(q, dq[--cnt]);
qu[cnt] = i, dq[cnt] = q, dr[cnt] = min(cnt == 0 ? INF : dr[cnt - 1], q + aa[i]), cnt++;
dp_[i + 1] = dr[cnt - 1];
}
memcpy(dp, dp_, (n + 1) * sizeof *dp_);
}
printf("%d\n", dp[n]);
return 0;
}
Compilation message (stderr)
blocks.c: In function 'main':
blocks.c:13:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
blocks.c:15:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# | 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... |