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"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> order_set;
#define int long long
signed main () {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
order_set S;
int n, k;
cin >> n >> k;
vector <int> a(n);
for(auto &i : a) cin >> i;
int pref[n];
function <int(int,int)> get=[&](int l, int r) {
if(l == 0) return pref[r];
return pref[r] - pref[l - 1];
};
function <int(int)> Sum=[&](int pos) {
auto pered = S.upper_bound(pos);
auto zad = --S.upper_bound(pos);
int sumPered = get(pos + 1, *pered);
int sumZad = get(*zad+1, pos);
return sumPered * sumZad;
};
pref[0] = a[0];
for(int i = 1; i < n; i ++) pref[i] = a[i] + pref[i-1];
S.insert(-1);
S.insert(n-1);
set <int> ans;
vector <int> res;
int sum = 0;
for(int step = 0; step < k; step ++) {
int mx = -LLONG_MAX, pos;
for(int i = 0; i < n-1; i ++) {
if(ans.count(i) == 1) continue;
if(Sum(i) > mx) {
mx = Sum(i);
pos = i;
}
// cout << i << " " << Sum(i) << "\n";
}
// cout << "\n";
sum += mx;
ans.insert(pos);
res.push_back(pos);
S.insert(pos);
}
cout << sum << "\n";
for(auto i : res) cout << i+1 << " ";
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |