#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <algorithm>
#include <deque>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pll;
const int N = 1e5 + 500;
const int K = 215;
inline bool ccw(const pll &A, const pll &B, const pll &C){
return (ld)A.X * (B.Y - C.Y) + (ld)B.X * (C.Y - A.Y) + (ld)C.X * (A.Y - B.Y) <= 0;
}
int ind[N], a[N];
pll hl[N];
int cur = 0, n, k;
ll P[N], lst[N], dp[N];
int rek[N][K], cur_k, sz;
inline void add(const pll &X,const int &i){
//for(;sz >= 2 && ccw(X, hl[sz - 1], hl[sz - 2]); sz--);
hl[sz] = X; ind[sz++] = i;
cur = min(cur, sz - 1);
}
inline ll get(const int &i,const ll &x){
return hl[i].X * x + hl[i].Y;
}
inline int query(const ll &x){
if(sz == 0) return -1;
for(; cur + 1 < sz && get(cur, x) < get(cur + 1, x); cur++);
return cur;
}
inline void dinamika(){
sz = 0; cur = 0;
for(int i = 0;i < n;i++){
int r = query(P[i]);
dp[i] = -1;
if(r >= 0){
rek[i][cur_k] = ind[r];
dp[i] = get(r, P[i]);
}
if(lst[i] >= 0)
add({P[i], lst[i] - P[i] * P[i]}, i);
lst[i] = dp[i];
}
cur_k++;
}
int main(){
scanf("%d%d", &n, &k);
//if(n > 50000 && k > 150) return 0;
for(int i = 0;i < n;i++){
scanf("%d", a + i);
P[i] = a[i] + (i ? P[i - 1] : 0);
}
for(int i = 0;i < k;i++)
dinamika();
printf("%lld\n", dp[n - 1]);
vector < int > fin;
int cur = n - 1;
for(int i = k - 1;i >= 0;i--){
fin.PB(rek[cur][i]);
cur = fin.back();
}
reverse(fin.begin(), fin.end());
for(int x : fin)
printf("%d ", x + 1);
printf("\n");
}
Compilation message
palindrome.cpp: In function 'int main()':
palindrome.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
palindrome.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", a + i);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |