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;
int main()
{
int n, k;
cin >> n >> k;
vector<int> trees(n), pref(n);
for (int i = 0; i < n; i++)
{
cin >> trees[i];
pref[i] = trees[i];
if (i)
pref[i] += pref[i - 1];
}
set<pair<int, int>> parts;
parts.insert({0, n - 1});
int ans = 0;
vector<int> moves;
for (int l = 0; l < k; l++)
{
int mxVal = 0, loc = -1, mst = -1, med = -1;
for (auto e : parts)
{
int st = e.first, ed = e.second;
int curVal = 0;
for (int i = st; i < ed; i++)
{
int fpart = pref[i];
if (st)
fpart -= pref[st - 1];
int spart = pref[ed];
spart -= pref[i];
int curVal = spart * fpart;
if (curVal >= mxVal)
{
mxVal = curVal;
loc = i;
mst = st;
med = ed;
}
}
}
if (loc != -1)
{
ans += mxVal;
moves.push_back(loc + 1);
parts.erase(parts.find({mst, med}));
parts.insert({mst, loc});
parts.insert({loc + 1, med});
}
else
break;
}
cout << ans << '\n';
for (auto e : moves)
{
cout << e << ' ';
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:32:17: warning: unused variable 'curVal' [-Wunused-variable]
32 | int curVal = 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... |