#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll Mod = 1e9 + 7;
const ll Inf = 2242545357980376863LL;
const ll Log = 20;
//const ll N = 1ll << Log;
const int Maxn = 1e5;
const int Base = 101;
typedef pair < ll , ll > Line;
struct CHT{
set < pair < Line , ll > > S;
set < pair < ll , Line > > I;
map< Line, int> mp;
ll INF = 4e18;
inline void Add(Line X, ll idx)
{
X.F *= -1;
X.S *= -1;
mp[X] = idx;
ll t = -INF;
auto it = S.lower_bound({X, -INF});
while ((int)S.size())
{
if (it == S.begin())
{t = -INF; break;}
it --; ll r = Intersection(it->first, X);
if (r <= it->second)
I.erase({it->second, it->first}), it = S.erase(it);
else
{t = r; break;}
}
it = S.lower_bound({X, -INF});
while ((int)S.size())
{
if (it == S.end())
break;
ll r = Intersection(X, it->first);
Line Y = it->first;
I.erase({it->second, it->first});
it = S.erase(it);
if (r <= t)
{
r = -INF;
if (it != S.begin())
it --, r = Intersection(it->first, Y);
S.insert({Y, r}); I.insert({r, Y}); return ;
}
if (it != S.end() && it->second <= r)
continue;
S.insert({Y, r}); I.insert({r, Y}); break;
}
S.insert({X, t}); I.insert({t, X});
}
inline pll GetMin(ll X)
{
auto it = I.upper_bound({X, {INF, INF}}); it --;
return pll(-(X * it->second.first + it->second.second), mp[it -> S]);
}
inline ll Intersection(Line X, Line Y)
{
if (X.first == Y.first && X.second <= Y.second)
return (-INF);
if (X.first == Y.first)
return (INF);
return ((X.second - Y.second) / (Y.first - X.first)) + ((X.second - Y.second) % (Y.first - X.first) > 0);
}
};
CHT cht;
ll a[Maxn], mk[Maxn];
ll ps[Maxn], sf[Maxn];
ll dp[Maxn][203], par[Maxn][203];
vector<ll> A;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
ps[0] = 0;
for(int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i - 1];
for(int i = 0; i <= n; i++) dp[i][0] = ps[i] * ps[i];
pll res;
for(int j = 1; j <= k; j++){
cht.I.clear();
cht.S.clear();
cht.mp.clear();
dp[0][j] = 0;
for(int i = 1; i <= n; i++){
cht.Add( pll(-2LL * ps[i - 1], dp[i - 1][j - 1] + ps[i - 1]*ps[i - 1] ), i - 1);
res = cht.GetMin(ps[i]);
dp[i][j] = res.F + ps[i]*ps[i];
par[i][j] = res.S;
}
}
cout << ((ps[n] * ps[n]) - dp[n][k]) / 2LL << '\n';
ll nw = n;
for(int i = 0; i < k; i++){
nw = par[nw][k - i];
A.pb(nw);
}
reverse(all(A));
for(auto x : A) cout << x << ' ';
cout << '\n';
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
2 ms |
380 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
8 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 1 == 1 |
9 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 140072 == 140072 |
14 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 376041456 == 376041456 |
15 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 805 == 805 |
16 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 900189994 == 900189994 |
17 |
Correct |
2 ms |
376 KB |
contestant found the optimal answer: 999919994 == 999919994 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
2 ms |
508 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
3 ms |
504 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
3 ms |
504 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
2 ms |
504 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
3 ms |
504 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
3 ms |
508 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
15 ms |
504 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
3 ms |
504 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
3 ms |
1064 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
51 ms |
1088 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
3 ms |
1016 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
13 ms |
1144 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
15 ms |
1016 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
16 ms |
1016 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
6 ms |
1016 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
5 ms |
1020 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
7 ms |
1016 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3704 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
6 ms |
3704 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
94 ms |
3960 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
7 ms |
3704 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
94 ms |
3832 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
84 ms |
3704 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
99 ms |
3832 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
93 ms |
3708 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
24 ms |
3704 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
43 ms |
3704 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
33784 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
54 ms |
33784 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
1884 ms |
34332 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
59 ms |
34424 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
1094 ms |
33912 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
1387 ms |
33784 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
1454 ms |
34296 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
1309 ms |
34296 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
1339 ms |
34040 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
1656 ms |
34088 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
124 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |