#include <iostream>
#include <deque>
#include <algorithm>
#include <utility>
#include <vector>
#define int long long
using namespace std;
struct Line
{
int a = 0, b = 0;
Line(int a, int b) :a(a), b(b) {}
Line() {}
const inline int operator()(int x) const
{
return a * x + b - x * x;
}
const bool operator<(const Line& other) const
{
return a == other.a ? b < other.b : a < other.a;
}
const inline int intersect(const Line& other) const
{
return (other.b - b) / (a - other.a) + ((other.b - b) % (a - other.a) != 0);
}
};
struct Node
{
int idx = 0;
int itsPos = 0;
Line line = Line();
Node(int itsPos, const Line& line, int idx) :itsPos(itsPos), line(line), idx(idx) {}
Node(int itsPos) {}
Node() {}
const bool operator<(const Node& other) const
{
return itsPos < other.itsPos;
}
};
std::deque<Node> dq;
void addLine(const Line& line, int idx)
{
while (!dq.empty())
{
if (dq.back().line.a == line.a)
{
if (dq.back().line.b < line.b) dq.pop_back();
return;
}
if (dq.back().line.intersect(line) <= dq.back().itsPos) dq.pop_back();
else break;
}
if (dq.empty()) dq.push_back({ -99999999999999999LL,line,idx });
else dq.push_back({ dq.back().line.intersect(line),line, idx });
}
void clearDeque()
{
while (!dq.empty()) dq.pop_back();
}
pair<int, int> query(int x)
{
int l = 0, r = dq.size() - 1;
int result = 0;
int idx = 0;
while (l <= r)
{
int mid = (l + r) / 2;
if (result < dq[mid].line(x))
{
result = dq[mid].line(x);
idx = dq[mid].idx;
}
if (dq[mid].itsPos <= x)
{
l = mid + 1;
}
else
{
r = mid - 1;
}
}
return{ result,idx };
}
int n, k;
signed trace[201][100001];
int dp[2][100001];
Line pre[2][100001];
int a[100001];
int prx[100001];
void debug()
{
for (int i = 0; i <= k; i++)
{
for (int j = 1; j <= n; j++)
{
cerr << trace[i][j] << " ";
}
cerr << "\n";
}
cerr << "\n";
}
int sum(int l, int r)
{
return prx[r] - prx[l - 1];
}
signed main()
{
cin >> n >> k;
//k++;
k--;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
prx[i] = prx[i - 1] + a[i];
}
//maximize this dp: dp[t][i] = max(dp[t][i],dp[t - 1][j - 1] + sum(j,i) * sum(i + 1,n));
for (int i = 2; i <= n; i++)
{
dp[0][i] = prx[i - 1] * (prx[n] + prx[0]) - prx[i - 1] * prx[i - 1] - prx[0] * prx[n];
pre[0][i] = Line((prx[n] + prx[i - 1]), dp[0][i] - prx[i - 1] * prx[n]);
}
for (signed t = 1; t <= k; t++)
{
//cerr << t << "\n";
clearDeque();
for (signed i = t + 1; i <= n; i++)
{
//add previous one
addLine(pre[(t-1)&1][i - 1], i - 1);
//get
pair<int,int> x = query(prx[i - 1]);
int val = x.first;
int idx = x.second;
dp[t&1][i] = val;
trace[t][i] = idx;
//apply
if (i < n)
{
pre[t&1][i] = Line(prx[n] + prx[i - 1], val - prx[i - 1] * prx[n]);
}
}
/*
for (auto x : dq)
{
cerr << x.itsPos << " " << x.line.a << "x " << x.line.b << " -x^2 \n";
}
*/
}
vector<int> res;
int result = 0, idx = 0;
for (int i = n; i >= 1; i--)
{
if (result < dp[k&1][i])
{
result = dp[k&1][i];
idx = i;
}
}
res.push_back(idx-1);
for (int i = k; i >= 1; i--)
{
res.push_back(trace[i][idx] - 1);
idx = trace[i][idx];
}
//debug();
cout << result << '\n';
sort(res.begin(), res.end());
int id = 1;
for (auto x : res)
{
if (x < 0 || x < id)
{
cout << id++ << " ";
}else
cout << x << " ";
}
return 0;
}
Compilation message
sequence.cpp: In constructor 'Node::Node(long long int, const Line&, long long int)':
sequence.cpp:35:10: warning: 'Node::line' will be initialized after [-Wreorder]
35 | Line line = Line();
| ^~~~
sequence.cpp:33:9: warning: 'long long int Node::idx' [-Wreorder]
33 | int idx = 0;
| ^~~
sequence.cpp:36:5: warning: when initialized here [-Wreorder]
36 | Node(int itsPos, const Line& line, int idx) :itsPos(itsPos), line(line), idx(idx) {}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
1 ms |
6488 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
2 ms |
10588 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
1 ms |
6488 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
1 ms |
6688 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
1 ms |
6488 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
3 ms |
26968 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
8792 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
10584 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
10584 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
2 ms |
16732 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
10588 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
2 ms |
10584 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
2 ms |
12632 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
8536 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
10 ms |
84460 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
7 ms |
61784 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
9 ms |
78424 KB |
declared answer doesn't correspond to the split scheme: declared = 103780000, real = 107630184 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
18 ms |
84568 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
8536 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
18 ms |
84568 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
16 ms |
74328 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
20 ms |
84568 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
18 ms |
84568 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
5 ms |
22872 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
9 ms |
39260 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8792 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
8796 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
132 ms |
84816 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
4 ms |
9048 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
89 ms |
54096 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
114 ms |
62292 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
119 ms |
66384 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
100 ms |
56144 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
108 ms |
62300 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
136 ms |
78676 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11600 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
30 ms |
11352 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1544 ms |
88420 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
34 ms |
11860 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1699 ms |
87380 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
1342 ms |
64124 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1386 ms |
69376 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
1179 ms |
58924 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
1233 ms |
65052 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1567 ms |
81776 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |