#include<bits/stdc++.h>
#define int long long
#define ld long double
#define sz(u) (int)u.size()
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
int a[maxn], dp[2][205], pf[maxn];
signed pre[maxn][205];
struct iii{
int a, b;
ld x;
int p;
};
inline ld ori(iii a, iii b)
{
int u = a.a, v = a.b;
int x = b.a, y = b.b;
if (u == x) return (ld)1e18;
return (ld)(y - v) / (ld)(u - x);
}
void trace(int n, int k)
{
if (pre[n][k] == 0) return;
trace(pre[n][k], k - 1);
assert(pre[n][k] <= n && pre[n][k] >= 1);
cout << pre[n][k] << " ";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], pf[i] = pf[i - 1] + a[i];
/// dp[i][j] = dp[k - 1][j - 1] + (pf[i] - pf[k - 1]) * (pf[n] - pf[i])
/// dp[i][j] = pf[k - 1] * pf[i] + dp[k - 1][j - 1] + pf[n] * pf[i] - pf[i] ^ 2 - pf[k - 1] * pf[n]
vector<iii> b[2];
int be = 0, cu = 1;
b[0].push_back({0, 0, -1e18, 0});
k++;
for (int j = 0; j < k; j++)
{
b[cu].clear();
for (int i = 1, pt = -1; i <= n; i++)
{
pt = min(pt, sz(b[be]) - 2);
while (pt + 1 < sz(b[be]) && (ld)pf[i] >= b[be][pt + 1].x) pt++;
dp[cu][j + 1] = b[be][pt].a * pf[i] + b[be][pt].b - pf[i] * pf[i] + pf[n] * pf[i];
pre[i][j + 1] = b[be][pt].p;
iii now;
now.a = pf[i];
now.b = dp[cu][j + 1] - pf[i] * pf[n];
now.p = i;
now.x = -1e18;
while (sz(b[cu]) >= 2 && ori(b[cu][sz(b[cu]) - 2], now) <= ori(b[cu][sz(b[cu]) - 2], b[cu][sz(b[cu]) - 1]))
b[cu].pop_back();
if (b[cu].size()) now.x = ori(now, b[cu].back());
b[cu].emplace_back(now);
}
swap(cu, be);
}
cout << dp[be][k] << "\n";
trace(n, k);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
0 ms |
348 KB |
Integer 2 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
604 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
604 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
600 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
1 ms |
604 KB |
Integer 200 violates the range [1, 199] |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
2904 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
5 ms |
2652 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
2648 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
6 ms |
2836 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
6 ms |
2652 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
6 ms |
2652 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
5 ms |
2648 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
3 ms |
2652 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
3 ms |
2652 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9560 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
5 ms |
9492 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
49 ms |
10192 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
4 ms |
10196 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
45 ms |
9504 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
48 ms |
9560 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
49 ms |
10192 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
36 ms |
10192 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
42 ms |
10196 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
54 ms |
10196 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
95220 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
51 ms |
95232 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
502 ms |
96184 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
51 ms |
96188 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
713 ms |
95164 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
452 ms |
92104 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
495 ms |
96188 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
344 ms |
96184 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
399 ms |
95680 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
491 ms |
95512 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |