#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ld long double
#define pll pair<ll,ll>
using namespace std;
const ld inf = 1e18;
const ll infll = 1e18;
const int maxn = 100100;
ll arr[maxn], pref[maxn];
ll dp[210][maxn], n, k;
ll parent[210][maxn];
struct cht {
struct line {
ll k, b; ld x;
ll val; bool isquery;
ll ind;
line (ll _k=0LL, ll _b=0LL, ll _ind=0) :
k(_k), b(_b), x(-inf), val(0), isquery(false), ind(_ind) { }
ll eval(ll x) const { return k*x+b; }
bool parallel(const line &l) const { return k == l.k; }
ld intersection(const line &l) const {
if(parallel(l)) return inf;
return (1.0*b - l.b) / (1.0*l.k - k);
}
bool operator <(const line &l) const {
if(l.isquery) return x < l.val;
else return k < l.k;
}
};
set<line>hull;
typedef set<line>::iterator iter;
bool cPrev(iter it) { return it != hull.begin(); }
bool cNext(iter it) { return it != hull.end() && next(it) != hull.end(); }
bool bad(const line &l1, const line &l2, const line &l3) {
return l1.intersection(l3) <= l1.intersection(l2);
}
bool bad(iter it) {
return cPrev(it) && cNext(it) && bad(*prev(it), *it, *next(it));
}
iter update(iter it) {
if(!cPrev(it)) return it;
line tmp(*it);
ld x = tmp.intersection(*prev(it));
tmp.x = x;
it = hull.erase(it);
return hull.insert(it, tmp);
}
void addline(ll k, ll b, ll ind) {
line l(k, b, ind);
iter it = hull.lower_bound(l);
if(it != hull.end() && it->parallel(l)) {
if(b > it->b) it = hull.erase(it);
else return;
}
it = hull.insert(it, l);
if(bad(it)) return (void) hull.erase(it);
while(cPrev(it) && bad(prev(it))) hull.erase(prev(it));
while(cNext(it) && bad(next(it))) hull.erase(next(it));
it = update(it);
if(cPrev(it)) update(prev(it));
if(cNext(it)) update(next(it));
}
pll query(ll x) {
if(hull.empty()) return mp(-infll, -1LL);
line tmp;
tmp.val = x; tmp.isquery = true;
iter it = --hull.lower_bound(tmp);
return mp(it->eval(x), it->ind);
}
};
int main() {
cin>>n>>k;
for(int i=1;i<=n;i++) {
cin>>arr[i];
pref[i] = pref[i-1] + arr[i];
}
for(int d=1;d<=k;d++) {
if(d == 1) {
for(int i=1;i<n;i++) {
dp[d][i] = pref[i] * (pref[n] - pref[i]);
}
continue;
}
cht ds;
for(int i=d;i<n;i++) {
// add info for state(d-1, i-1)
ds.addline(pref[i-1], dp[d-1][i-1] - pref[i-1]*pref[n], i-1);
pll curr = ds.query(pref[i]);
dp[d][i] = curr.first + pref[i]*pref[n] - pref[i] * pref[i];
parent[d][i] = curr.second;
}
}
ll result = -inf;
ll resultind = -1;
for(int i=k;i<n;i++) {
if(dp[k][i] > result) {
result = dp[k][i];
resultind = i;
}
}
cout<<result<<"\n";
vector<int>v;
for(int i=k;i>=1;i--) {
v.pb(resultind);
resultind = parent[i][resultind];
}
reverse(v.begin(), v.end());
for(int i:v) {
cout<<i<<" ";
} cout<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
5 ms |
512 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
5 ms |
896 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
5 ms |
512 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
5 ms |
640 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
5 ms |
512 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
6 ms |
384 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
5 ms |
512 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
17 ms |
2816 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
5 ms |
384 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
11 ms |
2176 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
11 ms |
2688 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
13 ms |
2944 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
8 ms |
1152 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
6 ms |
768 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
9 ms |
1280 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
6 ms |
512 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
75 ms |
5496 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
6 ms |
640 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
65 ms |
5496 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
58 ms |
4728 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
76 ms |
5496 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
75 ms |
5368 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
20 ms |
1536 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
34 ms |
2552 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1792 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
15 ms |
1792 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
947 ms |
34680 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
21 ms |
2048 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
465 ms |
21356 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
530 ms |
24568 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
675 ms |
26488 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
578 ms |
21880 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
592 ms |
24696 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
761 ms |
31356 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
14840 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
156 ms |
14840 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Execution timed out |
2101 ms |
59768 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |