#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Line {
mutable ll k, m, p, idx;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m, int idx) {
auto z = insert({k, m, 0, idx}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
pair<ll,int> query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return {l.k * x + l.m, l.idx};
}
};
int N, K, a[100001];
ll pref[100001] = {0};
ll dp[100001] = {0};
int last_split[100001];
int cnt[100001];
LineContainer cht;
int calc(ll cost)
{
cht.clear();
cnt[0] = 0;
for (int i = 1; i <= N; i++)
{
dp[i] = 0;
cht.add(pref[i-1], dp[i-1] - pref[i-1] * pref[i-1], i-1);
auto [val, idx] = cht.query(pref[i]);
dp[i] = val - cost;
last_split[i] = idx;
cnt[i] = cnt[idx] + 1;
}
return cnt[N];
}
int main()
{
cin >> N >> K;
for (int i = 1; i <= N; i++)
{
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
ll lo = 0, hi = 1e12;
while (lo < hi)
{
ll mid = (lo + hi) / 2;
if (calc(mid) > K + 1)
lo = mid + 1;
else
hi = mid;
}
calc(lo);
cout << dp[N] + lo * cnt[N] << '\n';
int i = N;
do
{
i = last_split[i];
cout << i << ' ';
} while (--K);
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Incorrect |
0 ms |
212 KB |
Integer 0 violates the range [1, 9] |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
0 ms |
212 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
340 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Incorrect |
1 ms |
340 KB |
Integer 0 violates the range [1, 199] |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
348 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
4 ms |
340 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
7 ms |
340 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Incorrect |
6 ms |
340 KB |
declared answer doesn't correspond to the split scheme: declared = 42840610445659, real = 30576061956157 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
852 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
51 ms |
1204 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
74 ms |
1344 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Incorrect |
77 ms |
1332 KB |
declared answer doesn't correspond to the split scheme: declared = 4927594230864216, real = 409963188750151 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
633 ms |
6864 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
725 ms |
7744 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Incorrect |
1255 ms |
11332 KB |
declared answer doesn't correspond to the split scheme: declared = 499095080972605870, real = 242021068067177547 |
4 |
Halted |
0 ms |
0 KB |
- |