이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
using namespace std;
const int N = (int) 100;
const int M = (int) 2e6 + 10;
const ll LINF = (ll) 1e18;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
int n, k;
ll dp[N][N][N];
int a[N];
int sum(int l, int r) {
return a[r] - a[l - 1];
}
ll calc(int l, int r, int k) {
if (l > r) return 0;
if (k == 0) return 0;
ll &ans = dp[l][r][k];
if (ans != -1)
return ans;
ans = 0;
for (int i = l; i < r; i++) {
for (int t = 0; t < k; t++) {
ans = max(ans, calc(l, i, t) + calc(i + 1, r, k - 1 - t) + sum(l, i) * 1ll * sum(i + 1, r));
}
}
return ans;
}
void print(int l, int r, int k) {
if (k == 0) return;
for (int i = l; i < r; i++) {
for (int t = 0; t < k; t++) {
if (calc(l, r, k) == calc(l, i, t) + calc(i + 1, r, k - 1 - t) + sum(l, i) * 1ll * sum(i + 1, r)) {
cout << i << " ";
print(l, i, t);
print(i + 1, r, k - 1 - t);
return;
}
}
}
}
int main() {
#define fn "balls"
#ifdef witch
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen(fn".in", "r", stdin);
// freopen(fn".out", "w", stdout);
#endif
memset(dp, -1, sizeof(dp));
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
cout << calc(1, n, k) << endl;
print(1, n, k);
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |