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;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
const int N = 10001;
void solve(){
// brute force O(n^2 * k)
int n, k; cin >> n >> k;
vector<ll> a(n+1); FOR(i,1,n+1) cin >> a[i];
vector<ll> s(n+1); FOR(i,1,n+1) s[i]=a[i]+s[i-1];
ll dp[n+1][k+1], pos[n+1][k+1];
ms(dp,0,sizeof(dp));
ms(pos,-1,sizeof(pos));
FOR(i,1,n+1){
dp[i][0] = s[i] * (s[n]-s[i]);
FOR(j,1,min(i,k+1)){
FOR(l,1,i){
ll val = dp[l][j-1] + (s[i]-s[l])*(s[n]-s[i]+s[l]);
if (val > dp[i][j] && dp[l][j-1] != 0){
dp[i][j]=val;
//cout<<i<<" "<<j<<" "<<val<<" "<<val-dp[l][j-1]<<'\n';
pos[i][j]=l;
}
}
}
}
cout << dp[n][k]/2 << '\n';
int cur = n;
vi b;
ROF(i,k,1){
cur = pos[cur][i];
b.pb(cur);
}
ROF(i,k-1,0){
cout << b[i]<<" ";
}
cout << '\n';
return;
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
}
Compilation message (stderr)
sequence.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("Ofast")
|
sequence.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
# | 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... |