This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Be name Khoda
// Khodaya khodet komak kon
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define set_dec(x) cout << fixed << setprecision(x);
#define kill(x) cout << x << endl, exit(0)
#define sz(x) ((int)(x).size())
#define file_io freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
const ll maxN = 100000 + 10, maxK = 210, inf = 1e18 + 7;
ll sm[maxN], dp[2][maxN];
int O[maxK][maxN];
void f(ll op, ll k){
if(k == 0) return;
cout << O[k][op] << " ";
f(O[k][op] , k - 1);
}
void divide(ll k, ll l, ll r, ll optl, ll optr, ll amirJ){
if(r - l < 0) return;
ll mid = (l + r) / 2, op = 0;
dp[k][mid] = -inf;
for(int i = optl; i <= min(optr, mid - 1); i++){
ll res = dp[1 - k][i] + sm[i] * (sm[mid] - sm[i]);
if(res >= dp[k][mid]) dp[k][mid] = res, op = i;
}
O[amirJ][mid] = op;
divide(k, l, mid - 1, optl, op, amirJ);
divide(k, mid + 1, r, op, optr, amirJ);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, k; cin >> n >> k;
for(int i = 0; i < n; i++){
ll x; cin >> x;
sm[i + 1] = sm[i] + x;
}
for(int i = 1; i <= k; i++) divide(i % 2, 1, n, 0, n, i);
cout << dp[k % 2][n] << '\n';
f(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... |