#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18;
const ll BASE = 137;
const int szBL = 320;
int n, K;
ll a[N];
ll dp[N][2];
unordered_map<int,int> trace[202];
ll pre[N];
struct Line {
#define ld long double
ll a, b, id;
ld rx;
Line(ll aa = 0, ll bb = 0, ll _id = INF, ld _rx = INF) : a(aa), b(bb), id(_id), rx(_rx) {};
ll eval(ll x) {
return a * x + b;
}
ld isectx(const Line &other) {
assert(a != other.a);
return 1.0 * (other.b - b) / (a - other.a);
}
bool operator < (const Line &other) const {
if (other.a != INF) return a < other.a;
return rx < other.rx || (rx == other.rx && id < other.id);
}
};
struct dynamicCHT {
set<Line> hull;
void init () {
set<Line> ().swap(hull);
}
bool hasPrev(set<Line>::iterator it) { return it != hull.begin(); }
bool hasNext(set<Line>::iterator it) { return it != hull.end() && next(it) != hull.end(); }
bool bad(Line l1, Line l2, Line l3) {
return l2.isectx(l3) <= l1.isectx(l2);
}
bool bad(set<Line>::iterator it) {
return (hasPrev(it) && hasNext(it) && bad(*prev(it), *it, *next(it)));
}
void calcrx(set<Line>::iterator it) {
Line l = *it;
if (hasNext(it)) l.rx = l.isectx(*next(it));
else l.rx = INF;
hull.erase(it); hull.insert(l);
}
void insert_Line(Line newLine) {
auto it = hull.lower_bound(newLine);
if (it != hull.end() && it->a == newLine.a) {
if (it->b > newLine.b) return;
hull.erase(it);
}
it = hull.insert(newLine).first;
if (bad(it)) {
hull.erase(it);
return;
}
while (hasPrev(it) && bad(prev(it))) hull.erase(prev(it));
while (hasNext(it) && bad(next(it))) hull.erase(next(it));
if (hasPrev(it)) calcrx(prev(it));
calcrx(it);
}
pll get (ll x) {
if (hull.empty())
return MP(-INF, 0);
Line it = *hull.lower_bound(Line(INF, 0, 0, x));
return MP(it.eval(x), it.id);
}
} cht;
void solution () {
cin >> n >> K;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n) pre[i] = pre[i - 1] + a[i];
rep (i, 1, n) dp[i][1] = 0;
rep (k, 2, K) {
cht.init();
rep (i, 1, n) {
pll cur = cht.get(pre[i]);
dp[i][k & 1] = cur.fs;
if (cur.fs < 1e16)
trace[k][i] = cur.se;
// cout << i <<" "<<k<<" "<<cur.fs<<" "<<cur.se <<"\n";
cht.insert_Line(Line(pre[i], dp[i][k & 1 ^ 1] - pre[i] * pre[i], i));
}
}
ll res = 0;
int opt = 0;
rep (i, 1, n) {
if (res < dp[i][K & 1] + (pre[n] - pre[i]) * pre[i]) {
res = dp[i][K & 1] + (pre[n] - pre[i]) * pre[i];
opt = i;
}
}
vector<int> Ans;
reb (k, K, 1) {
Ans.push_back(opt);
opt = trace[k][opt];
}
cout << res <<"\n";
iter (&id, Ans) cout << id<<" ";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("TREEV");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
7 3
4 1 3 4 0 2 3
*/
Compilation message
sequence.cpp: In function 'void solution()':
sequence.cpp:116:50: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
116 | cht.insert_Line(Line(pre[i], dp[i][k & 1 ^ 1] - pre[i] * pre[i], i));
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
0 ms |
2396 KB |
Integer 0 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
9 ms |
3932 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
10 ms |
3616 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
8 ms |
3792 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
9 ms |
3928 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
2 ms |
2652 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
2 ms |
2652 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
3 ms |
2908 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
57 ms |
10416 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
84 ms |
10344 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
42 ms |
9300 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
52 ms |
10468 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
51 ms |
10324 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
11 ms |
3932 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
29 ms |
5544 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4188 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
6 ms |
3792 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
746 ms |
81796 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
9 ms |
4188 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
454 ms |
49976 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
469 ms |
57600 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
549 ms |
62032 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
491 ms |
50992 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
515 ms |
57972 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
672 ms |
73800 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
21676 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
79 ms |
21388 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Execution timed out |
2101 ms |
38556 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |