This repository was archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1067.cpp
More file actions
108 lines (97 loc) · 2.24 KB
/
1067.cpp
File metadata and controls
108 lines (97 loc) · 2.24 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
auto nextInt () -> int;
auto readN (int *array, int n) -> void;
template <typename T>
auto print (const T &) -> void;
template <typename T>
auto println (const T &) -> void;
template <typename T>
auto printsp (const T &) -> void;
template <typename T>
struct Greater;
template <typename T>
struct Less;
constexpr int kMaxn = 100 + 10;
constexpr int kSize = 2 * kMaxn;
int array[kSize];
int dp[kSize][kSize];
auto main () -> int {
const int n = nextInt();
for (int i = 0; i < n; ++i) {
array[i] = array[n + i] = nextInt();
}
for (int length = 1; length <= n; ++length) {
for (int l = 0, r; (r = l + length - 1) < 2 * n; ++l) {
for (int i = l; i < r; ++i) {
dp[l][r] = std::max(dp[l][r], dp[l][i] + dp[i + 1][r] + array[l] * array[r + 1] * array[i + 1]);
}
}
}
int max = 0;
for (int i = 0; i < n; ++i) {
max = std::max(max, dp[i][i + n - 1]);
}
println(max);
return 0;
}
auto nextInt () -> int {
int i = 0, sign = 1;
char c;
while (!isdigit(c = getchar())) if (c == '-') sign = -1;
do {
i = i * 10 + c - '0';
} while (isdigit(c = getchar()));
return i * sign;
}
auto readN (int *array, int n) -> void {
for (int i = 0; i < n; ++i) array[i] = nextInt();
}
template <>
auto print<int> (const int &val) -> void {
printf("%d", val);
}
template <>
auto print<char> (const char &val) -> void {
putchar(val);
}
template <>
auto print<char *> (char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<const char *> (const char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<long long> (const long long &val) -> void {
printf("%lld", val);
}
template <>
auto print<unsigned long long> (const unsigned long long &val) -> void {
printf("%llu", val);
}
template <typename T>
auto println (const T &val) -> void {
print(val);
putchar('\n');
}
template <typename T>
auto printsp (const T &val) -> void {
print(val);
putchar(' ');
}
template <typename T>
struct Greater {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs > rhs;
}
};
template <typename T>
struct Less {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs < rhs;
}
};