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 pb push_back
#define task "sequence"
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
using ll = int;
const long long mod = 1e12+7;
const ll N = 1e5+5;
const int base = 350;
using ull = unsigned long long;
using namespace std;
ll n, m, t, k, T, a[N], cnt, lab[N], ans, u, tong, v, b[202][N];
long long dp[2][N], c[N];
string s, ss;
vector<ll> kq;
vector<ll> adj[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = (total * k) % mod;
k = (k * k) % mod;
}
return total;
}
void cal(ll l, ll r, ll opl, ll opr, ll pos)
{
if(l > r)return;
ll mid = (l + r) / 2;
ll best = mid;
dp[1][mid] = -mod;
for(int i = max(opl, pos); i <= min(opr, mid); i ++)
{
if(dp[1][mid] < dp[0][i] + (c[mid] - c[i]) * c[i])
{
dp[1][mid] = dp[0][i] + (c[mid] - c[i]) * c[i];
best = i;
}
}
b[pos][mid] = best;
cal(l, mid-1, opl, best, pos);
cal(mid+1, r, best, opr, pos);
}
void sol()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
c[i] = c[i-1] + a[i];
//dp[1][i] = -mod;
}
for(int i = 1; i <= k; i ++)
{
cal(1, n, 1, n, i);
for(int j = 1; j <= n; j ++)dp[0][j] = dp[1][j];
//cout << dp[n][i] <<" ";
}
//for(int i = 1; i <= n; i ++)cout << dp[i][1] <<" ";
cout << dp[1][n] << '\n';
t = n;
for(int i = k; i > 0; i --)
{
kq.pb(b[i][t]);
t = b[i][t];
}
for(int i = k-1; i >= 0; i --)cout << kq[i] <<" ";
}
int main()
{
if(fopen(task".in", "r")){
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest;
ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:76:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
76 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:77:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
77 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |