This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
#define int ll
typedef pair <int, int> pii;
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"
const int N = 1e5 + 5;
int n, k, a[N], sum[N], ans;
set <pii> st;
int calc(int suml, int sumr, int pos) {
return (sum[pos] - suml) * (sumr - (sum[pos] - suml));
}
set <pii> pos;
vector <int> vec;
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum[i] = sum[i-1] + a[i];
}
st.insert({0, 0});
st.insert({sum[n], n});
pos.insert({0, 0});
pos.insert({n, sum[n]});
for (int _it = 1; _it <= k; ++_it) {
// r, sumr -> st
auto it = st.rbegin();
int r = (it -> S) - 1;
int sumr = (it -> F);
// l, suml -> pos {r}
auto it1 = prev(pos.lower_bound({r, 0}));
int l = (it1 -> F) + 1;
int suml = (it1 -> S);
//show(l); show(r); show(suml); show(sumr); nl;
while (l < r) {
int mid = (l + r) / 2;
if (calc(suml, sumr, mid) >= calc(suml, sumr, mid+1)) r = mid;
else l = mid + 1;
}
ans += calc(suml, sumr, r);
//show(calc(suml, sumr, r)); nl;
vec.pb(r);
st.insert({sumr - (sum[r] - suml), (it -> S)});
st.insert({sum[r] - suml, r});
pos.insert({(it -> S), sumr - (sum[r] - suml)});
pos.insert({r, sum[r] - suml});
//if (pos.find({it->S, it->F}) != pos.end())
pos.erase(pos.find({it->S, it->F}));
st.erase(*it);
/*cout << "V set V\n";
for (auto x : st){
cout << x.F <<" "<< x.S <<"\n";
}*/
}
cout << ans <<"\n";
for (int p : vec) {
cout << p <<" ";
}
}
main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
//cin >> tests;
while (tests --) {
solve();
}
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp:107:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
107 | main () {
| ^
# | 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... |