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;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
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 A>
void __print(const A &x);
template <typename A, typename B>
void __print(const pair<A, B> &p);
template <typename... A>
void __print(const tuple<A...> &t);
template <typename T>
void __print(stack<T> s);
template <typename T>
void __print(queue<T> q);
template <typename T, typename... U>
void __print(priority_queue<T, U...> q);
template <typename A>
void __print(const A &x) {
bool first = true;
cerr << '{';
for (const auto &i : x) {
cerr << (first ? "" : ","), __print(i);
first = false;
}
cerr << '}';
}
template <typename A, typename B>
void __print(const pair<A, B> &p) {
cerr << '(';
__print(p.first);
cerr << ',';
__print(p.second);
cerr << ')';
}
template <typename... A>
void __print(const tuple<A...> &t) {
bool first = true;
cerr << '(';
apply([&first](const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
cerr << ')';
}
template <typename T>
void __print(stack<T> s) {
vector<T> debugVector;
while (!s.empty()) {
T t = s.top();
debugVector.push_back(t);
s.pop();
}
reverse(debugVector.begin(), debugVector.end());
__print(debugVector);
}
template <typename T>
void __print(queue<T> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.front();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
template <typename T, typename... U>
void __print(priority_queue<T, U...> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.top();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
void _print() { cerr << "]\n"; }
template <typename Head, typename... Tail>
void _print(const Head &H, const Tail &...T) {
__print(H);
if (sizeof...(T))
cerr << ", ";
_print(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve(){
int n,k; cin>>n>>k;
vector<int> a(n+1);
for(int i=1;i<=n;i++) cin>>a[i];
vector<int> pre(n+1);
for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
vector<vector<int>> dp(n+1,vector<int>(k+1,-1e9)),from(n+1,vector<int>(k+1,-1));
for(int i=0;i<=n;i++) dp[i][0]=0;
for(int i=0;i<=k;i++) dp[0][i]=0;
for(int i=0;i<=n;i++){
for(int j=0;j<=i;j++){
for(int s=k;s>=1;s--){
if(dp[j][s-1]!=-1e9){
if(dp[j][s-1]+(pre[i]-pre[j])*(pre[n]-pre[i])>dp[i][s]){
dp[i][s]=dp[j][s-1]+(pre[i]-pre[j])*(pre[n]-pre[i]);
from[i][s]=j;
}
}
}
}
}
int mx=-1,x,y;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++){
if(dp[i][j]>mx){
mx=dp[i][j];
x=i; y=j;
}
}
}
cout<<mx<<'\n';
vector<int> ans;
for(int i=y-1;i>=0;i--){
ans.pb(x);
x=from[x][i];
}
reverse(ans.begin(),ans.end());
for(auto&x:ans) cout<<x+1<<' ';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:125:17: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
125 | int mx=-1,x,y;
| ^
# | 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... |