This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir-Rahmanir-Rahim
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define pb push_back
#define pii pair <int, int>
#define pll pair <long long, long long>
#define pld pair <long double, long double>
#define ll long long
#define ld long double
#define x first
#define y second
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define skip continue
#define bpop(x) (ll)__builtin_popcountll(x)
using namespace std;
const int N = 1e5 + 7;
const int M = 1e2 + 7;
const int MAXA = 5e4 + 7;
const int inf = 1e18 + 7;
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
#define int long long
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k, a[N], pre[N], pr[N][207], dp[N][207];
int calc(int l, int r) {
return pre[r] - pre[l - 1];
}
void subtask1234() {
memset(dp, 0, sizeof dp), memset(pr, 0, sizeof pr);
for (int i = 2;i <= n;i++) {
for (int j = 2;j <= k + 1;j++) {
for (int last = 1;last < i;last++) {
int x = dp[last][j - 1] + pre[last] * (pre[i] - pre[last]);
if (x > dp[i][j])dp[i][j] = x, pr[i][j] = last;
}
}
}
int ans = dp[n][k + 1];
cout << ans << '\n';
int pos = n, cur = k + 1;
vector <int> path;
while (cur > 0) {
pos = pr[pos][cur], cur--;
if (pos)path.pb(pos);
}
reverse(all(path));
for (auto to : path)cout << to << ' ';
}
void solve() {
cin >> n >> k;
for (int i = 1;i <= n;i++)cin >> a[i], pre[i] = pre[i - 1] + a[i];
subtask1234();
}
signed main() {
//srand(time(NULL));
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("milkorder.in", "r", stdin);
//freopen("milkorder.out", "w", stdout);
int test = 1;
//cin >> test;
for (int i = 1;i <= test;i++) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
sequence.cpp:4: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
4 | #pragma comment(linker, "/stack:200000000")
|
sequence.cpp:26:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
26 | const int inf = 1e18 + 7;
| ~~~~~^~~
# | 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... |