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>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
const int maxk = 200 + 10;
const ll inf = 1e18;
int n, k, a[maxn], sum, L, R, flg[maxk][maxn];
ll val, dp[2][maxn];
inline void add(int i){
val += 1ll * sum * a[i];
sum += a[i];
}
inline void remove(int i){
sum -= a[i];
val -= 1ll * sum * a[i];
}
inline ll get(int l, int r){
while(l < L){
L--;
add(L);
}
while(R < r){
R++;
add(R);
}
while(L < l){
remove(L);
L++;
}
while(r < R){
remove(R);
R--;
}
return val;
}
void solve(int x, int l, int r, int ql, int qr){
int PTR = (x&1);
int PRV = 1 - PTR;
if (r < l) return;
int mid = (l + r) >> 1;
dp[PTR][mid] = dp[PRV][min(mid-1, qr)] + get(min(mid-1, qr) + 1, mid);
int ptr = min(mid - 1, qr);
for (int i = min(mid-1, qr) - 1; i >= ql; i--){
ll tmp = dp[PRV][i] + get(i+1, mid);
if (tmp < dp[PTR][mid]){
dp[PTR][mid] = tmp;
ptr = i;
}
}
flg[x][mid] = ptr;
solve(x, l, mid-1, ql, ptr);
solve(x, mid+1, r, ptr, qr);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = 0;
int sum = 0;
for (int i = 1; i <= n; i++){
ans += 1ll * sum * a[i];
sum += a[i];
dp[0][i] = ans;
}
for (int i = 1; i <= k; i++){
solve(i, 1, n, 0, n);
}
cout << ans - dp[(k&1)][n] << '\n';
int ptr = n;
int tmp = k;
while(tmp){
cout << flg[tmp][ptr] << ' ';
ptr = flg[tmp][ptr];
tmp--;
}
cout << '\n';
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... |