#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[maxn][205], pf[maxn], 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);
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[205];
vector<int> pt(205, -1);
b[0].push_back({0, 0, -1e18, 0});
k++;
for (int i = 1; i <= n; i++) for (int j = 0; j < k; j++)
{
pt[j] = min(pt[j], sz(b[j]) - 2);
while (pt[j] + 1 < sz(b[j]) && (ld)pf[i] >= b[j][pt[j] + 1].x) pt[j]++;
dp[i][j + 1] = b[j][pt[j]].a * pf[i] + b[j][pt[j]].b - pf[i] * pf[i] + pf[n] * pf[i];
pre[i][j + 1] = b[j][pt[j]].p;
iii now;
now.a = pf[i];
now.b = dp[i][j + 1] - pf[i] * pf[n];
now.p = i;
now.x = -1e18;
while (sz(b[j + 1]) >= 2 && ori(b[j + 1][sz(b[j + 1]) - 2], now) <= ori(b[j + 1][sz(b[j + 1]) - 2], b[j + 1][sz(b[j + 1]) - 1]))
b[j + 1].pop_back();
if (b[j + 1].size()) now.x = ori(now, b[j + 1].back());
b[j + 1].emplace_back(now);
}
cout << dp[n][k] << "\n";
trace(n, k);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
2396 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
1 ms |
2396 KB |
Integer 2 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
2652 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
2652 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
2652 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
4956 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
4 ms |
7396 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
4956 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
2 ms |
4956 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
2 ms |
5212 KB |
Integer 200 violates the range [1, 199] |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
3 ms |
5976 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
10 ms |
15392 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
2 ms |
5980 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
14 ms |
15448 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
9 ms |
15576 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
17 ms |
18268 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
11 ms |
16216 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
7628 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
5 ms |
10332 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
36096 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
14 ms |
35832 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Runtime error |
128 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
55 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |