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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define f first
#define s second
using namespace std;
using namespace __gnu_pbds;
typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tests = 1;
//cin >> tests;
while(tests--)
{
ll n, k, i, j, f;
cin >> n >> k;
ll v[n+5], dp[n+5][k+5], pref[n+5];
for(i = 0; i <= n; i++)
for(j = 0; j <= k; j++)
dp[i][j] = 0;
pref[0] = 0;
for(i = 1; i <= n; i++){
cin >> v[i];
pref[i] = pref[i-1] + v[i];
}
pref[n+1] = pref[n];
for(j = 1; j <= k; j++)
{
for(i = 1; i <= n; i++)
{
for(f = 1; f < i; f++)
dp[i][j] = max(dp[i][j], dp[i-f][j-1] + pref[i-f] * (pref[i] - pref[i-f]));
}
}
cout << dp[n][k] << "\n";
i = n, j = k;
vl res;
while(j)
{
for(f = 1; f < i; f++)
{
if(dp[i-f][j-1] + pref[i-f] * (pref[i] - pref[i-f]) == dp[i][j])
{
j--;
i -= f;
res.pb(i);
break;
}
}
}
reverse(all(res));
for(auto u : res)
cout << u << ' ';
}
}
# | 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... |