-
Notifications
You must be signed in to change notification settings - Fork 1
/
P1043.c
62 lines (54 loc) · 1.67 KB
/
P1043.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdio.h"
#include "stdlib.h"
#define MAXN 50
#define MAXM 9
typedef long long rt;
#define INF 0x7fffffffffffffff
int arr[MAXN << 1];
int sum[MAXN << 1];
rt f[MAXN << 1][MAXN << 1][MAXM + 1];
rt g[MAXN << 1][MAXN << 1][MAXM + 1];
#define FLOOR10(x) ((x) > 0 ? (x)/10 : ((x) % 10 ? (x)/10 - 1 : (x)/10))
#define MOD10(x) ((x) - FLOOR10(x) * 10)
#define SUM(i, j) ((i) > 0 ? sum[j] - sum[i - 1] : sum[j])
#define MOD(i, j) MOD10(SUM(i, j))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main() {
int n, m;
scanf("%d %d", &n, &m);
int i, j, k, l;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum[i] = arr[i];
if (i) sum[i] += sum[i - 1];
}
for (; i < n << 1; i++) {
arr[i] = arr[i - n];
sum[i] = arr[i] + sum[i - 1];
}
for (i = 0; i < n << 1; i++)
for (j = i; j < n << 1; j++) {
f[i][j][1] = g[i][j][1] = MOD(i, j);
for (k = 2; k <= m; k++)
g[i][j][k] = INF;
}
for (int len = 2; len <= n; len++) {
for (i = 0; i <= (n << 1) - len; i++) {
j = i + len - 1;
for (k = 2; k <= MIN(len, m); k++) {
for (l = i + k - 1; l <= j; l++) {
f[i][j][k] = MAX(f[i][j][k], f[i][l - 1][k - 1] * MOD(l, j));
g[i][j][k] = MIN(g[i][j][k], g[i][l - 1][k - 1] * MOD(l, j));
}
}
}
}
rt ans_max = 0, ans_min = INF;
for (i = 0; i <= n; i++) {
ans_max = MAX(ans_max, f[i][i + n - 1][m]);
ans_min = MIN(ans_min, g[i][i + n - 1][m]);
}
printf("%lld\n%lld\n", ans_min, ans_max);
exit(0);
}