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 endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e4 + 10;
const ll inf = 1e18;
int n, k;
ll a[maxn], pref[maxn], dp[maxn][maxn], bef[maxn][maxn];
void input()
{
cin >> n >> k;
k ++;
for (int i = 1; i <= n; i ++)
cin >> a[i];
}
void calculate_prefix()
{
for (int i = 1; i <= n; i ++)
pref[i] = pref[i - 1] + a[i];
}
ll get_range(int l, int r)
{
return pref[r] - pref[l - 1];
}
void calculate_states()
{
for (int j = 0; j <= k; j ++)
for (int i = 0; i <= n; i ++)
dp[j][i] = -inf;
dp[0][0] = 0;
for (int j = 1; j <= k; j ++)
for (int i = 1; i <= n; i ++)
{
for (int d = 1; d <= i; d ++)
{
ll sum = dp[j - 1][d - 1] + get_range(d, i) * get_range(i + 1, n);
if (sum > dp[j][i])
{
dp[j][i] = sum;
bef[j][i] = d - 1;
}
}
}
/**for (int j = 1; j <= k; j ++, cout << endl)
for (int i = 1; i <= n; i ++)
cout << dp[j][i] << " ";*/
}
void print_answer()
{
cout << dp[k][n] << endl;
int pos = n, part = k;
vector < int > path;
while(bef[part][pos] != 0)
{
pos = bef[part][pos];
part --;
path.push_back(pos);
}
reverse(path.begin(), path.end());
for (int v : path)
cout << v << " ";
cout << endl;
}
void solve()
{
input();
calculate_prefix();
calculate_states();
print_answer();
}
int main()
{
solve();
return 0;
}
/**
7 3
4 1 3 4 0 2 3
*/
# | 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... |