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("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, int> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
void init()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
}
int n,k;
vi arr;
vl pre;
priority_queue<pair<ll,pair<int,pi>>> pq;
ll ans = 0;
vi ans_arr;
void calc(int l,int r){
if(l>=r)return;
ll tot = pre[r+1] - pre[l];
ll sumt = tot/2;
int pos = lower_bound(pre.begin(),pre.end(),sumt+pre[l]) - pre.begin();
// cout << "create " << l << " " << r << " - " << tot << " " << sumt << " " << pos << " ";
ll sums = (pre[pos] - pre[l]) * (pre[r+1] - pre[pos]);
// cout << (pre[pos] - pre[l]) << " & " << (pre[r+1] - pre[pos]) << " " << sums << endl;
pq.push({sums,{pos-1,{l,r}}});
}
void solve()
{
cin >> n >> k;
arr = vi (n);
FOR(i,0,n)cin >> arr[i];
pre = vl (n+1,0);
FOR(i,1,n+1)pre[i] = pre[i-1] + arr[i-1];
FOR(i,0,n+1)cout << pre[i] << " ";
cout << endl;
calc(0,n-1);
while(ans_arr.size() < k){
auto a = pq.top();pq.pop();
// cout << a.F << " " << a.S.F << " " << a.S.S.F << " " << a.S.S.S << endl;
ans += a.F;
ans_arr.pb(a.S.F);
calc(a.S.S.F,a.S.F);
calc(a.S.F+1,a.S.S.S);
}
cout << ans << endl;
FOR(i,0,k)cout << ans_arr[i] << " ";
cout << endl;
}
int main()
{
fastIO();
// init();
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:84:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | while(ans_arr.size() < k){
| ~~~~~~~~~~~~~~~^~~
sequence.cpp: In function 'void init()':
sequence.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen("output.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |