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'
#define fr first
#define sc second
#define all(v) v.begin(),v.end()
#define unq(v) sort( all(v) ); v.erase( unique( all(v) ), v.end() )
#define bout(x) cout << bitset<sizeof(x)*8>(x) << endl
#define mkp(a,b) make_pair(a,b)
#define gcd(a,b) __gcd(a,b)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pi2 = pair<int, int>;
using pl2 = pair<ll, ll>;
using cpl = complex<ld>;
const ld pi = 3.14159265358979323846264338327950288;
const ld tau = 2 * pi;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, ddy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int nx[8] = {1, 2, 2, 1, -1, -2, -2, -1}, ny[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
// #define DEBUG
ll arr[100020], prf[100020];
ll dp[100020][220];
int bef[100020][220];
ll sum(int st, int ed){
if (st > ed){ return 0; }
return prf[ed] - prf[st-1];
}
ll f(int p, int i, int j){
return dp[p][j-1] + sum(1, p) * sum(p+1, i);
}
void Main(){
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> arr[i];
prf[i] = prf[i-1] + arr[i];
}
for (int j = 1; j <= k; j++){
int ptr = j;
for (int i = j+1; i <= n; i++){
while ( ptr < i-1 && f(ptr, i, j) <= f(ptr+1, i, j) ){ ptr += 1; }
dp[i][j] = f(ptr, i, j); bef[i][j] = ptr;
}
}
cout << dp[n][k] << endl;
while (k){
cout << bef[n][k] << ' ';
n = bef[n][k]; k -= 1;
}
}
int main(){
#ifndef DEBUG
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#endif
cout.setf(ios::fixed);
cout.precision(0);
Main();
}
# | 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... |