This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
#define mak make_pair
constexpr ll inf = 1e18, N = 1e6 + 10, mod = 1e9 + 7, pr = 1000696969;
ll a[N], ps[N];
bool mark[N];
inline pair<ll, ll> Solve(int l, int r) {
ll ans = 0, sum = 0, pos = 0;
sum = ps[r - 1] - ps[l - 1];
for(int i = l; i < r - 1; i++) {
ll val = ps[i] - ps[l - 1];
val *= (sum - val);
if(val >= ans) {
ans = val, pos = i;
}
}
return mak(ans, pos + 1);
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
int n, k; cin >>n >>k;
for(int i = 1; i <= n; i++) cin >>a[i];
for(int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i];
vector<int> vc;
vc.push_back(1), vc.push_back(n + 1);
ll sum = 0;
while(k--) {
ll mx = 0, ind = 0;
for(int i = 0; i < Sz(vc) - 1; i++) {
int l = vc[i], r = vc[i + 1];
if(r - l == 1) continue;
pair<ll, ll> p = Solve(l, r);
if(p.first >= mx) ind = p.second;
mx = max(mx, p.first);
}
sum += mx;
vc.push_back(ind);
sort(All(vc));
}
cout<<sum <<endl;
for(auto i : vc) {
if(i != 1 && i != n + 1) cout<<i - 1 <<" ";
}
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... |