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 FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
x = (x > y ? y : x);
}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
void solve() {
int n, k;
cin >> n >> k;
ll dp[n + 1][k + 1];
V<int>a (n);
forn(i, n)cin >> a[i];
forn(i, n + 1) forn(j, k + 1) dp[i][j] = 0;
V<int> pr(n);
forn(i, n) {
pr[i] = a[i] + (i == 0 ? 0 : pr[i - 1]);
}
debug(pr);
debug(a);
auto sum = [&](int l, int r) {
if(l > r)return 0;
int p = pr[r];
if(l)p -= pr[l - 1];
return p;
};
ll ans = 0;
VV<pairs> parent(n + 1, V<pairs>(k + 1, {-1, -1}));
pairs lst = {-1, -1};
for(int i = 0;i < n;++i) {
for(int r = i;r < n;++r) {
for(int x = 0;x < k;++x) {
ll trans = dp[i][x] + sum(i, r) * 1ll * sum(r + 1, n - 1);
if(dp[r + 1][x + 1] < trans) {
dp[r + 1][x + 1] = trans;
parent[r + 1][x + 1] = {i, x};
}
if(dp[r + 1][x + 1] > ans) {
ans = dp[r + 1][x + 1];
lst = {r + 1, x + 1};
}
}
}
}
V<int> op;
cout << ans << '\n';
while(lst.first > 0) {
op.push_back(lst.first);
lst = parent[lst.first][lst.second];
}
if(op.size() < k) {
V<bool> vis(n, false);
for(auto c : op) vis[c] = 1;
for(int i = 1;i < n;++i) {
if(op.size() == k)break;
if(!vis[i])op.push_back(i);
}
}
for(auto c : op) {
cout << c << ' ';
}
}
void test_case() {
int t;
cin >> t;
forn(p, t) {
//cout << "Case #" << p + 1 << ": ";
solve();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:106:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
106 | if(op.size() < k) {
| ~~~~~~~~~~^~~
sequence.cpp:111:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
111 | if(op.size() == k)break;
| ~~~~~~~~~~^~~~
# | 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... |